首页 > 代码库 > POJ3268 Silver Cow Party
POJ3268 Silver Cow Party
PS: 对原图和反图求最短路,然后求最长的即可。省赛热身练习。
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <cstring> using namespace std; const int maxn = 1010; const int INF = 0x3ffffff; //Accepted 468K 63MS struct edges { int from, to, w; }; int n, m, s; int d1[maxn]; int d2[maxn]; queue<int> que; bool inque[maxn]; void spfa(vector<edges> G[], int d[]) { while(!que.empty()) que.pop(); memset(inque, false, sizeof(inque)); for(int i = 1; i <= n; i++) d[i] = INF; d[s] = 0; inque[s] = true; que.push(s); while(!que.empty()) { int u = que.front(); que.pop(); for(int i = 0; i < (int)G[u].size(); i++) { edges v = G[u][i]; if(d[u]!=INF && d[u]+v.w < d[v.to]) { d[v.to] = d[u] + v.w; if(!inque[v.to]) { inque[v.to] = true; que.push(v.to); } } } inque[u] = false; } } int work() { int ans = -INF; for(int i = 1; i <= n; i++) { if(d1[i]+d2[i]>ans) { ans = d1[i] + d2[i]; } } return ans; } vector<edges> G[maxn]; vector<edges> RG[maxn]; int main() { edges t; int from, to, w; while(scanf("%d%d%d", &n, &m, &s)!=EOF) { for(int i = 1; i <= m; i++) { scanf("%d%d%d", &from, &to, &w); t.from = from; t.to = to; t.w = w; G[from].push_back(t); t.from = to; t.to = from; RG[t.from].push_back(t); } spfa(G, d1); spfa(RG, d2); int res = work(); printf("%d\n", res); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。