UVa 10806 Dijkstra, Dijkstra (最小费用流)

Dijkstra, Dijkstra

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 Deu

Description

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

Input

The input will contain several test cases. Each test case will begin with 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 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

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

Sample Output

Back to jail

80

Back to jail

/*
 * UVa 10806 Dijkstra, Dijkstra
 * 求两条从S到T的路径,没有重复的边,并且边权和最小
 *
 * 最小费用流,流量为2时的费用就是答案,否则无解
 */

#include <bits/stdc++.h>
using namespace std;

struct MinCostMaxFlow
{
    const static int MAXN = 10000;
    const static int MAXE = 100000;
    const static int INF = 0x3f3f3f3f;
    struct Edge
    {
        int from,to,next,cap,flow,cost;
        Edge(){}
        Edge(int u,int v,int c,int f,int _c,int nxt):from(u),to(v),cap(c),flow(f),cost(_c),next(nxt) {}
    }edge[MAXE];
    int head[MAXN],tol,N;
    int pre[MAXN],dis[MAXN];
    bool vis[MAXN];
    //Function
    void init()
    {
        N=MAXN;
        tol=0;
        memset(head,-1,sizeof(head));
    }
    void link(int u,int v,int cap,int cost)//s->t,cap,cost
    {
        edge[tol]=Edge(u,v,cap,0,cost,head[u]);head[u]=tol++;
        edge[tol]=Edge(v,u,0,0,-cost,head[v]);head[v]=tol++;
    }
    bool spfa(int S,int T)
    {
        queue<int>Q;
        for(int i=0;i<N;i++) dis[i]=INF;
        for(int i=0;i<N;i++) vis[i]=false;
        for(int i=0;i<N;i++) pre[i]=-1;
        dis[S] = 0;
        vis[S] = true;
        Q.push(S);
        while(!Q.empty())
        {
            int u=Q.front();
            Q.pop();
            vis[u] = false;
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].to;
                if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
                {
                    dis[v]=dis[u]+edge[i].cost;
                    pre[v]=i;
                    if(!vis[v])
                    {
                        vis[v] = true;
                        Q.push(v);
                    }
                }
            }
        }
        if(pre[T] == -1) return false;
        else return true;
    }
    int MCMF(int S,int T,int &cost)
    {
        cost=0;
        int maxflow=0;
        while(spfa(S,T)&&maxflow<=2)
        {
            int Min=INF;
            for(int i=pre[T];i!=-1;i=pre[edge[i^1].to])
                Min=min(Min,edge[i].cap-edge[i].flow);
            for(int i=pre[T];i!=-1;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
                cost+=edge[i].cost*Min;
            }
            maxflow+=Min;
        }
        return maxflow;
    }
}MCMF;
int main()
{
    int n,m;
    int u,v,w;
    while(scanf("%d",&n)==1&&n)
    {
        MCMF.init();
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            MCMF.link(u,v,1,w);
            MCMF.link(v,u,1,w);
        }
        MCMF.link(0,1,2,0);
        MCMF.link(n,n+1,2,0);
        int ans;
        int f=MCMF.MCMF(0,n+1,ans);
        if(f<=1) printf("Back to jail\n");
        else printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-25 13:04:40

UVa 10806 Dijkstra, Dijkstra (最小费用流)的相关文章

uva 10806 Dijkstra, Dijkstra. (最小费最大流)

uva 10806 Dijkstra, Dijkstra. 题目大意:你和你的伙伴想要越狱.你的伙伴先去探路,等你的伙伴到火车站后,他会打电话给你(电话是藏在蛋糕里带进来的),然后你就能够跑去火车站了,那里有人接应你. 可是.由于你的伙伴跑去火车站的时候穿的是囚服,所以,他经过的街道都被戒严了,你必须从其它街道跑过去. 假设你能够到达火车站,请输出你和你的伙伴在路上花费的最短时间,假设不能请"Back to jail". 解题思路:最小费最大流.设置一个超级源点连向监狱(起点1), 容

UVA 10594 Data Flow (最小费用流)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=116&page=show_problem&problem=1535 Problem F Data Flow Time Limit 5 Seconds   In the latest Lab of IIUC, it requires to send huge amount of data from the local s

BOI&#39;98 DAY 2 TASK 1 CONFERENCE CALL Dijkstra/Dijkstra+priority_queue/SPFA

BOI'98 DAY 2 TASK 1 CONFERENCE CALL PROBLEM A telecom company would like to offer a three-party conference call service. This service enables three customers to participate in a telephone conversation simultaneously. A customer accesses the interconn

UVA 10806 Dijkstra, Dijkstra.

费用流第一题主要是临街表实现这个算法的问题.这里存下 思路还是比较简单.源点0,汇点N+1.费用为边长.容量为1.(普通边).添加边为2(0-1 N-N+1) 代码 #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <que

Airport Express UVA - 11374(dijkstra)

Airport Express UVA - 11374 题意:n个点,有m条普通路径,k条高速路径,但是k条只能选一条走.问从s到e最短时间. 如果选a-->b这条高速,那么s-->a和b--->e必然也要是最短路. 于是我们可以先用两次dijkstra预处理出s到各点的最短路和e到各点的最短路,然后枚举k条高速走哪条. 输出路径的时候,可以递归~ 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int in

UVA-10806 Dijkstra, Dijkstra. (最小费用流,网络流建模)

题目大意:给一张带权线图,找出一条经过起点s和终点t的最小回路. 题目分析:建立网络,以s为源点,t为汇点,另每条边的容量为1,单位费用为边权值.求最小费用流,增广两次后的最小费用便是答案. 代码如下: # include<iostream> # include<cstdio> # include<cmath> # include<string> # include<vector> # include<list> # include&

Uva 10806 来回最短路,不重复,MCMF

题目链接:https://uva.onlinejudge.org/external/108/10806.pdf 题意:无向图,从1到n来回的最短路,不走重复路. 分析:可以考虑为1到n的流量为2时的最小花费: 建图: 一个点到一个点的容量为1,费用为距离. 1 #include <cstring> 2 #include <cstdio> 3 #include <vector> 4 #include <queue> 5 #include <algorit

UVa 1658 (拆点法 最小费用流) Admiral

题意: 给出一个有向带权图,求从起点到终点的两条不相交路径使得权值和最小. 分析: 第一次听到“拆点法”这个名词. 把除起点和终点以外的点拆成两个点i和i',然后在这两点之间连一条容量为1,费用为0的边.这样就保证了每个点最多经过一次. 其他有向边的容量也是1 然后求从起点到终点的流量为2(这样就保证了是两条路径)的最小费用流. 本来要在加一个源点和汇点来限制流量的,但是这样弧就多了很多.lrj代码中用了很巧妙的方法,避免了这个问题. 1 #include <bits/stdc++.h> 2

UVA 10806 Cheerleaders

Cheerleaders Description C Cheerleaders In most professional sporting events, cheerleaders play a major role in entertaining the spectators. Their roles are substantial during breaks and prior to start of play. The world cup soccer is no exception. U