n個點的無向帶權圖,求1->n的最短往返路徑,不走重復邊。
這裡涉及到一個知識點:求無向圖上s->t的最短路,其實就是費用流。
而求1->n最短往返路徑呢?增加源點s,由s到1加弧,容量為2(往返兩次),費用為0;而對於原圖中的邊<u, v>,分別由u到v,由v到u增加容量為1(往返不能走重邊),費用為邊權的弧。然後跑費用流得到的最小費用便是答案。如果最後求得的最大流小於2,則說明無解。
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<fstream> #include<sstream> #include<bitset> #include<vector> #include<string> #include<cstdio> #include<cmath> #include<stack> #include<queue> #include<stack> #include<map> #include<set> #define FF(i, a, b) for(int i=a; i<b; i++) #define FD(i, a, b) for(int i=a; i>=b; i--) #define REP(i, n) for(int i=0; i<n; i++) #define CLR(a, b) memset(a, b, sizeof(a)) #define debug puts("**debug**") #define LL long long #define PB push_back using namespace std; const int maxn = 111; const int INF = 1e9; int n, m, s, t, d[maxn], p[maxn], a[maxn], inq[maxn]; int flow, cost; struct Edge { int from, to, cap, flow, cost; }; vector<Edge> edges; vector<int> G[maxn]; inline void init() { flow = cost = s = 0, t = n; REP(i, t+1) G[i].clear(); edges.clear(); } void add(int from, int to, int cap, int cost) { edges.PB((Edge){from, to, cap, 0, cost}); edges.PB((Edge){to, from, 0, 0, -cost}); int nc = edges.size(); G[from].PB(nc-2); G[to].PB(nc-1); } bool spfa(int& flow, int& cost) { REP(i, t+1) d[i] = INF; CLR(inq, 0); d[s] = 0, inq[s] = 1, p[s] = 0, a[s] = INF; queue<int> q; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; int nc = G[u].size(); REP(i, nc) { Edge& e = edges[G[u][i]]; if(e.cap > e.flow && d[e.to] > d[u] + e.cost) { d[e.to] = d[u] + e.cost; p[e.to] = G[u][i]; a[e.to] = min(a[u], e.cap - e.flow); if(!inq[e.to]) q.push(e.to), inq[e.to] = 1; } } } if(d[t] == INF) return false; flow += a[t], cost += d[t] * a[t]; int u = t; while(u != s) { edges[p[u]].flow += a[t]; edges[p[u]^1].flow -= a[t]; u = edges[p[u]].from; } return true; } int main() { while(scanf("%d", &n), n) { scanf("%d", &m); init(); int a, b, c; add(s, 1, 2, 0); while(m--) { scanf("%d%d%d", &a, &b, &c); add(a, b, 1, c); add(b, a, 1, c); } while(spfa(flow, cost)); if(flow < 2) puts("Back to jail"); else printf("%d\n", cost); } return 0; }