什么是Floyd?就是考虑最坏的情况遍历n次找出与Dijkstra思想类似的情况:如果P(i,j)={Vi....Vk....Vj}是从顶点i到j的最短路径,k是这条路径上的一个中间顶点,那么P(k,j)必定是从k到s的最短路径。
题:
POJ1125 其实这道题数据是有缺陷的,因为没有判断是否所有人都传到了消息。
#include<cstdio>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
#define M 210
int map[M][M];
void Floyd(int n)
{
for(int k = 1;k <= n; k++)
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
map[i][j] = min(map[i][j],map[i][k] + map[k][j]);
}
int main()
{
int n;
while(scanf("%d",&n),n){
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
map[i][j] = INF;
for(int i = 1;i <= n; i++){
int n1;
scanf("%d",&n1);
int p,t;
for(int j = 1;j <= n1; j++){
scanf("%d%d",&p,&t);
map[i][p] = t;
}
}
Floyd(n);
int ans = INF,coor;
for(int i = 1;i <= n; i++){
int temp = 0;
for(int j = 1;j <= n; j++)
if(i != j && map[i][j] > temp) //找出最小的时间
temp = map[i][j];
if(ans > temp){
ans = temp;
coor = i;
}
}
printf("%d %d\n",coor,ans);
}
return 0;
}
题:POJ2240
题意:输入每一种钱转化率,判断是否有哪一种经过兑换能增值。
#include<cstdio>
#include<cstring>
#define M 210
char str[M][M];
double map[M][M];
int n,m;
int change(char a[])
{
for(int i = 1;i <= n; i++)
if(strcmp(a,str[i]) == 0)
return i;
}
void Floyd()
{
for(int k = 1;k <= n; k++)
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
if(map[i][j] < map[i][k]*map[k][j]) //万变不离其宗
map[i][j] = map[i][k]*map[k][j];
}
int main()
{
// freopen("input.txt","r",stdin);
int nCase = 1;
while(scanf("%d",&n),n){
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
if(i != j)
map[i][j] = 0;
else
map[i][j] = 1;
for(int i = 1;i <= n; i++)
scanf("%s",str[i]);
scanf("%d",&m);
for(int i = 1;i <= m; i++){
char s1[M],s2[M];
double rate;
scanf("%s%lf%s",s1,&rate,s2);
map[change(s1)][change(s2)] = rate;
}
Floyd();
int flat = 0;
for(int i = 1;i <= n; i++)
if(map[i][i] > 1)
flat = 1;
if(flat)
printf("Case %d: Yes\n",nCase++);
else
printf("Case %d: No\n",nCase++);
}
return 0;
}