uva10806 - Dijkstra, Dijkstra. 最小费用流 求两条路权值之和最小

来源:互联网 发布:d3.js官网 编辑:程序博客网 时间:2024/06/13 02:09

  最小费用流是每次在残量图中求出起点到终点的最短路,然后再找出最短路上权值最小的一个边作为增广流量进行增广,更新流量,加上费用(最短路*增广流量)。找最短路用spfa算法。因为每次增广后都是新流量下的最小费用流,所以最后就是最大流的最小费用。

  如果是无向图、有平行边和反向边就比较麻烦了。。在有向图,没有平行边和反向边的情况下,设cap[v][u]=0,cost[v][u]=-cost[v][u],这样就容易了。


  要求两条路权值之和最小,也是用到最小费用流的思想。

Dijkstra, Dijkstra.
Time Limit: 10 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, 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 them 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 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


  不能先找出最短路,把这条路删了,再找条最短的,这样并不能保证两条路权值之和最小。设一个起点0,到1的cap为2,费用0,一个终点N+1,到N的容量2,费用0。把有连接的点的cap都设为1。这样第一次增广的流最多是1,找到流为1的最小费用,第二次增广的流最多也是1,找到流为2的最小费用。如果最后流小于2,就说明不能实现。

  很坑的是这道题是无向图,但幸运的是一条边要么满载要么是空的(流不是1就是0),不会出现流不为0而且小于容量的情况。所以一开始cost[u][v]=cost[v][u],每次增广路再把最短路径上的cost[u][v]=cost[v][u]变成他们的负值。


#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<cmath>#include<queue>#define INF 0x3f3f3f3f#define MAXN 110using namespace std;int N,M,ans;int flow[MAXN][MAXN],cost[MAXN][MAXN],cap[MAXN][MAXN],d[MAXN],inq[MAXN],p[MAXN];int min_cost(){    memset(flow,0,sizeof(flow));    int s=0,t=N+1,f=0;    ans=0;    while(1){        memset(d,INF,sizeof(d));        memset(inq,0,sizeof(inq));        queue<int> q;        d[s]=0;        q.push(s);        inq[s]=1;        while(!q.empty()){            int u=q.front();            q.pop();            inq[u]=0;            for(int v=0;v<=N+1;v++) if(cap[u][v]>flow[u][v]&&d[v]>d[u]+cost[u][v]){                d[v]=d[u]+cost[u][v];                p[v]=u;                if(!inq[v]){                    q.push(v);                    inq[v]=1;                }            }        }        if(d[t]==INF) break;        int a=INF;        for(int u=t;u!=s;u=p[u]){            a=min(a,cap[p[u]][u]-flow[p[u]][u]);            cost[p[u]][u]=cost[u][p[u]]=-cost[u][p[u]];        }        for(int u=t;u!=s;u=p[u]){            flow[p[u]][u]+=a;            flow[u][p[u]]-=a;        }        f+=a;        ans+=a*d[t];    }    if(f<2) return 0;    return 1;}int main(){    freopen("in.txt","r",stdin);    while(scanf("%d",&N),N){        scanf("%d",&M);        memset(cap,0,sizeof(cap));        memset(cost,INF,sizeof(cost));        int u,v,t;        while(M--){            scanf("%d%d%d",&u,&v,&t);            cap[u][v]=cap[v][u]=1;            cost[u][v]=cost[v][u]=t;        }        cap[0][1]=2;        cap[N][N+1]=2;        cost[0][1]=cost[1][0]=0;        cost[N][N+1]=cost[N+1][N]=0;        if(min_cost()) printf("%d\n",ans);        else printf("Back to jail\n");    }    return 0;}


0 0