2*spfa.
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #define maxv 1050 #define maxe 200500 #define inf 2000000000 using namespace std; queue <int> q; int n,m,s,x,y,z,g[maxv],nume=0,dis[maxv][3]; struct edge { int v,w,nxt; }e[maxe]; bool vis[maxv]; void addedge(int u,int v,int w) { e[++nume].v=v; e[nume].w=w; e[nume].nxt=g[u]; g[u]=nume; } void spfa(int type) { for (int i=1;i<=n;i++) { dis[i][type]=inf; vis[i]=false; } dis[s][type]=0;vis[s]=true;q.push(s); while (!q.empty()) { int head=q.front();q.pop(); for (int i=g[head];i;i=e[i].nxt) { int v=e[i].v; if ((i%2==type) && (dis[v][type]>dis[head][type]+e[i].w)) { dis[v][type]=dis[head][type]+e[i].w; if (!vis[v]) {vis[v]=true;q.push(v);} } } vis[head]=false; } } int main() { scanf("%d%d%d",&n,&m,&s); for (int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&z); addedge(x,y,z);addedge(y,x,z); } spfa(0); spfa(1); int ans=0; for (int i=1;i<=n;i++) ans=max(ans,dis[i][0]+dis[i][1]); printf("%d\n",ans); return 0; }
时间: 2024-10-14 10:51:24