Problem Descriptiondandelion ' s uncle is a boss of a factory. As the Spring Festival is coming, he wants to distribute rewards to his workers. Now he had a trouble about what to distribute the rewards.
The workers would compare their rewards, and some one may has 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'll be at least 888, because it's a lucky number.
Inputone line with a 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 the integers a and B, stands for a ' s reward should is more than B ' s.
Outputfor 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 11 22 21 22 1
Sample Output
1777-1
Authordandelion
Topology Sort:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Queue>typedef Long long ll;using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define RE P (i, n) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) const int maxn=10100;int h Ead[maxn*2],next[maxn*2],end[maxn*2];int in[maxn],val[maxn];int n,m,cnt,k;void toposort () {int sum=0; k=0; queue<int>q; REPF (I,1,n) if (!in[i]) Q.push (i); while (!q.empty ()) {int V=q.front (); SUM+=VAL[V]; Q.pop (); k++; for (int i=head[v];i!=-1;i=next[i]) {if (--in[end[i]]==0) {Q.push (end[i]); val[end[i]]=val[v]+1; }}} if (k==n) printf ("%d\n", sum); else printf (" -1\n");} void Add (int x,int y) {next[cnt]=head[x]; End[cnt]=y; head[x]=cnt++;} int main () {int x, y; while (~SCANF ("%d%d", &n,&m)) { cnt=0; REPF (i,1,n) val[i]=888; CLEAR (in,0); CLEAR (head,-1); REP (i,m) {scanf ("%d%d", &x,&y); Add (y,x); in[x]++; } toposort (); } return 0;}
HDU 2647 Reward (topological sort)