hdu1285+hdu2467(拓扑排序)
确定比赛名次
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10604 Accepted Submission(s): 4150
。。
,N进行比赛,比赛结束后。裁判委员会要将全部參赛队伍从前往后依次排名,但如今裁判委员会不能直接获得每一个队的比赛成绩。仅仅知道每场比赛的结果,即P1赢P2,用P1。P2表示,排名时P1在P2之前。如今请你编程序确定排名。
输出时队伍号之间有空格,最后一名后面没有空格。
其它说明:符合条件的排名可能不是唯一的。此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。
4 3
1 2
2 3
4 3
1 2 4 3
pid=1811">1811
1548pid=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
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.
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
2 1
1 2
2 2
1 2
2 1
1777
-1
题解:
本题同上也是道比較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(拓扑排序)的更多相关文章
- 确定比赛名次---hdu1285(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 拓扑序就是求一个序列 数 a 出现在数 b 前面,最终输出满足条件的序列即可: 过程就是每次选取 ...
- HDU1285(拓扑排序裸题
..被多组测试坑了一波 #include<iostream> #include<vector> #include<queue> using namespace st ...
- *HDU1285 拓扑排序
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 确定比赛名次---HDU1285(拓扑排序)
http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目大意: 给你每场比赛的成绩,让你根据成绩把排名弄出来 分析: 本来我是用普通方法写的,然后就一直wa, ...
- 拓扑排序 HDU1285
这个题是个模板题,可以直接用拓扑排序的模板来做, AC代码 #include <stdio.h> #include<iostream> #include <string. ...
- hdu1285 确定比赛名次(拓扑排序)
有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道 ...
- 2019/4/22 拓扑排序的高效写法. 模板题HDU1285:确定比赛名次
传送门 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现 ...
- HDU1285 确定名次 拓扑排序
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
- HDU1285 确定比赛问题【拓扑排序+优先队列】
题目 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩, ...
随机推荐
- ubuntu下C编程,编译基础( 转)
buntu下C编程,编译基础 C 编程中相关文件后缀 .a 静态库 (archive) .c C源代码(需要编译预处理) .h C源代码头文件 .i C源代码(不需编译预处理) .o 对象文件 ...
- SetThreadAffinityMask设置线程亲缘性
The SetThreadAffinityMask function sets a processor affinity mask for the specified thread. DWORD_PT ...
- Kruskal-Wallis Test and Friedman test
- js清空页面控件值
function funClear() {var txts = document.getElementsByTagName("input");for (var i = 0; i & ...
- 2014.12.01 B/S之windows8.1下安装IIS
1.打开 控制面板——程序——程序和功能——启用或关闭windows功能 2.找到Internet信息服务 3.等待安装完毕即可 4.控制面板——系统和安全——管理工具——Intern ...
- asp.net预览图片
Aspx code <table> <tr> <td class="style3"> <asp:Label ID="Label1 ...
- Spring整合hibernate4:事务管理[转]
Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTransaction,commit,这些都是重复的工作 ...
- SSDT表详解
SSDT(system service dispatch table) 系统服务分派表 SSPT(system service parameter table) 系统服务参数表 #pragma pac ...
- hdu3516
题目大意:这个....翻译起来还真是不好说,各位四六没过的ACMer正好去原网页看看题意,过了的好孩子还是去看看原网页看看锻炼一下吧.(当然我做这道题目的时候,教练已经摆明说要用四边形不等式,所以还是 ...
- 管理node_modules
http://stackoverflow.com/questions/15225865/centralise-node-modules-in-project-with-subproject