zoukankan      html  css  js  c++  java
  • 杭电 1285 确定比赛名次

    确定比赛名次

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 9696    Accepted Submission(s): 3790


    Problem Description
    有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
     
    这是关于拓扑排序的问题,首先应该统计每个顶点的入度,从入度为0的顶点开始,将入度为0的顶点处理之后并把它的邻接点的入度减1,知道所有顶点处理完毕。
    下面是代码:
     1 #include <iostream>
     2 #include <queue>
     3 using namespace std;
     4 #define MAX 505
     5 int map[MAX][MAX];
     6 int main()
     7 {
     8     int p1, p2;
     9     int n, m;
    10     int indegree[MAX], i, j;
    11     priority_queue<int, vector<int>, greater<int> > q;  //用优先队列保存入度为0的顶点,这样就可以使输出的顶点是从小到大的顺序
    12     while(cin>>n>>m)
    13     {
    14         while(!q.empty())
    15             q.pop();
    16         for (i=0; i<n; i++)
    17             indegree[i] = 0;
    18         for (i=0; i<n; i++)
    19             for(j=0; j<n; j++)
    20                 map[i][j] = 0;
    21         for (i=0; i<m; i++)
    22         {
    23             cin>>p1>>p2;
    24             if(!map[p1-1][p2-1])    //一定要加判断,否则有可能输入重边,结果就会WA
    25             {
    26                 map[p1-1][p2-1] = 1;
    27                 indegree[p2-1]++;
    28             }
    29         }
    30         for (i=0; i<n; i++)
    31             if (!indegree[i])
    32                 q.push(i+1);
    33         while (!q.empty())
    34         {
    35             j = q.top();
    36             q.pop();
    37             cout<<j;
    38             for (i=0; i<n; i++)
    39                 if (map[j-1][i])
    40                 {
    41                     indegree[i]--;
    42                     if (!indegree[i])
    43                         q.push(i+1);
    44                 }
    45             if(!q.empty())
    46                 cout<<" ";
    47         }
    48         cout<<endl;
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    AIMS 2013中的性能报告工具不能运行的解决办法
    读懂AIMS 2013中的性能分析报告
    在线研讨会网络视频讲座 方案设计利器Autodesk Infrastructure Modeler 2013
    Using New Profiling API to Analyze Performance of AIMS 2013
    Map 3D 2013 新功能和新API WebCast视频下载
    为Autodesk Infrastructure Map Server(AIMS) Mobile Viewer创建自定义控件
    ADN新开了云计算Cloud和移动计算Mobile相关技术的博客
    JavaScript修改css样式style
    文本编辑神器awk
    jquery 开发总结1
  • 原文地址:https://www.cnblogs.com/yazhou/p/3601310.html
Copyright © 2011-2022 走看看