zoj 1092 || poj 2240 Arbitrage(Floyd)

CW说用DIJ能做出来,我死活做不出来。。。就用了广大同胞用的Floyd,现学现卖,就三个循环,理解了下。

 

引用下网上某位大侠的一段代码

 

For k←1 to n do // k为“媒介节点” For i←1 to n do For j←1 to n do if (dist(i,k) + dist(k,j) < dist(i,j)) then // 是否是更短的路径? dist(i,j) = dist(i,k) + dist(k,j)

 

意思就是,把 i 和 j 的距离缩短到最小。。。就像一个三角形(不过不符合两边之和大于第三边罢了),寻找一个顶点,更新i 和 j的距离,如果这两边之和比原来的小,更新之,反之,不更新。就这样,k取1到n,即所有的点都试过一遍,留下的就是i 和 j 距离最小的。

 

 

恩,这道题是找最大的,好神奇的算法。。。

 

这个题的意思是根据input给的汇率,计算,某种货币经过N次(N>=0)转换后,是否能赚。。。这个就是炒货币吧。。。

 

 

#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <string.h> #include <math.h> int main(void) { int n,m,x,y,i,j,k; char name[32][100],temp1[100],temp2[100]; double map[32][32],price; int success,flag,count = 1; while( scanf("%d",&n)!=EOF && n ) { for(i=1; i<=n; i++) scanf("%s",name[i]); scanf("%d",&m); success = 0; for(i=1; i<=n; i++) { for(j=1; j<=n; j++) map[i][j] = 0.0; map[i][i] = 1.0; } while(m--) { flag = 0; scanf("%s %lf %s",temp1,&price,temp2); for(i=1; i<=n && flag!=2; i++) { if( strcmp(temp1,name[i]) == 0 ) { x = i; flag++; } if( strcmp(temp2,name[i]) == 0 ) { y = i; flag++; } } map[x][y] = price; } for(k=1; k<=n; k++) for(i=1; i<=n; i++) for(j=1; j<=n; j++) if( map[i][j] < map[i][k] * map[k][j] ) map[i][j] = map[i][k] * map[k][j]; for(k=1; k<=n; k++) if( map[k][k] > 1 ) { success = 1; break; } if( success == 0 ) { printf("Case %d: No/n",count); count++; } else { printf("Case %d: Yes/n",count); count++; } } system("pause"); return 0; }

你可能感兴趣的:(算法,System,input)