zoukankan      html  css  js  c++  java
  • 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?' ':'
    ');
    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 }
  • 相关阅读:
    课程评价
    6.1-6.7 第十六周总结
    5.31 软件开发日志
    5.25-5.31 第十五周总结
    5.30 软件开发日志
    5.29 软件开发日志
    5.28 软件开发日志
    对搜狗输入法的评价
    找水王
    用户模板/用户场景
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6720423.html
Copyright © 2011-2022 走看看