【最短路】poj3660 最短路的应用
2015-07-14 14:02
211 查看
题意:翻译来自scy
Input
* Line 1: Two space-separated integers: N and M
第一行n和m
(1 ≤ N ≤ 100)
(1 ≤ M ≤ 4,500)
* Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B
下来m行,每行两个数A和B,A是胜出者。
Output
* Line 1: A single integer representing the number of cows whose ranks can be determined
输出一行,一个数,代表能被确定排名的牛的数目。
Sample Input
5 5
4 3
4 2
3 2
1 2
2 5
Sample Output
2
做法:其实就是最短路的应用。因为数据范围比较小,本题可以直接用Floyd跑一遍最短路(其实不用记录数值,只需要判断是否联通),如果i赢了a个人(d[i][j]!=INF),输给了b个人(d[j][i]==INF),若a+b==n-1,则i的排名可以确定。
Input
* Line 1: Two space-separated integers: N and M
第一行n和m
(1 ≤ N ≤ 100)
(1 ≤ M ≤ 4,500)
* Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B
下来m行,每行两个数A和B,A是胜出者。
Output
* Line 1: A single integer representing the number of cows whose ranks can be determined
输出一行,一个数,代表能被确定排名的牛的数目。
Sample Input
5 5
4 3
4 2
3 2
1 2
2 5
Sample Output
2
做法:其实就是最短路的应用。因为数据范围比较小,本题可以直接用Floyd跑一遍最短路(其实不用记录数值,只需要判断是否联通),如果i赢了a个人(d[i][j]!=INF),输给了b个人(d[j][i]==INF),若a+b==n-1,则i的排名可以确定。
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> using namespace std; const int N=110,M=5000,INF=(int)1e8; int n,m; bool t ; int d ; void floyd() { int i,j,k; for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } void solve() { int i,j; int g ,f ; memset(g,0,sizeof(g)); memset(f,0,sizeof(f)); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j) continue; if(d[i][j]<INF) g[i]++; if(d[j][i]<INF) f[i]++; } } int ans=0; for(i=1;i<=n;i++) if(g[i]+f[i]==n-1) ans++; printf("%d\n",ans); } int main() { int i,j; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(i==j) d[i][i]=0; else d[i][j]=INF; memset(t,0,sizeof(t)); for(i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); t[x]=1;t[y]=1; d[x][y]=1; } floyd(); for(i=1;i<=n;i++) if(!t[i]) {printf("0\n");return 0;} solve(); }
相关文章推荐
- 封装好的Folyd建图,C++源码
- 图论学习笔记之一——Floyd算法
- poj 3249 Test for Job 最长路
- HDU 2544
- Timus 1557 Network Attack DFS+各种各种...
- HDU1289 Tarjan-模板题
- Poj2638 网络流+最短路+二分答案
- Aizu1311 分层图最短路 (...大概)
- HDU 3631 Shortest Path
- 二分图匹配模板
- 单源最短路深度分析
- Six Degrees of Cowvin Bacon(最短路径floyd算法)
- Edmonds-Karp 最大流 hdu 1532 Drained Ditches
- dijkstra_最短路径_hdu_3790
- dfs_poj_2531
- hdu_1175
- poj_1135_最短路径&枚举
- 最小生成树_hdu_1863
- Dijkstra算法
- 图论算法 有图有代码 万字总结 向前辈致敬