Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5336 Accepted Submission(s): 1619
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
Author
dandelion
题解: 这个题可以用dfs做,也可以用拓扑排序,但要注意两点,因为是要求前者比后者的值要大,所以要反向加边,而且在排序的时候,没加入一个点,就要将其后面的价钱值更新,取这个点的价钱值+1和后面点本身的价钱值得最大值。
代码:
1 #include2 #include 3 #include 4 using namespace std; 5 #define N 10005 6 #define M 20005 7 int in[N]; 8 int que[N]; 9 int my[N];10 struct Edge{11 int to ;12 int next;13 }edge[M];14 int head[N];15 int Enct;16 void init()17 {18 Enct = 0;19 memset(head,-1,sizeof(head));20 memset(my,0,sizeof(my));21 memset(in,0,sizeof(in));22 }23 void add(int from, int to)24 {25 edge[Enct].to = to;26 edge[Enct].next = head[from];27 head[from] = Enct++;28 }29 int c;30 int t;31 int n;32 int tm ;33 bool tp()34 {35 c = 0;36 t = 1;37 for(int i = 0 ;i < n ;i++)38 {39 if(in[i]==0)40 {41 que[c++] = i;42 }43 }44 for(int i = 0 ;i < c ; i++)45 {46 int id = que[i];47 for(int j = head[id] ; j !=-1 ; j = edge[j].next)48 {49 int t = my[id];50 Edge e = edge[j];51 in[e.to]--;52 my[e.to] = max(t+1,my[e.to]);//后继大于前驱,每次都要更新 53 if(in[e.to]==0)54 {55 que[c++] = e.to;56 }57 }58 }59 if(c <= n ;i++)80 {81 sum+=my[i];82 }83 printf("%d\n",888*n+sum);84 }85 }86 return 0;87 }