标签:
Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 628 Accepted Submission(s): 244
代码:
#include<stdio.h> #include<queue> #include<vector> using namespace std; #include<string.h> const int MAXN=100010; struct Node{ int to,next; }; Node edg[MAXN*10]; int head[MAXN],ans[MAXN],top,n,que[MAXN]; priority_queue<int>dl; typedef __int64 LL; void topu(){ int temp=0; for(int i=1;i<=n;i++){ if(!que[i])dl.push(i); } while(!dl.empty()){ int k=dl.top(); dl.pop(); //temp++; ans[top++]=k; for(int j=head[k];j!=-1;j=edg[j].next){ que[edg[j].to]--; if(!que[edg[j].to])dl.push(edg[j].to); } } int x = ans[0]; LL sum = 0; for(int i = 0;i < top;i++){ // printf("%d ", ans[i]); if(ans[i] < x){ x = ans[i]; } sum += x; }//puts(""); printf("%I64d\n", sum); } void initial(){ memset(head,-1,sizeof(head)); memset(que,0,sizeof(que)); while(!dl.empty())dl.pop(); top=0; } int main(){ int T,M,a,b; scanf("%d",&T); while(T--){ initial(); scanf("%d%d",&n,&M); for(int i=0;i<M;i++){ scanf("%d%d",&b,&a); edg[i].to=a; edg[i].next=head[b]; head[b]=i; que[a]++; } topu(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/5517960.html