POJ 2240 Floyd

來源:互聯網
上載者:User

一道有趣的題目,zzy大神告訴我的用floyd... 基本上沒想什麼map現在用得比較熟練了,想起昨天的福州的三國殺,寫的我想吐血啊!

題目就是求匯率是否能形成一個環,使得利滾利,錢生錢。這樣只要把初始結點對自己的匯率調整為1.0,對其他的是0,這樣來用Floyd就可以解決啦~

#include<iostream>#include<map>#include<string>#define MAXN 31using namespace std;int main(){    map< string,int >myMap;    double G[MAXN][MAXN];    int n,m;    int T=1;    while( cin>>n )    {           if( !n ) break;           int i,j,k;           char nationA[101],nationB[101];           double rate;           for( i=1;i<=n;i++ )           {                cin>>nationA;                myMap[nationA]=i;                for( j=1;j<=n;j++ ) G[i][j]=(i==j)?1.0:0;            }           cin>>m;           for( i=1;i<=m;i++ )           {                cin>>nationA>>rate>>nationB;                G[myMap[nationA]][myMap[nationB]]=rate;           }           for( k=1;k<=n;k++ )           for( i=1;i<=n;i++ )           for( j=1;j<=n;j++ )                if( G[i][j]<G[i][k]*G[k][j] )                    G[i][j]=G[i][k]*G[k][j];                               bool found=false;           for( i=1;i<=n;i++ )                if( G[i][i]>1.0 )                {                    found=true;break;                    }           if( found )               printf( "Case %d: Yes\n",T++ );           else               printf( "Case %d: No\n",T++ );               }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.