hdoj 2647 Reward【反向拓扑排序】

时间:2021-06-13 07:44:10

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5617    Accepted Submission(s):
1707

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
 
唉!用优先队列WA了一下午,现在想想,人家题中也没说这种如果同样优先级谁放前边的话,而且根据题意同样优先级的人发的钱数应该一样
题解:发奖金,每个人至少888,但是一些工作做得好的觉得应该多拿,所以就排出了优先级,优先级高的要比优先级低的多拿,优先级相同的拿同样的钱,
        问最少发出去多少奖金;
题解:还是要用反向拓扑,因为所给的数据可能不只是一棵树,而是森林,
这里给出一组数据
3  2
1  2
1  3
结果是2665不是2666
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#define MAX 20010
using namespace std;
vector<int>map[MAX];
int vis[MAX];
int reward[MAX];
int n,m,sum;
void getmap()
{
int i,j;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
map[i].clear();
for(i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
map[b].push_back(a);
vis[a]++;
}
}
void tuopu()
{
int i,j,ok=0,sum=0;
queue<int>q;
memset(reward,0,sizeof(reward));
while(!q.empty())
q.pop();
for(i=1;i<=n;i++)
if(vis[i]==0)
{
q.push(i);
reward[i]=888;
}
int u,v;
int ans=0;
while(!q.empty())
{
u=q.front();
q.pop();
ans++;
for(i=0;i<map[u].size();i++)
{
v=map[u][i];
vis[v]--;
if(vis[v]==0)
{
q.push(v);
reward[v]=reward[u]+1;
}
}
}
if(ans!=n)
printf("-1\n");
else
{
for(i=1;i<=n;i++)
sum+=reward[i];
printf("%d\n",sum);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
getmap();
tuopu();
}
return 0;
}