竹園論壇

標題: 63 - D.網子把戲 [打印本頁]

作者: domen111    時間: 2014-7-17 17:00
標題: 63 - D.網子把戲
http://toj.tfcis.org/oj/pro/63/

用Floyd-Warshall演算法就可解決,求出所有點的最短路徑,然後最遠的兩點的距離就是答案。
這題要注意的是: 兩顆珠子之間可能會有多條繩子,必須取min才會AC  (LFsWang的題目總是有陷阱啊! 我又掉進去了)

[C++] 純文本查看 復制代碼
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int d[100][100];
int main()
{
        int T;
        cin>>T;
        int v,e;
        while(T--)
        {
                cin>>v>>e;
                for(int i=0;i<v;i++)
                        for(int j=0;j<v;j++)
                                d[j]=1e7;
                for(int i=0;i<e;i++)
                {
                        int a,b,c;
                        cin>>a>>b>>c;
                        d[a]=d[a]=min(d[a],c); //陷阱
                }
                for(int i=0;i<v;i++)
                        d=0;
                for(int i=0;i<v;i++)
                        for(int j=0;j<v;j++)
                                for(int k=0;k<v;k++)
                                        d[j][k]=min(d[j][k],d[j]+d[k]);
                int ans=0;
                for(int i=0;i<v;i++)
                        for(int j=0;j<v;j++)
                                ans=max(ans,d[j]);
                cout<<ans<<endl;
        }
}





歡迎光臨 竹園論壇 (http://forum.tfcis.org/) Powered by Discuz! X3.2