Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3713 Accepted Submission(s): 1125
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
传送门:【HDU】2647 Reward
题目分析:拓扑排序确定递推关系。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REPV( i , a , b ) for ( int i = a ; i >= b ; -- i ) #define clear( a , x ) memset ( a , x , sizeof a ) typedef long long Int ; const int MAXN = 10005 ; const int MAXE = 1000000 ; struct Edge { int v , n ; } ; Edge edge[MAXE] ; int adj[MAXN] , cntE ; int in[MAXN] ; int Q[MAXN] , head , tail ; int cnt ; int d[MAXN] ; int n , m ; void addedge ( int u , int v ) { edge[cntE].v = v ; edge[cntE].n = adj[u] ; adj[u] = cntE ++ ; } void DAG () { REPF ( i , 1 , n ) d[i] = 888 ; head = tail = 0 ; cnt = 0 ; REPF ( i , 1 , n ) if ( !in[i] ) Q[tail ++] = i ; while ( head != tail ) { int u = Q[head ++] ; cnt ++ ; for ( int i = adj[u] ; ~i ; i = edge[i].n ) { int v = edge[i].v ; if ( d[v] < d[u] + 1 ) d[v] = d[u] + 1 ; if ( 0 == ( -- in[v] ) ) Q[tail ++] = v ; } } if ( cnt < n ) printf ( "-1\n" ) ; else { int ans = 0 ; REPF ( i , 1 , n ) ans += d[i] ; printf ( "%d\n" , ans ) ; } } void work () { int u , v ; while ( ~scanf ( "%d%d" , &n , &m ) ) { clear ( adj , -1 ) ; clear ( in , 0 ) ; cntE = 0 ; while ( m -- ) { scanf ( "%d%d" , &u , &v ) ; ++ in[u] ; addedge ( v , u ) ; } DAG () ; } } int main () { work () ; return 0 ; }