USACO07FEB银牛派对
Posted phemiku
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; }
以上是关于USACO07FEB银牛派对的主要内容,如果未能解决你的问题,请参考以下文章
Luogu P1821 [USACO07FEB]银牛派对Silver Cow Party
[USACO07FEB]银牛派对Silver Cow Party
P1821 [USACO07FEB]银牛派对Silver Cow Party