hdu1217 floyd

来源:互联网 发布:mac如何打人民币符号 编辑:程序博客网 时间:2024/06/14 07:41

1217 floyd算法 不过算的是相乘的最大值

写完后一直在纠结对不对,因为如果出现ex[i][k] ex[k][j]均为-1时,相乘即为1这样就乱套,但是交了之后过了,  说明测试数据中,并没有给出孤立的国家

如果国家间利率没有联系,则赋为-1

贴上代码:

#include <cstdio>#include <iostream>#include <map>#include <string>#include <cstring>using namespace std;const int C_MAX = 35;double ex[C_MAX][C_MAX];//ex[u][v]表示u对v的汇率 u-->vmap<string, int> m;int C,M;bool floyd(){    for(int k=1; k<=C; k++)    {        for(int i=1; i<=C; i++)        {            for(int j=1; j<=C; j++)            {                    ex[i][j] = max(ex[i][j],ex[i][k] * ex[k][j]);            }        }    }        for (int i=1; i<=C; i++)    {        if(ex[i][i]>1)return true;    }    return false;}int main(){    int Case = 1;    while((scanf("%d",&C)!=EOF)&&C){        for(int i=1;i<=C; i++){            for(int j=1; j<=C; j++){                    if(i==j)ex[i][j] = 1;                    else{ex[i][j] = -1.0;                }            }        }        string temp;        int fir, sec;        double exc;        for(int i=1; i<=C; i++)        {            cin >> temp;            m[temp] = i;        }        scanf("%d",&M);        for(int i=0; i<M; i++)        {            cin >> temp;            fir = m[temp];            scanf("%lf",&exc);            cin >> temp;            sec = m[temp];            ex[fir][sec] = exc;        }        if(floyd())printf("Case %d: Yes\n", Case);        else printf("Case %d: No\n",Case);        Case++;    }    return 0;}


0 0