HDU 2647 Reward

来源:互联网 发布:超链接调用js方法 编辑:程序博客网 时间:2024/06/16 06:51

Reward

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


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 11 22 21 22 1
 

Sample Output
1777-1
题意: 给你n个工人 老板要给工人发奖金,奖金最低为 888 给你m个关系 a b 表示a的奖金要比b的奖金多。。问你老板最少要花费
多少钱 如果矛盾的话 ,就输出-1 。
思路 : 存图的时候注意要是钱最小的为没有入度的点。 ans 记录每个人的得到的最少的钱。 fincnt 判断是否有矛盾存在
如果有矛盾存在那么 fincnt不可能等于n 。
代码:
#include<stdio.h>#include<string.h>#include<queue>#include<iostream>#include<algorithm>#define N 105using namespace std;struct node{int u,v,next;}edge[N];int ingree[N],head[N];int n,m,cnt,fin;void add(int u,int v){edge[++cnt].u=u;edge[cnt].v=v;edge[cnt].next=head[u];head[u]=cnt;return ;}int main(){int u,v,i;while(scanf("%d %d",&n,&m)!=EOF){if(n==0&&m==0) break;memset(ingree,0,sizeof(ingree));cnt=0;memset(head,-1,sizeof(head));for(i=1;i<=m;i++){scanf("%d %d",&u,&v);add(u,v);ingree[v]++;}fin=0;queue< int >q;for(i=0;i<n;i++){if(ingree[i]==0){q.push(i);fin++;}}int temp;while(!q.empty()){temp=q.front();//printf("%d ",temp);q.pop();for(int k=head[temp];k!=-1;k=edge[k].next){v=edge[k].v;ingree[v]--;if(ingree[v]==0){fin++;q.push(v);}}}if(fin==n) printf("YES\n");else printf("NO\n");}return 0;} 


原创粉丝点击