Dexter: "You don't understand. I can't walk... they've tied my shoelaces together." Topper Harley: "A knot. Bastards!" |
You are a political prisoner in jail. Things are looking grim, but fortunately, your jailmate has come up with an escape plan. He has found a way for both of you to get out of the cell and run through the city to the train station, where you will leave the country. Your friend will escape first and run along the streets of the city to the train station. He will then call you from there on your cellphone (which somebody smuggled in to you inside a cake), and you will start to run to the same train station. When you meet your friend there, you will both board a train and be on your way to freedom.
Your friend will be running along the streets during the day, wearing his jail clothes, so people will notice. This is why you can not follow any of the same streets that your friend follows - the authorities may be waiting for you there. You have to pick a completely different path (although you may run across the same intersections as your friend).
What is the earliest time at which you and your friend can board a train?
Problem, in short
Given a weighed, undirected graph, find the shortest path from S toT and back without using the same edge twice.
Input
The input will contain several test cases. Each test case will begin with an integern (2<=n<=100) - the number of nodes (intersections). The jail is at node number 1, and the train station is at node numbern. The next line will contain an integer m - the number of streets. The nextm lines will describe the m streets. Each line will contain 3 integers - the two nodes connected by the street and the time it takes to run the length of the street (in seconds). No street will be longer than 1000 or shorter than 1. Each street will connect two different nodes. No pair of nodes will be directly connected by more than one street. The last test case will be followed by a line containing zero.
Output
For each test case, output a single integer on a line by itself - the number of seconds you and your friend need between the time he leaves the jail cell and the time both of you board the train. (Assume that you do not need to wait for the train - they leave every second.) If there is no solution, print "Back to jail".
Sample Input | Sample Output |
2 1 1 2 999 3 3 1 3 10 2 1 20 3 2 50 9 12 1 2 10 1 3 10 1 4 10 2 5 10 3 5 10 4 5 10 5 7 10 6 7 10 7 8 10 6 9 10 7 9 10 8 9 10 0 |
Back to jail 80 Back to jail |
自己yy出来的第一道网络流题目,虽然基础,但感觉还不错,呵呵
这到题是对边的访问次数进行限制,所以我考虑把边也设为一个点,然后连向这条边的两个端点,容量都为1,cost只放在一条边上
然后,题目中是要求从起点到终点再回起点,就相当于从终点选两条路径到起点,那么源点就是原来的终点,汇点就是原来的起点,流量就是符合条件的路径数。所以,在流量为2时break,如果最大流小于2,则说明答案不存在
#include<cstdio> #include<cstring> #include<queue> #include<vector> #include<algorithm> using namespace std; const int maxn = 10000; const int INF = 1000000000; struct Edge { int from, to, cap, flow, cost; }; struct MCMF { int n, m, s, t; vector<Edge> edges; vector<int> G[maxn]; int inq[maxn]; // 是否在队列中 int d[maxn]; // Bellman-Ford int p[maxn]; // 上一条弧 int a[maxn]; // 可改进量 void init(int n) { this->n = n; for(int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int cap, int cost) { edges.push_back((Edge){from, to, cap, 0, cost}); edges.push_back((Edge){to, from, 0, 0, -cost}); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BellmanFord(int s, int t, int &flow,int &cost) { for(int i = 0; i < n; i++) d[i] = INF; memset(inq, 0, sizeof(inq)); 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; for(int i = 0; i < G[u].size(); i++) { 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;//s-t不连通,失败退出 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 Mincost(int &can,int s, int t) { int cost = 0,flow = 0; while(BellmanFord(s, t,flow, cost)){ if(flow == 2) break; } if(flow == 2) can = 1; return cost; } }; MCMF g; int main(){ int n,m; while(scanf("%d",&n)){ if(n == 0) break; scanf("%d",&m); g.init(n+m); int sorce = n-1,sink = 0; for(int i = 1;i <= m;i++){ int from,to,cost; scanf("%d%d%d",&from,&to,&cost); g.AddEdge(from-1,i+n-1,1,cost); g.AddEdge(i+n-1,from-1,1,cost); g.AddEdge(i+n-1,to-1,1,0); g.AddEdge(to-1,i+n-1,1,0); } int can = 0; int ans = g.Mincost(can,sorce,sink); if(!can) printf("Back to jail\n"); else printf("%d\n",ans); } return 0; }