您的位置:首页 > 其它

【最短路】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的排名可以确定。

#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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图论 最短路