首页 > 代码库 > POJ 2135 Farm Tour

POJ 2135 Farm Tour

最小费用最大流.....


Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11735 Accepted: 4373

Description

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

Source

USACO 2003 February Green




#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn=11000;
const int INF=(1<<30);

struct Edge
{
	int to,next,cap,flow,cost;
}edge[maxn*22];

int Adj[maxn],Size,n,m;

void init()
{
	Size=0;
	memset(Adj,-1,sizeof(Adj));
}

void addedge(int u,int v,int cap,int cost)
{
	edge[Size].to=v; 
	edge[Size].next=Adj[u];
	edge[Size].cost=cost; 
	edge[Size].cap=cap;
	edge[Size].flow=0; 
	Adj[u]=Size++;
}

void Add_Edge(int u,int v,int cap,int cost)
{
	addedge(u,v,cap,cost);
	addedge(v,u,0,-cost);
}

int dist[maxn],vis[maxn],pre[maxn];

bool spfa(int s,int t)
{
	queue<int> q;
	for(int i=0;i<n;i++)
	{
		dist[i]=INF; vis[i]=0; pre[i]=-1;
	}
	dist[s]=0; vis[s]=1; q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=Adj[u];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(edge[i].cap>edge[i].flow&&
					dist[v]>dist[u]+edge[i].cost)
			{
				dist[v]=dist[u]+edge[i].cost;
				pre[v]=i;
				if(!vis[v])
				{
					vis[v]=true;
					q.push(v);
				}
			}
		}
	}
	if(pre[t]==-1) return false;
	return true;
}

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;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;i=pre[edge[i^1].to])
		{
			edge[i].flow+=Min;
			edge[i^1].flow-=Min;
			cost+=edge[i].cost*Min;
		}
		flow+=Min;
	}
	return flow;
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init();
		for(int i=0;i<m;i++)
		{
			int u,v,c;
			scanf("%d%d%d",&u,&v,&c);
			Add_Edge(u,v,1,c);
			Add_Edge(v,u,1,c);
		}
		n++;
		Add_Edge(0,1,2,0); Add_Edge(1,0,2,0);
		Add_Edge(n-1,n,2,0); Add_Edge(n,n-1,2,0);
		int liu,cost; n++;
		liu=MinCostMaxFlow(0,n-1,cost);
		printf("%d\n",cost);
	}
	return 0;
}


POJ 2135 Farm Tour