POJ - 3268 【改变边方向 + dijk】
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Line 1: Three space-separated integers, respectively:
N, M, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3Sample Output
10
题意:
一个奶牛的最短路为 (i -> X + X -> i)。奶牛每次都走最短路,求所有奶牛的最短路的最大值。
题解:
用堆优化的dijk跑 (n+1)次最短路可以过。。。。。正解是:求一次 x -> i的最短路,然后建立反向边的图,再跑一次 x -> i的最短路。然后直接更新ans即可。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <bitset> #include <vector> #include <queue> #include <stack> #include <cmath> #include <list> #include <set> #include <map> #define rep(i,a,b) for(int i = a;i <= b;++ i) #define per(i,a,b) for(int i = a;i >= b;-- i) #define mem(a,b) memset((a),(b),sizeof((a))) #define FIN freopen("in.txt","r",stdin) #define FOUT freopen("out.txt","w",stdout) #define IO ios_base::sync_with_stdio(0),cin.tie(0) #define mid ((l+r)>>1) #define ls (id<<1) #define rs ((id<<1)|1) #define N 1005 #define INF 0x3f3f3f3f #define INFF ((1LL<<62)-1) typedef long long LL; using namespace std; int n, m, sx, u, v, x, dis[N], X[N]; bool vis[N]; vector < pair<int, int> > G[N]; struct Node{ int id,val; Node(int _id, int _val) { id = _id, val = _val; } bool operator < (const Node &r) const{ return val > r.val; } }; void dijk(int x){ mem(dis, INF); mem(vis, false); priority_queue <Node> Q; dis[x] = 0; Q.push(Node(x, 0)); while(!Q.empty()){ Node h = Q.top(); Q.pop(); int u = h.id; if(vis[u]) continue; vis[u] = true; rep(i, 0, (int)G[u].size()-1){ int v = G[u][i].first; int c = G[u][i].second; if(!vis[v] && dis[u]+c < dis[v]){ dis[v] = dis[u]+c; Q.push(Node(v, dis[v])); } } } } int main() {IO; //FIN; while(cin >> n >> m >> sx){ rep(i, 0, n) G[i].clear(); rep(i, 1, m){ cin >> u >> v >> x; G[u].push_back(make_pair(v, x)); //G[v].push_back(make_pair(u, x)); } int ans = 0; dijk(sx); rep(i, 0, n) X[i] = dis[i]; rep(i, 1, n){ dijk(i); if(dis[sx]!= INF); ans = max(ans, X[i] + dis[sx]); } cout << ans << endl; } return 0; }