确定比赛名次

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10604    Accepted Submission(s): 4150

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
  1. 4 3
  2. 1 2
  3. 2 3
  4. 4 3
 
Sample Output
  1. 1 2 4 3
 
Author
SmallBeer(CML)
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2647 3342 

pid=1811">1811 1548 

pid=1532">1532



题解:

      本题是道比較裸的拓扑排序题。注意重边即可了。还有本题符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在。其次就是输出格式的限定!

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. const int MAXN=500+10;
  6. int inDev[MAXN];
  7. bool visited[MAXN];
  8. bool g[MAXN][MAXN];
  9. int n,m;
  10.  
  11. void init()
  12. {
  13. memset(g,0,sizeof(g));
  14. memset(visited,0,sizeof(visited));
  15. memset(inDev,0,sizeof(inDev));
  16. }
  17.  
  18. void input()
  19. {
  20. int a,b;
  21. for(int i=0;i<m;i++)
  22. {
  23. scanf("%d%d",&a,&b);
  24. if(!g[a][b])
  25. {
  26. g[a][b]=true;
  27. ++inDev[b];
  28. }
  29. }
  30. }
  31.  
  32. void topSort()
  33. {
  34. int i,tag=0;
  35. while(tag<n)
  36. {
  37. for(i=1;i<=n;i++)
  38. if(!visited[i]&&0==inDev[i])
  39. break;
  40. if(tag)
  41. printf(" ");
  42. ++tag;
  43. printf("%d",i);
  44. visited[i]=true;
  45. for(int j=1;j<=n;j++)
  46. if(g[i][j])
  47. --inDev[j];
  48. }
  49. printf("\n");
  50. }
  51.  
  52. int main()
  53. {
  54. while(scanf("%d%d",&n,&m)!=EOF)
  55. {
  56. init();
  57. input();
  58. topSort();
  59. }
  60. return 0;
  61. }

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3580    Accepted Submission(s): 1085

Problem Description
Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.

The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward
will be at least 888 , because it's a lucky number.
 
Input
One line with two integers n and m ,stands for the number of works and the number of demands .(n<=10000,m<=20000)

then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
 
Output
For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.
 
Sample Input
  1. 2 1
  2. 1 2
  3. 2 2
  4. 1 2
  5. 2 1
 
Sample Output
  1. 1777
  2. -1
 
Author
dandelion
 
Source
 

题解:

      本题同上也是道比較easy看出的拓扑排序题,不同的建立拓扑结构图是应该逆向见图。这样可以保证前者比后者大。

  1. #include<iostream>
  2. #include<vector>
  3. #include<cstring>
  4. #include<queue>
  5. using namespace std;
  6.  
  7. const int MAXN=10000+10;
  8. vector<int>g[MAXN];
  9. int inDev[MAXN];
  10. int money[MAXN];
  11. int n,m;
  12.  
  13. void init()
  14. {
  15. for(int i=0;i<MAXN;i++)
  16. {
  17. g[i].clear();
  18. money[i]=888;
  19. }
  20. memset(inDev,0,sizeof(inDev));
  21. }
  22.  
  23. void input()
  24. {
  25. int a,b;
  26. for(int i=0;i<m;i++)
  27. {
  28. scanf("%d%d",&a,&b);
  29. g[b].push_back(a);
  30. ++inDev[a];
  31. }
  32. }
  33.  
  34. void topSort()
  35. {
  36. queue<int>q;
  37. int tag=0,ans=0;
  38. for(int i=1;i<=n;i++)
  39. {
  40. if(inDev[i]==0)
  41. q.push(i);
  42. }
  43. while(!q.empty())
  44. {
  45. ++tag;
  46. int now=q.front();
  47. ans+=money[now];
  48. q.pop();
  49. for(int i=0;i<g[now].size();i++)
  50. {
  51. int next=g[now][i];
  52. if(--inDev[next]==0)
  53. {
  54. q.push(next);
  55. money[next]=money[now]+1;
  56. }
  57. }
  58. }
  59. if(tag!=n)
  60. ans=-1;
  61. printf("%d\n",ans);
  62. }
  63.  
  64. int main()
  65. {
  66. while(scanf("%d%d",&n,&m)!=EOF)
  67. {
  68. init();
  69. input();
  70. topSort();
  71. }
  72. return 0;
  73. }

 

246

hdu1285+hdu2467(拓扑排序)的更多相关文章

  1. 确定比赛名次---hdu1285(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 拓扑序就是求一个序列 数 a 出现在数 b 前面,最终输出满足条件的序列即可: 过程就是每次选取 ...

  2. HDU1285(拓扑排序裸题

    ..被多组测试坑了一波 #include<iostream> #include<vector> #include<queue> using namespace st ...

  3. *HDU1285 拓扑排序

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. 确定比赛名次---HDU1285(拓扑排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目大意: 给你每场比赛的成绩,让你根据成绩把排名弄出来 分析: 本来我是用普通方法写的,然后就一直wa, ...

  5. 拓扑排序 HDU1285

    这个题是个模板题,可以直接用拓扑排序的模板来做, AC代码 #include <stdio.h> #include<iostream> #include <string. ...

  6. hdu1285 确定比赛名次(拓扑排序)

    有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道 ...

  7. 2019/4/22 拓扑排序的高效写法. 模板题HDU1285:确定比赛名次

    传送门 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现 ...

  8. HDU1285 确定名次 拓扑排序

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...

  9. HDU1285 确定比赛问题【拓扑排序+优先队列】

    题目 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩, ...

随机推荐

  1. SQL Server带游标的SQL

    DECLARE test_cursor CURSOR FOR SELECT ID FROM dbo.T_BD_Restaurant WHERE id <> '0AAB2E55-79F8-4 ...

  2. ListFragment和ListActivity的setOnItemClickListener不起作用

    在使用ListFragment时,发现一个奇怪的问题,就是getListView().setOnItemClickListener(new OnItemClickListener...)不起作用.在s ...

  3. overload的一点思考

    仅参数类型不同的重载方法,使用过程的一个困惑: 有没有必要使用instanceof方法? package overload.special; public class OverLoadTest { p ...

  4. 通过jpegoptim批量压缩文件

    #!/bin/sh filelist=$(ls) for file in $filelist do if [ -d $file ] then du -h $file /usr/local/bin/jp ...

  5. JavaEE Tutorials (21) - Java EE安全:高级主题

    21.1使用数字证书331 21.1.1创建服务器证书332 21.1.2向证书安全域增加用户334 21.1.3为GlassFish服务器使用一个不同的服务器证书33421.2认证机制335 21. ...

  6. windows和linux下获取当前程序路径以及cpu数

    #ifdef WIN32 #include <Windows.h> #else #include <stdio.h> #include <unistd.h> #en ...

  7. POJ 2429 GCD & LCM Inverse(Pollard_Rho+dfs)

    [题目链接] http://poj.org/problem?id=2429 [题目大意] 给出最大公约数和最小公倍数,满足要求的x和y,且x+y最小 [题解] 我们发现,(x/gcd)*(y/gcd) ...

  8. java快速排序1000万无序数组JVM-Xmx=256M 耗时2s

    自己动手写排序算法,快速排序是比较不好写的了~ import java.util.*; class Test{ public void quickSort(int[] arr,int low,int ...

  9. mysql数据库学习(一)--基础

    一.简介 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 R ...

  10. hdu 1520Anniversary party(简单树形dp)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...