您的位置:首页 > 其它

hdoj2647Reward《《拓(tuo)扑》》

2016-03-22 13:04 162 查看
Reward

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6708 Accepted Submission(s): 2076

Problem Description

Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.

The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward
will be at least 888 , because it's a lucky number.

Input

One line with two integers n and m ,stands for the number of works and the number of demands .(n<=10000,m<=20000)

then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.

Output

For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.

Sample Input

2 1

1 2

2 2

1 2

2 1

Sample Output

1777

-1

Author

dandelion

Source

曾是惊鸿照影来

越想越难





这星期要ac

额,先放两个wrong 代码,,希望给你们启发,,,

第三个ac...没用vector

第四个用STL——vector的正在努力中。。。。。。。。。。

代码1://用的矩阵。。。Memory Limit Exceeded

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,jisuan,a,b,s;
int rudu[10100];
bool map[10100][10100];
bool fafe[10100];
void topo()
{
queue<int> que;
for (int i=1;i<=n;i++)
if (rudu[i]==0)
{
jisuan++;
que.push(i);
}
while (!que.empty())
{
int now=que.front();
que.pop();
for (int i=1;i<=n;i++)
{
if (map[i][now])
{
rudu[i]--;
map[i][now]=false;
s++;
if (rudu[i]==0)
{
que.push(i);
jisuan++;
}
}
}
}
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
memset(map,false,sizeof(map));
memset(rudu,0,sizeof(rudu));
for (int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=true;
rudu[a]++;
}
s=888*n;
jisuan=0;
topo();
if (jisuan==n)
printf("%d\n",s);
else
printf("-1\n");
}
return 0;
}
代码2://用aaa[ ],bbb[ ] 带替矩阵,,发现wrong了。。。。正在努力中......................

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,jisuan,a,b,s;
/*struct node{
int ge;
int rushu;
int shu;
bool friend operator <(node xx,node yy)
{
return xx.rushu>yy.rushu;
}
};*/
int rudu[10100];
//bool map[10100][10100];
int aaaa[20050],bbbb[20050];
bool fafe[10100];
void topo()
{
queue<int> que;
for (int i=1;i<=n;i++)
if (rudu[i]==0)
{
jisuan++;
que.push(i);
}
while (!que.empty())
{
int now=que.front();
que.pop();
for (int i=0;i<m;i++)
{
if (bbbb[i]==now)
{
bbbb[i]=0;
int kk=aaaa[i];
rudu[kk]--;
s++;
if (rudu[kk]==0)
{
que.push(kk);
jisuan++;
}
}
}

/*for (int i=1;i<=n;i++)
{
if (map[i][now])
{
rudu[i]--;
map[i][now]=0;
//    map[i][now]=false;
s++;
if (rudu[i]==0)
{
que.push(i);
jisuan++;
}
}
}*/
}
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
//memset(map,0,sizeof(map));
memset(rudu,0,sizeof(rudu));
for (int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
aaaa[i]=a;
bbbb[i]=b;
//map[a][b]=1;
rudu[a]++;
}
s=888*n;
jisuan=0;
topo();
if (jisuan==n)
printf("%d\n",s);
else
printf("-1\n");
}
return 0;
}


正确代码:发现上一个代码最后n加上的是m



。。。先已更正。。。。。。

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,jisuan,a,b,s;
int rudu[10100];
int aaaa[20050],bbbb[20050];
bool fafe[10100];
void topo()
{
queue<int> que;
memset(fafe,true,sizeof(fafe));
int ss=0;
while (1)
{

int pp=0;
for (int i=1;i<=n;i++)
if (rudu[i]==0&&fafe[i])
{
pp=1;
jisuan++;
que.push(i);
}
if (pp==0)
break;
while (!que.empty())
{
int now=que.front();
que.pop();s+=ss;
fafe[now]=false;
for (int i=0;i<m;i++)
{
if (bbbb[i]==now)
{
bbbb[i]=0;
int kk=aaaa[i];
rudu[kk]--;
}
}
}
ss++;
}
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
memset(rudu,0,sizeof(rudu));
memset(aaaa,0,sizeof(aaaa));
memset(bbbb,0,sizeof(bbbb));
for (int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
aaaa[i]=a;
bbbb[i]=b;
rudu[a]++;
}
s=888*n;
jisuan=0;
topo();
if (jisuan==n)
printf("%d\n",s);
else
printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: