http://acm.hdu.edu.cn/showproblem.php?pid=2647
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9821 Accepted Submission(s): 3136
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.
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.
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
Recommend
记录每一层的人数,统计答案。。。
唉,蒟蒻就是弱、、边的方向搞错了调半天。。。
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 6 using namespace std; 7 8 const int N(100000+5); 9 const int M(200000+5); 10 int sumedge,head[N]; 11 struct Edge 12 { 13 int v,next; 14 Edge(int v=0,int next=0):v(v),next(next){} 15 }edge[M]; 16 inline void ins(int u,int v) 17 { 18 edge[++sumedge]=Edge(v,head[u]); 19 head[u]=sumedge; 20 } 21 22 queue<int>que; 23 int rd[N],ans,cnt,sum[N]; 24 25 inline void init() 26 { 27 sumedge=0; 28 for(;!que.empty();) que.pop(); 29 memset(rd,0,sizeof(rd)); 30 memset(sum,0,sizeof(sum)); 31 memset(edge,0,sizeof(edge)); 32 memset(head,0,sizeof(head)); 33 } 34 35 int AC() 36 { 37 for(int n,m,if_;~scanf("%d%d",&n,&m);init()) 38 { 39 if_=n;ans=0;cnt=1; 40 for(int u,v;m--;) 41 { 42 scanf("%d%d",&u,&v); 43 ins(v,u); rd[u]++; 44 } 45 for(int i=1;i<=n;i++) 46 if(!rd[i]) que.push(i); 47 for(int u,v;!que.empty();) 48 { 49 if_--;ans+=888; 50 u=que.front(); que.pop(); 51 for(int v,i=head[u];i;i=edge[i].next) 52 { 53 v=edge[i].v; 54 if(--rd[v]==0) 55 sum[v]=sum[u]+1,que.push(v); 56 } 57 } 58 for(int i=1;i<=n;i++) ans+=sum[i]; 59 if(if_) puts("-1"); 60 else printf("%d ",ans); 61 } 62 return 0; 63 } 64 65 int I_want_AC=AC(); 66 int main(){;}