[N] that Italy and m points directed edge, seeking a single source shortest longest round trip distance
[Solving] build two graphs
Ran twice spfa
However, there are many details that need attention
#include<bits/stdc++.h> const int N = 1005; struct node{ int v,w; }; std::vector<node>g[3][N]; std::queue<int>q; int n,k,m,x,y,z,hs,vis[N],f[3][N],ans = -0x3f3f; void spfa(int k){ memset(vis,0,sizeof vis); to [HS] = 1 ; f[k][hs] = 0; q.push(hs); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; //std::cout << "DEBUG!"<<g[k][u].size(); for(int i = 0;i < g[k][u].size();++i){ int v = g[k][u][i].v, w = g[k][u][i].w; if(f[k][v] > f[k][u] + w){ f[k][v] = f[k][u] + w; if(!vis[v]){ to [v] = 1 ; q.push(v); } } } } } int main () { scanf("%d%d%d",&n,&m,&hs); memset(f,0x3f3f3f,sizeof(f)); for(register int i = 1; i <= m; ++i){ scanf("%d%d%d",&x,&y,&z); g[1][x].push_back((node){y,z}); g[2][y].push_back((node){x,z}); } spfa(1); spfa(2); for(register int i = 1; i <= n; ++i){ if(i == hs)continue; ans = std::max(ans,f[1][i] + f[2][i]); } printf("%d",ans); return 0; }