1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include<cstdio> #include<iostream>
#define MAXN 1005 #define MAX_INT 2147483647
using namespace std;
int gra_in[MAXN][MAXN],gra_out[MAXN][MAXN],n,m,p,dist_in[MAXN],dist_out[MAXN];
void init() { cin>>n>>m>>p; for(int i = 1;i <= m;i ++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); gra_out[a][b] = c; gra_in[b][a] = c; } }
void dijkstra(int gra[MAXN][MAXN] , int dist[MAXN]) { bool mark[MAXN] = {false}; for(int i = 1;i <= n;i ++) dist[i] = MAX_INT; dist[p] = 0; for(int i = 1;i <= n;i ++) { int Min = MAX_INT,tj; for(int j = 1;j <= n;j ++) { if(mark[j]) continue; if(Min > dist[j]) { Min = dist[j]; tj = j; } } mark[tj] = true; for(int j = 1;j <= n;j ++) { if(mark[j] || !gra[tj][j]) continue; dist[j] = min(dist[j] , dist[tj] + gra[tj][j]); } } }
int main() { init(); dijkstra(gra_in , dist_in); dijkstra(gra_out , dist_out); int Max = 0; for(int i = 1;i <= n;i ++) Max = max(Max , dist_in[i] + dist_out[i]); cout<<Max<<endl; return 0; }
|