spfa裸题
跑两遍spfa
。
#ifdef Dubug
#endif
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
inline LL In() { LL res(0),f(1); register char c ;
while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : 0 ;
while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ;
return res * f ;
}
int n , m , s ;
const int N = 1000 + 5 ;
const int M = 100000 + 5 ;
const int Inf = INT_MAX ;
struct node {
int v,w;
int nxt ;
};
int cnt(0) ;
node edge[M<<1] ;
int head[N] ;
int dis[N] ;
inline void Add(int u,int v,int w) {
edge[++cnt].v = v ;
edge[cnt].w = w ;
edge[cnt].nxt = head[u] ;
head[u] = cnt ;
return ;
}
int cnt2(0) ;
node edge2[M<<1] ;
int head2[N] ;
int dis2[N] ;
inline void _Add(int u,int v,int w) {
edge2[++cnt2].v = v ;
edge2[cnt2].w = w ;
edge2[cnt2].nxt = head2[u] ;
head2[u] = cnt2 ;
return ;
}
bool vis[N] ;
queue< int > q;
inline void Spfa(){
for(register int i=1;i<=n;i++) dis[i] = Inf , vis[i] = 0 ;
q.push(s) , dis[s] = 0 , vis[s] = 1 ;
while(!q.empty()){
int u=q.front() ;
q.pop() , vis[u] = 0 ;
for(register int i=head[u];i;i=edge[i].nxt){
int v = edge[i].v;
if(dis[v]>dis[u]+edge[i].w){
dis[v] = dis[u] + edge[i].w;
if(!vis[v]) vis[v] = 1 , q.push(v) ;
}
}
}
return ;
}
inline void Spfa2(){
for(register int i=1;i<=n;i++) dis2[i] = Inf , vis[i] = 0 ;
q.push(s) , dis2[s] = 0 , vis[s] = 1 ;
while(!q.empty()){
int u=q.front() ;
q.pop() , vis[u] = 0 ;
for(register int i=head2[u];i;i=edge2[i].nxt){
int v = edge2[i].v;
if(dis2[v]>dis2[u]+edge2[i].w){
dis2[v] = dis2[u] + edge2[i].w;
if(!vis[v]) vis[v] = 1 , q.push(v) ;
}
}
}
return ;
}
signed main() {
n = In() ; m = In() ; s = In() ;
for(register int i=1;i<=m;i++) {
int u , v , w ;
u = In() , v = In() , w = In() ;
Add(u,v,w) , _Add(v,u,w) ;
}
Spfa() , Spfa2() ;
int Max = -Inf ;
for(register int i=1;i<=n;i++) Max = max(Max,dis[i]+dis2[i]) ;
cout << Max << endl ;
return 0 ;
}