有向图,每个点到终点,然后从不同的路径回来。
最短路跑一次是源点到各个点的距离,把边反向再跑一次,就是各个点到源点的距离,两者相加取最大值即可。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define f first
#define s second
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const int N = 1e3 + 10;
const int M = 1e5 + 10;
int n,m,x,a,b,c,ans,d[N];
int mp[N][N],tmp[N];
void dij(){
memset(d,10,sizeof(d));
d[x] = 0 ;
priority_queue<PII> q;
q.push(make_pair(0,x));
while(!q.empty()){
int u = q.top().s;
q.pop();
for(int i = 1 ; i <= n; i++){
int di = mp[u][i];
if(!di) continue;
if(d[i] > d[u] + di){
d[i] = d[u] + di;
q.push(make_pair(-d[i],i));
}
}
}
for(int i = 1; i <=n ;i ++) tmp[i] += d[i];
}
int main(){
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d%d%d",&n,&m,&x);
for(int i = 0 ; i < m;i++){
scanf("%d%d%d",&a,&b,&c);
mp[a][b] = c;
}
dij();
for(int i = 1; i <= n; i++)
for(int j = i + 1; j <= n; j++)
swap(mp[i][j] , mp[j][i]);
dij();
for(int i = 1; i <=n; i ++) ans = max(ans, tmp[i]);
printf("%d\n",ans);
return 0;
}