思路:正向建边,一遍Dijkstra,反向建边,再一遍Dijkstra。ans加在一起输出最大值。
(SPFA也行……)
// by SiriusRen
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1005
int n,m,X,tot=0,maxx=0,first[N],v[N*N],w[N*N],next[N*N],xx[N*N],yy[N*N],zz[N*N],d[N],ans[N];
struct node{int weight,now;}jy;
void add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
bool operator < (node a,node b){return a.weight>b.weight;}
void Dijkstra(int x){
priority_queue<node>pq;
bool vis[N];memset(vis,0,sizeof(vis)),memset(d,0x3f,sizeof(d));
jy.weight=0,jy.now=x,d[x]=0;
pq.push(jy);
while(!pq.empty()){
node temp=pq.top();pq.pop();
if(vis[temp.now])continue;
vis[temp.now]=1;
for(int i=first[temp.now];~i;i=next[i])
if(!vis[v[i]]&&d[v[i]]>d[temp.now]+w[i]){
d[v[i]]=d[temp.now]+w[i];
jy.now=v[i];
jy.weight=temp.weight+w[i];
pq.push(jy);
}
}
for(int i=1;i<=n;i++)ans[i]+=d[i];
}
void spfa(int x){
bool vis[N];memset(vis,0,sizeof(vis)),memset(d,0x3f,sizeof(d));
queue<int>q;
vis[x]=1,d[x]=0,q.push(x);
while(!q.empty()){
int temp=q.front();q.pop(),vis[temp]=0;
for(int i=first[temp];~i;i=next[i])
if(d[temp]+w[i]<d[v[i]]){
d[v[i]]=d[temp]+w[i];
if(!vis[v[i]])q.push(v[i]),vis[v[i]]=1;
}
}
for(int i=1;i<=n;i++)ans[i]+=d[i];
}
int main()
{
scanf("%d%d%d",&n,&m,&X);
memset(first,-1,sizeof(first));
for(int i=1;i<=m;i++)scanf("%d%d%d",&xx[i],&yy[i],&zz[i]),add(xx[i],yy[i],zz[i]);
Dijkstra(X);
memset(first,-1,sizeof(first)),tot=0;
for(int i=1;i<=m;i++)add(yy[i],xx[i],zz[i]);
Dijkstra(X);
for(int i=1;i<=n;i++)maxx=max(maxx,ans[i]);
printf("%d",maxx);
}