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

Problem ?
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 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 mstreets. 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


Problemsetter: Igor Naverniouk


题目大意:

有一个n个点,m条边的无向图,起点为1,汇点为n,问你走两遍,走过的边不能再走,最小花费是多少?

解题思路:

这是一道费用流,每条边只能走一遍,也就是流量为1,花费最小,也就是最小费用流。

代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn=110;
const int inf=(1<<30);
struct edge{
	int u,v,next,f,c;
	edge(int u0=0,int v0=0,int f0=0,int c0=0,int next0=0){
		u=u0,v=v0,f=f0,c=c0,next=next0;
	}
}e[maxn*maxn*10];
int head[maxn*2],visited[maxn*2],path[maxn*2],dist[maxn*2];
int cnt,from,to,marked,n;

void initial(){
	cnt=0;marked=1;
	from=1;to=n;
	memset(head,-1,sizeof(head));
	memset(visited,0,sizeof(visited));
}

void adde(int u,int v,int f,int c){
	e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].c=c,e[cnt].next=head[u],head[u]=cnt++;
	e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].c=-c,e[cnt].next=head[v],head[v]=cnt++;
}

void input(){
	int u0,v0,c0,m;
	scanf("%d",&m);
	while(m-- >0){
		scanf("%d%d%d",&u0,&v0,&c0);
		adde(u0,v0,1,c0);
		adde(v0,u0,1,c0);
	}
}

void bfs(){
	 for(int i=0;i<=n;i++){
	 	dist[i]=inf;
	 	path[i]=-1;
	 }
	 dist[from]=0;
     queue <int> q;
     q.push(from);
     marked++;
     visited[from]=marked;
     while(!q.empty()){
         int s=q.front();
         q.pop();
         for(int i=head[s];i!=-1;i=e[i].next){
              int d=e[i].v;
              if(e[i].f>0 && dist[s]+e[i].c<dist[d]){
                 dist[d]=dist[s]+e[i].c;
                 path[d]=i;
                 if(visited[d]!=marked){
                 	visited[d]=marked;
                 	q.push(d);
                 }
              }
         }
         visited[s]=-1;
     }
}

void computing(){
	int maxflow=0,ans=0;
	while(maxflow<2){
    	bfs();
    	if(dist[to]>=inf) break;
		ans+=dist[to];
		for(int i=to;i!=from;i=e[path[i]].u){
     		e[path[i]].f-=1;
        	e[path[i]^1].f+=1;
        	//cout<<e[path[i]].u<<"->";
    	}
    	//cout<<ans<<endl;
    	maxflow++;
    }
	if(maxflow<2) cout<<"Back to jail"<<endl;
    else cout<<ans<<endl;
}

int main(){
	while(scanf("%d",&n)!=EOF && n){
		initial();
		input();
		computing();
	}
	return 0;
}




posted @ 2014-04-16 13:59  炒饭君  阅读(165)  评论(0编辑  收藏  举报