POJ2135--Farm Tour(最小费用最大流)

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
题意:给定一个无向图,要从1点到n点再返回1点,每条边最多走一次,问最短需要走多远。
思路:设置一个超级源点和超级汇点,超级源点向1连一条容量为2,费用为0的边。
          节点n向超级汇点连一条容量为2,费用为0的边。然后对于图中所给的边,连一条容量为1,费用为w的边。注意是双向边。
           然后做一次最小费用最大流即可。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 3800
#define maxm 48000
#define inf 0x3f3f3f3f
int first[maxn];
int key[108][108];
int vv[maxm],ww[maxm],nxt[maxm],cst[maxm];
int e;
int pre[maxn],pos[maxn];
int dis[maxn],que[maxn*10];
bool vis[maxn];
inline int min(int a,int b)
{
	return a > b?b:a;
}
void addEdge(int u,int v,int w,int cost)
{
	vv[e] = v;
	ww[e] = w;
	cst[e] = cost;
	nxt[e] = first[u];
	first[u] = e++;
	vv[e] = u;
	ww[e] = 0;
	cst[e] = -cost;
	nxt[e] = first[v];
	first[v] = e++;
}

int spfa(int s,int t)
{
	memset(pre,-1,sizeof(pre));
	memset(vis,0,sizeof(vis));
	int head,tail;
	head = tail = 0;
	for(int i = 0;i < maxn;i++)
		dis[i] = inf;
	que[tail++] = s;
	pre[s] = s;
	dis[s] = 0;
	vis[s] = 1;
	while(head < tail)
	{
		int u = que[head++];
		vis[u] = 0;
		for(int i = first[u];i != -1;i = nxt[i])
		{
			int v = vv[i];
			if(ww[i] > 0 && dis[u] + cst[i] < dis[v])
			{
				dis[v] = dis[u] + cst[i];
				pre[v] = u;
				pos[v] = i;
				if(!vis[v])
				{
					vis[v] = 1;
					que[tail++] = v;
				}
			}
		}
	}
	return pre[t] != -1;
}

int MinCostFlow(int s,int t,int flow)
{
	int cost = 0;
	int nowflow = 0;
	while(spfa(s,t))
	{
		int f = inf;
		for(int i = t;i != s;i = pre[i])
			if(ww[pos[i]] < f)	f = ww[pos[i]];
		f = min(flow - nowflow,f);
		nowflow += f;	cost += dis[t]*f;
		for(int i = t;i != s;i = pre[i])
		{
			ww[pos[i]] -= f;
			ww[pos[i]^1] += f;
		}
		if(nowflow == flow)	break;
	}
	return cost;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int n,m;
	while(scanf("%d%d",&n,&m)==2)
	{
		memset(first,-1,sizeof(first));
		e = 0;
		for(int i = 1;i <= m;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			addEdge(u,v,1,w);
			addEdge(v,u,1,w);
		}
		addEdge(0,1,2,0);
		addEdge(n,n+1,2,0);
		printf("%d\n",MinCostFlow(0,n+1,2));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值