标签:
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 const int MAXN=10010; 6 struct Node{ 7 int to,next; 8 }; 9 Node edg[MAXN*2]; 10 int head[MAXN]; 11 int que[MAXN]; 12 queue<int>dl; 13 int money,N,M,flot; 14 void topu(){ 15 int temp; 16 for(int i=1;i<=N;i++){ 17 if(!que[i]){ 18 dl.push(i); 19 } 20 } 21 int value=888; 22 temp=0; 23 while(!dl.empty()){ 24 int t=dl.size(); 25 while(t--){ 26 int k=dl.front(); 27 money+=value; 28 dl.pop(); 29 temp++; 30 for(int j=head[k];j!=-1;j=edg[j].next){ 31 que[edg[j].to]--; 32 if(!que[edg[j].to])dl.push(edg[j].to); 33 } 34 } 35 value++; 36 } 37 if(temp==N) 38 printf("%d\n",money); 39 else puts("-1"); 40 } 41 void initial(){ 42 memset(head,-1,sizeof(head)); 43 memset(que,0,sizeof(que)); 44 while(!dl.empty())dl.pop(); 45 money=0;flot=1; 46 } 47 int main(){ 48 int a,b; 49 while(~scanf("%d%d",&N,&M)){ 50 initial(); 51 for(int i=0;i<M;i++){ 52 scanf("%d%d",&a,&b); 53 edg[i].to=a; 54 edg[i].next=head[b]; 55 head[b]=i; 56 que[a]++; 57 } 58 topu(); 59 } 60 return 0; 61 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4730755.html