hdu1285+hdu2467(拓扑排序)
2016-02-22 14:52
211 查看
确定比赛名次
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10604 Accepted Submission(s): 4150
[align=left]Problem Description[/align]
有N个比赛队(1<=N<=500)。编号依次为1。2,3。。。
。。
,N进行比赛,比赛结束后。裁判委员会要将全部參赛队伍从前往后依次排名,但如今裁判委员会不能直接获得每一个队的比赛成绩。仅仅知道每场比赛的结果,即P1赢P2,用P1。P2表示,排名时P1在P2之前。如今请你编程序确定排名。
[align=left]Input[/align]
输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;当中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1。P2表示即P1队赢了P2队。
[align=left]Output[/align]
给出一个符合要求的排名。
输出时队伍号之间有空格,最后一名后面没有空格。
其它说明:符合条件的排名可能不是唯一的。此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。
[align=left]Sample Input[/align]
4 3 1 2 2 3 4 3
[align=left]Sample Output[/align]
1 2 4 3
[align=left]Author[/align]
SmallBeer(CML)
[align=left]Source[/align]
杭电ACM集训队训练赛(VII)
[align=left]Recommend[/align]
lcy | We have carefully selected several similar problems for you: 2647 3342 1811 1548 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]) { 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
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3580 Accepted Submission(s): 1085
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2 1 1 2 2 2 1 2 2 1
[align=left]Sample Output[/align]
1777 -1
[align=left]Author[/align]
dandelion
[align=left]Source[/align]
曾是惊鸿照影来
题解:
本题同上也是道比較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
相关文章推荐
- 访问php程序无法解析,排查步骤
- 形式参数和返回值的问题深入研究
- 发布 Victor 串口控件 1.5.0.5 for C++ Builder 10 (CX) / XE8 / XE7 / XE6
- iOS开发 webView加载html数据
- 【宏芸老师】人生的十枚金币
- 指针和二维数组
- C++学习笔记:重载 函数调用符()
- linux java环境变量配置
- Cordova使用Webview
- C#实现PID控制的模拟测试和曲线绘图
- 分治算法
- 没错,我就是脚本小子
- 基于S3C6410的ARM11学习(四) 核心初始化之设置处理器模式
- Linux计划书
- 进程同步,条件变量,pthread_cond_wait,pthread_cond_init,PTHREAD_COND_INITIALIZER
- NGUI之OnDrag
- 一步步入门编写PHP扩展
- 一年成为Emacs高手(像神一样使用编辑器)
- JQ中 :not .not 伪类与方法之间的选择
- UIKit动力学---毛毛虫案例