HDU 1285 确定比赛名次

一道简单的拓扑排序,因为优先选小的所以用了优先队列







#include<algorithm> #include<cstring> #include<vector> #include<queue> #include<cstdio> using namespace std; vector<int> eg[505]; int ans[505], ct[505]; struct Node { bool operator < (Node b)const { return va>b.va; } int va; Node(){} Node(int x){va=x;} } ; priority_queue<Node>q; int main() { int i, j, n, m, now, a, b; while(scanf("%d%d",&n,&m)!=EOF) { now=0; memset(ct,0,sizeof(ct)); for(i=1;i<=n;i++) eg[i].clear(); for(i=0; i<m; i++) { scanf("%d%d",&a,&b); eg[a].push_back(b); ct[b]++; } int k=-1; for(i=1; i<=n; i++) if(ct[i]==0) q.push(Node(i)); int tp, to; while(!q.empty()) { tp=q.top().va; ans[now++]=tp; q.pop(); for(i=0; i<eg[tp].size(); i++) { to=eg[tp][i]; ct[to]--; if(ct[to]==0) q.push(Node(to)); } } for(i=0; i<now; i++) { if(i>0) printf(" "); printf("%d",ans[i]); } puts(""); } return 0; }

 

posted @ 2013-09-11 18:37  Ink_syk  阅读(141)  评论(0编辑  收藏  举报