4 3 1 2 2 3 4 3
1 2 4 3
#include#include using namespace std; const int MAXN=500+10; int inDev[MAXN]; bool visited[MAXN]; bool g[MAXN][MAXN]; int n,m; void init() { memset(g,0,sizeof(g)); memset(visited,0,sizeof(visited)); memset(inDev,0,sizeof(inDev)); } void input() { int a,b; for(int i=0;i
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3580 Accepted Submission(s): 1085
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 Input2 1 1 2 2 2 1 2 2 1
Sample Output1777 -1
Author dandelion
Source 曾是驚鴻照影來 題解:
本題同上也是道比較容易看出的拓撲排序題,不同的建立拓撲結構圖是應該逆向見圖,這樣能夠保證前者比後者大。
#include#include #include #include using namespace std; const int MAXN=10000+10; vector g[MAXN]; int inDev[MAXN]; int money[MAXN]; int n,m; void init() { for(int i=0;i q; int tag=0,ans=0; for(int i=1;i<=n;i++) { if(inDev[i]==0) q.push(i); } while(!q.empty()) { ++tag; int now=q.front(); ans+=money[now]; q.pop(); for(int i=0;i
246