UVa 10806 Dijkstra, Dijkstra. 费用流

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 Sto T and back without using the same edge twice.

Input
The input will contain several test cases. Each test case will beginwith an integer n (2<=n<=100) - the number of nodes (intersections). The jail is at node number 1, and the train station is at node number n. The next line will contain an integer m - the number of streets. The next m 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
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

题目链接: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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值