HDU 1285 确定比赛名次

传送门;http://acm.hdu.edu.cn/showproblem.php?pid=1285

解题思路:

优先队列+拓扑排序,但是优先队列要小的先出来。

实现代码:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 using namespace std;
 8 
 9 const int MAXN=505;
10 int deg[MAXN];
11 vector<int> G[MAXN];
12 
13 struct Node{
14     int u;
15     Node(int u):u(u){};
16     bool operator<(const Node&rhs) const{
17         return u>rhs.u;
18     }
19 };
20 int cnt=0;
21 void toplogicSort(int n){
22     priority_queue<Node>q;
23     for(int i=1;i<=n;i++)
24         if(!deg[i]) q.push(Node(i));
25     while(!q.empty()){
26         int u=(q.top()).u;
27         printf("%d%c",u,cnt+1!=n?' ':'\n');
28         cnt++;
29         q.pop();
30         for(int i=0;i<G[u].size();i++){
31             if(!--deg[G[u][i]])
32                 q.push(Node(G[u][i]));
33         }
34 
35     }
36 }
37 
38 int main(){
39     int N,M;
40     while(scanf("%d%d",&N,&M)!=EOF){
41         memset(deg,0,sizeof(deg));
42         for(int i=0;i<=N;i++)
43             G[i].clear();
44         for(int i=0;i<M;i++){
45             int u,v;
46             scanf("%d%d",&u,&v);
47             G[u].push_back(v);
48             deg[v]++;
49         }
50         cnt=0;
51         toplogicSort(N);
52     }
53 }

 

posted on 2017-04-16 22:39  mkfoy  阅读(248)  评论(0编辑  收藏  举报

导航