Descriptionsios
給出n個點和m條邊,接着是m條邊,表明從牛a到牛b須要花費c時間,如今全部牛要到牛x那裏去參加聚會,而且全部牛參加聚會後還要回來,給你牛x,除了牛x以外的牛,他們都有一個參加聚會而且回來的最短期,從這些最短期裏找出一個最大值輸出算法
Inputspa
Output.net
Sample Inputcode
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Outputblog
10
Hintip
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string>1 #include <cstring> #include <map> #include <stack> #include <set> #include <sstream> #define IOS ios_base::sync_with_stdio(0); cin.tie(0) #define Mod 1000000007 #define eps 1e-6 #define ll long long #define INF 0x3f3f3f3f #define MEM(x,y) memset(x,y,sizeof(x)) #define Maxn 1000+5 #define P pair<int,int>//first最短路徑second頂點編號 using namespace std; int N,M,X; struct edge { int to,cost; edge(int to,int cost):to(to),cost(cost){} }; vector<edge>G[Maxn];//G[i] 從i到G[i].to的距離爲cost int d[Maxn][Maxn];//d[i][j]從i到j的最短距離 void Dijk(int s) { priority_queue<P,vector<P>,greater<P> >q;//按first從小到大出隊 for(int i=0;i<=N;i++) d[s][i]=INF; d[s][s]=0; q.push(P(0,s)); while(!q.empty()) { P p=q.top(); q.pop(); int v=p.second;//點v if(d[s][v]<p.first) continue; for(int i=0;i<G[v].size();i++) { edge e=G[v][i];//枚舉與v相鄰的點 if(d[s][e.to]>d[s][v]+e.cost) { d[s][e.to]=d[s][v]+e.cost; q.push(P(d[s][e.to],e.to)); } } } } int main() { IOS; cin>>N>>M>>X; for(int i=0; i<M; i++) { int x,y,z; cin>>x>>y>>z; G[x].push_back(edge(y,z)); } for(int i=1;i<=N;i++)//枚舉全部兩點間的最短距離 Dijk(i); int ans=0; for(int i=1;i<=N;i++) { if(i==X) continue; ans=max(ans,d[i][X]+d[X][i]); } cout<<ans<<endl; return 0; }