UVa 10806 Dijkstra, Dijkstra. 费用流

来源:互联网 发布:linux 新建脚本 编辑:程序博客网 时间:2024/04/29 21:05

10806 - Dijkstra, Dijkstra.

Time limit: 3.000 seconds
Dexter: "You don't understand. I can't walk...
they've tied my shoelaces together."

Topper Harley: "A knot. Bastards!"
Jim Abrahams and Pat Proft,
"Hot Shots! Part Deux."

You are a political prisoner in jail. Things are looking grim, butfortunately, your jailmate has come up with an escape plan. He hasfound a way for both of you to get out of the cell and run throughthe city to the train station, where you will leave the country.Your friend will escape first and run along the streets of the cityto the train station. He will then call you from there on yourcellphone (which somebody smuggled in to you inside a cake), and youwill start to run to the same train station. When you meet yourfriend 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, wearinghis jail clothes, so people will notice. This is why you can notfollow any of the same streets that your friend follows - theauthorities may be waiting for you there. You have to pick a completely different path (although you may run across the sameintersections as your friend).

What is the earliest time at which you and your friend can boarda train?

Problem, in short
Given a weighed, undirected graph, find the shortest path from StoT and back without using the same edge twice.

Input
The input will contain several test cases. Each test case will beginwith 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 runthe length of the street (in seconds). No street will be longer than1000 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 heleaves 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 everysecond.) If there is no solution, print "Back to jail".

Sample InputSample Output

211 2 999331 3 102 1 203 2 509121 2 101 3 101 4 102 5 103 5 104 5 105 7 106 7 107 8 106 9 107 9 108 9 100
Back to jail80Back to jail

题目链接:UVa 10806 - Dijkstra, Dijkstra.

题目大意:n 个点,其中1为起点,n为终点;m条无向带权边,问是否能找出两条不重边的路径使得权值和最小。

题目分析:裸的费用流,建立超级源点,建边(s,1,2,0),对每一条边建边(u,v,1,w),(v,u,1,w)。最后跑一次最小费用最大流,如果flow == 2 输出cost,否则输出无解。

#include <stdio.h>#include <string.h>#include <algorithm>#define REP(I, X) for(int I = 0; I < X; ++I)#define FF(I, A, B) for(int I = A; I <= B; ++I)#define clear(A, B, SIZE) memset(A, B, sizeof(A[0]) * (SIZE + 1))#define copy(A, B, SIZE) memcpy(A, B, sizeof(A[0]) * (SIZE + 1))#define min(A, B) ((A) < (B) ? (A) : (B))#define max(A, B) ((A) > (B) ? (A) : (B))using namespace std;typedef long long ll;typedef long long LL;const int oo = 0x3f3f3f3f;const int maxE = 1000000;const int maxN = 105;const int maxQ = 10000;struct Edge{    int v, c, w, n;};Edge edge[maxE];int adj[maxN], cntE;int d[maxN], cur[maxN], a[maxN];int inq[maxN], Q[maxE], head, tail;int cost, flow, s, t;int n, m;void addedge(int u, int v, int c, int w){    edge[cntE].v = v; edge[cntE].c = c; edge[cntE].w =  w; edge[cntE].n = adj[u]; adj[u] = cntE++;    edge[cntE].v = u; edge[cntE].c = 0; edge[cntE].w = -w; edge[cntE].n = adj[v]; adj[v] = cntE++;}int SPFA(){    memset(d, oo, sizeof d);    memset(inq, 0, sizeof inq);    head = tail = 0;    d[s] = 0;    a[s] = oo;    cur[s] = -1;    Q[tail++] = s;    while(head != tail){        int u =  Q[head++];        inq[u] = 0;        for(int i = adj[u]; ~i; i = edge[i].n){            int v = edge[i].v;            if(edge[i].c && d[v] > d[u] + edge[i].w){                d[v] = d[u] + edge[i].w;                cur[v] = i;                a[v] = min(edge[i].c, a[u]);                if(!inq[v]){                    inq[v] = 1;                    Q[tail++] = v;                }            }        }    }    if(d[t] == oo) return 0;    flow += a[t];    cost += a[t] * d[t];    for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){        edge[i].c -= a[t];        edge[i ^ 1].c += a[t];    }    return 1;}int MCMF(){    flow = cost = 0;    while(SPFA());    return flow;}int read(){    int flag = 0, x = 0;    char ch = ' ';    while(ch != '-' && (ch > '9' || ch < '0')) ch = getchar();    if(ch == '-') flag = 1, ch = getchar();    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();    return flag ? -x : x;}void work(){    int u, v, w;    clear(adj, -1, n + 1);    cntE = 0;    m = read();    s = 0; t = n;    addedge(s, 1, 2, 0);    while(m--){        u = read(); v = read(); w = read();        addedge(u, v, 1, w);        addedge(v, u, 1, w);    }    MCMF();    if(flow == 2) printf("%d\n", cost);    else printf("Back to jail\n");}int main(){    while(~scanf("%d", &n) && n) work();    return 0;}


0 0
原创粉丝点击