Farm Tour poj 2135 最小费用流+另一个不错的模板

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10331 Accepted: 3827

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



这个题目可以说还是很不错的,有一个农夫想从家到仓库,有路径,他去和回来不能挑同一条路,求最短的路径,为什么这个
题目不可以用两次最短路呢?很简单,当我们第一次运行最短路找出最短路时,第二次找的时候就不能用了,这样两次加起来
不见得就是最小了,就好像第一次是3,第二次是20,而最短的方法第一次虽然是5,第二次却也是5,反而是最小的,就是这个
道理。


其实最短路也是一种特殊的最小费用流,只不过流为1,所以我们建图的时候把路径可以过的点流为1,超级源点设为0,到每个点
的流为2,然后点到超级汇点n的流也为2,为什么是2呢?因为这是无向图,每两个点之间都可能会有两次经过,就是a到b过了,
b到a也过了,所以是2

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define  vMax   1005
#define  eMax   40010
#define   inf   0x7fffffff
int dis[vMax], que[vMax], head[vMax], pre[vMax], qe[eMax];
bool vis[vMax];
int ne, n, ans;

struct Edge
{
	int v, c, cost, next;
} edge[eMax];

bool spfa()
{
	int i, f = 0, r = 1;

	memset(vis, false, sizeof(vis));
	memset(pre, -1, sizeof(pre));
	for(i = 0; i <= n; i++)
		dis[i] = inf;
	que[0] = 0;
	vis[0] = true;
	pre[0] = 0;
	dis[0] = 0;
	while(f != r)
	{
		int cur = que[f++];
		if(f == vMax) f = 0;
        vis[cur] = false;
		for(i = head[cur]; i != -1; i = edge[i].next)
		{
			int u = edge[i].v;
			if(edge[i].c && dis[u] > dis[cur] + edge[i].cost)
			{
				dis[u] = dis[cur] + edge[i].cost;
				if(!vis[u])
				{
					vis[u] = true;
					que[r++] = u;
					if(r == vMax) r = 0;
				}
				pre[u] = cur;
                qe[u] = i;
			}
		}
	}
    if(pre[n] == -1) return false;
	else return true;
}

void end()
{
   int i, tmp = inf;

   for(i = n; i != 0; i = pre[i])
	   tmp = min(tmp, edge[qe[i]].c);
   ans += dis[n] * tmp;
   for(i = n; i != 0; i = pre[i])
   {
	   edge[qe[i]].c -= tmp;
	   edge[qe[i]^1].c += tmp;
   }
}
void addEdge(int u, int v, int c, int cost)
{
    edge[ne].v = v; edge[ne].c = c; edge[ne].cost = cost;
	edge[ne].next = head[u]; head[u] = ne++;
	edge[ne].v = u; edge[ne].c = 0; edge[ne].cost = -cost;
	edge[ne].next = head[v]; head[v] = ne++;
}

int main()
{
	int N, M;
    int u, v, cost;
	scanf("%d%d", &N, &M);
	memset(head, -1, sizeof(head));
	ne = ans = 0;
    n= N+1;
	while(M--)
	{
		scanf("%d%d%d", &u, &v, &cost);
		addEdge(u, v, 1, cost);
		addEdge(v, u, 1, cost);
	}
    addEdge(0, 1, 2, 0);
	addEdge(N, n, 2, 0);
	while(spfa()) {end();}
	printf("%d\n", ans);
	return 0;
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值