zoukankan      html  css  js  c++  java
  • HDU1285 确定比赛问题【拓扑排序+优先队列】

    题目

    有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。 

    Input

    输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。 

    Output

    给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。 

    其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。 

    Sample Input

    4 3
    1 2
    2 3
    4 3

    Sample Output

    1 2 4 3

    拓扑排序,因为要小的先输出所以想到优先队列

    AC代码

    #include<iostream>
    #include<cstdio>     //EOF,NULL
    #include<cstring>    //memset
    #include<cstdlib>    //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
    #include<cmath>           //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
    #include<algorithm>  //fill,reverse,next_permutation,__gcd,
    #include<string>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<utility>
    #include<iterator>
    #include<iomanip>             //setw(set_min_width),setfill(char),setprecision(n),fixed,
    #include<functional>
    #include<map>
    #include<set>
    #include<limits.h>     //INT_MAX
    #include<bitset> // bitset<?> n
    using namespace std;
    
    typedef long long ll;
    typedef pair<int,int> P;
    #define all(x) x.begin(),x.end()
    #define readc(x) scanf("%c",&x)
    #define read(x) scanf("%d",&x)
    #define read2(x,y) scanf("%d%d",&x,&y)
    #define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
    #define print(x) printf("%d
    ",x)
    #define mst(a,b) memset(a,b,sizeof(a))
    #define pb(x) push_back(x)
    #define lowbit(x) x&-x
    #define lson(x) x<<1
    #define rson(x) x<<1|1
    const int INF =0x3f3f3f3f;
    const int mod = 1e9+7;
    const int MAXN = 1e5+5;
    
    vector<int> Edge[505];
    int in[505];
    int main(){
      int n,m;
      int a,b;  
      while(read2(n,m)!=EOF){
        for(int i = 1; i <= n; i++){
          Edge[i].clear();
          in[i] = 0;
        }
        for(int i = 0; i < m; i++){
          read2(a,b);
          in[b] ++ ;
          Edge[a].pb(b);
        }
        priority_queue<int,vector<int>,greater<int> > q;
        for(int i = 1; i <= n; i++){
          if(in[i]==0)
             q.push(i);
        }
        vector<int> ans;
        while(!q.empty()){
          int p = q.top();
          q.pop();
          ans.push_back(p);
          for(int i = 0; i < Edge[p].size() ; i++){
            int y = Edge[p][i];
            in[y]--;
            if(in[y] == 0){
              q.push(y);
            }
          }
        }
        for(int i = 0; i < ans.size(); i++){
          if(i == 0) printf("%d",ans[i]);
          else  printf(" %d",ans[i]);
        }
        printf("
    ");
      }
    
    }
  • 相关阅读:
    你能用多长时间停车?
    中国威胁论好像还挺严重的
    热爱生命
    lunix下shell脚本批量获取文件,批量修改文件内容
    sql数据操作的若干心得(二)向表中自动插入自增的ID
    Asp.net开发之旅动态产生控件
    Asp.net开发之旅GridView中嵌入DropDownList的一点心得
    Asp.net开发之旅开发环境
    Asp.net开发之旅简单的引用母版页
    Sql数据操作的若干心得
  • 原文地址:https://www.cnblogs.com/llke/p/10780134.html
Copyright © 2011-2022 走看看