http://acm.hdu.edu.cn/showproblem.php?pid=2647
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.
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
代码:
#include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std; #define N 10005 int in[N]; int ran[N]; vector<int> v[N]; int n,m; bool tp() { queue<int> q; for(int i=1;i<=n;i++) if(in[i]==0) q.push(i); int count=0; while(!q.empty()) { count++; int t=q.front(); q.pop(); for(int i=0;i<v[t].size();i++ ) { int k=v[t][i]; ran[k]=max(ran[k],ran[t]+1);//记录k节点有多少前继节点,为得到至少得到的工资数。 if(in[k]==1) q.push(k); else in[k]--; } } if(count==n) return true;//判断是否形成环。 else return false; } int main() { while(cin>>n>>m) { for(int i=0;i<=n;i++) { v[i].clear(); in[i]=0; ran[i]=0; } for(int i=0;i!=m;i++) { int a,b; cin>>a>>b; v[b].push_back(a);//把后继点存在本节点的邻接表内。 in[a]++;//记录每一点的入度。 } if(tp()) { int ans=0; for(int i=1;i<=n;i++) ans+=ran[i]; cout<<n*888+ans<<endl; } else cout<<"-1"<<endl; }return 0; }