确定比赛名次

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
4 3
1 2
2 3
4 3
 
Sample Output
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



题解:

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

#include<iostream>
#include<cstring>
using namespace std; const int MAXN=500+10;
int inDev[MAXN];
bool visited[MAXN];
bool g[MAXN][MAXN];
int n,m; void init()
{
memset(g,0,sizeof(g));
memset(visited,0,sizeof(visited));
memset(inDev,0,sizeof(inDev));
} void input()
{
int a,b;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
if(!g[a][b])
{
g[a][b]=true;
++inDev[b];
}
}
} void topSort()
{
int i,tag=0;
while(tag<n)
{
for(i=1;i<=n;i++)
if(!visited[i]&&0==inDev[i])
break;
if(tag)
printf(" ");
++tag;
printf("%d",i);
visited[i]=true;
for(int j=1;j<=n;j++)
if(g[i][j])
--inDev[j];
}
printf("\n");
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
input();
topSort();
}
return 0;
}

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
2 1
1 2
2 2
1 2
2 1
 
Sample Output
1777
-1
 
Author
dandelion
 
Source
 

题解:

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

#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std; const int MAXN=10000+10;
vector<int>g[MAXN];
int inDev[MAXN];
int money[MAXN];
int n,m; void init()
{
for(int i=0;i<MAXN;i++)
{
g[i].clear();
money[i]=888;
}
memset(inDev,0,sizeof(inDev));
} void input()
{
int a,b;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
g[b].push_back(a);
++inDev[a];
}
} void topSort()
{
queue<int>q;
int tag=0,ans=0;
for(int i=1;i<=n;i++)
{
if(inDev[i]==0)
q.push(i);
}
while(!q.empty())
{
++tag;
int now=q.front();
ans+=money[now];
q.pop();
for(int i=0;i<g[now].size();i++)
{
int next=g[now][i];
if(--inDev[next]==0)
{
q.push(next);
money[next]=money[now]+1;
}
}
}
if(tag!=n)
ans=-1;
printf("%d\n",ans);
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
input();
topSort();
}
return 0;
}

 

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. J2SE知识点摘记(一)

    1.        数组的声明时无法指定数组的长度. 2.        一维数组的声明和内存的分配 "数据类型    数组名[]; //声明一维数组     数组名=  new 数据类型[ ...

  2. ECO

    ECO(生态环保的简写) 在与环保相关的概念和资料中,ECO 是ECOLOGICAL的缩写,用来表示生态环保的意思.与之相关产生了一些新的名词,尤其是在房地产楼书中常常被提及,如: ECO-HOME, ...

  3. 论山寨手机与Android 【14】3G SmartPhone时代的MTK

    分析了SmartPhone的里里外外以后,现在我们可以分析MTK的机遇和挑战了.MTK面临的外部环境在发生变化,变化有两条,一是移动网络从2G演变到3G,二是手机由FeaturePhone演化到Sma ...

  4. python 生成器理解

    通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素 ...

  5. 垃圾回收算法简单介绍——JVM读书笔记&lt;二&gt;

    垃圾回收的过程主要包含两部分:找出已死去的对象.移除已死去的对象. 确定哪些对象存活有两种方式:引用计数算法.可达性分析算法. 方案一:引用计数算法 给对象中加入一个引用计数器.每当有一个地方引用它时 ...

  6. 列"xx"不在表Table中

    在数据库中用了left join来查一个表的所有列和另一个表的一个列,但无论用IDataReader还是DataSet都不能获取到另一个表的列,调试时总是说没有那个值,但在数据库中执行语句又有.一直想 ...

  7. php,ajax登陆退出

    利用ajax可以做到页面无刷新登陆. 运行效果 目录结构 site/ css/ images/ js/ site/css/bootstrap.css(bootstrap样式表) site/js/boo ...

  8. [Android]Plug-in com.android.ide.eclipse.adt was unable to load class com.android.ide

    今天启动eclipse的时候报了上述错误,打开xml是都报错.其实解决方法很简单:重启eclipse即可.

  9. Java中如何把两个数组合并为一个

    在Java中,如何把两个String[]合并为一个? 看起来是一个很简单的问题.但是如何才能把代码写得高效简洁,却还是值得思考的.这里介绍四种方法,请参考选用. 一.apache-commons 这是 ...

  10. new String[0]的作用

    返回包含此 collection 中所有元素的数组:返回数组的运行时类型与指定数组的运行时类型相同.如果指定的数组能容纳 该 collection,则返回包含此 collection 元素的数组.否则 ...