1. 程式人生 > >【POJ - 2135】Farm Tour(最小費用最大流)

【POJ - 2135】Farm Tour(最小費用最大流)

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000. 

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again. 

He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.

Input

* Line 1: Two space-separated integers: N and M. 

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length. 

Output

A single line containing the length of the shortest tour. 

Sample Input

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output

6

思路:

這道題一開始想用兩次dijkstra解決,但其實是不行的因為有可能找到一條最短路後,第二次就沒有路徑可走了,但第一次走的不是最短路,之後可能會有路徑,這一題是要求到再回去所以不行。如下圖:

 

看完題解後才知道這是一道最小費用最大流的題目,最小費用最大流,先是保證最大流量在找最小的費用,用spfa找最短路。這道題目中因為每條路只能走一遍,所以所有的邊的流量都設為1,又因為是找兩條路,所以找到兩條增廣路後,及流量為2時就退出搜尋。

以下來自:此部落格

求解步驟

(1)找到一條從源點到達匯點的“距離最短”的路徑,“距離”使用該路徑上的邊的單位費用之和

來衡量。 
(2)然後找出這條路徑上的邊的容量的最小值f,則當前最大流max_flow擴充f,同時當前最小費用min_cost擴充 f*min_dist(s,t)。 
(3)將這條路徑上的每條正向邊的容量都減少f,每條反向邊的容量都增加f。 
(4)重複(1)--(3)直到無法找到從源點到達匯點的路徑。

ac程式碼:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<set>
#include<iostream>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#define ll long long
#define mod 1000000007
#define eps 1e-8
using namespace std;
const int MAXN = 10000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge {
	int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//節點總個數,節點編號從 0 ~ N-1
void init(int n) {
	N = n;
	tol = 0;
	memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost) {
	edge[tol].to = v;
	edge[tol].cap = cap;
	edge[tol].cost = cost;
	edge[tol].flow = 0;
	edge[tol].next = head[u];
	head[u] = tol++;
	edge[tol].to = u;
	edge[tol].cap = 0;
	edge[tol].cost = -cost;
	edge[tol].flow = 0;
	edge[tol].next = head[v];
	head[v] = tol++;
}
bool spfa(int s,int t) {
	queue<int>q;
	for(int i = 0; i < N+1; i++) {
		dis[i] = INF;
		vis[i] = false;
		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;
}
//返回的是最大流,cost 存的是最小費用
int minCostMaxflow(int s,int t,int &cost) {
	int flow = 0;
	cost = 0;
	while(spfa(s,t)) {
		int Min = INF;
		for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) {
			if(Min > edge[i].cap - edge[i].flow)
				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;
		}
		flow += Min;
		if(flow==2)
		return flow;
	}
	return flow;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	init(n);
	for(int i=0;i<m;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addedge(u,v,1,w);
		addedge(v,u,1,w);
	}
	int c;
	minCostMaxflow(1,n,c);
	cout<<c<<endl;
	return 0;
}