USACO07FEB银牛派对
【题意】n个点m条有向边,求单源最短路最长往返距离
【解题】建两个图
跑两遍spfa
不过有很多细节需要注意
#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); vis[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]){ vis[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; }
满堂花醉三千客,一剑霜寒十四州