POJ2135(Farm Tour)

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的最小费用流问题。但是这道题如果单纯的跑一次最短路再删边继续跑一次最短路可能没有答案。
在这里插入图片描述
如果是上面那种做法先做一次最短路删边继续最短那么第一次最短路的时候必定走1->2这条边,删完边图不连通没有第二条最短路了。为了避免这种操作引入最大流问题结合一起解决。因为网络流可以通过"后悔"操作调整流量,这里的流量就是每条边能访问的次数1。因此利用最小费用最大流就可以将最短路调整。

  1. 关于建边,首先这是无向图,先将无向边转为两条有向边,然后每条有向边再可以在转化为一条正向弧和一条反向弧。正向弧的流量为1,费用为距离,反向弧的流量为0,费用为距离的负数。另外一条有向边同理。

  2. 建立超级源点,因为这道题要求去一次回一次。所以建立超级源点S到起点,流量为2,距离为0。终点到超级汇点,流量为2,距离为0。

  3. 最后跑SPFA + EK从S - > T找最小费用及时最短距离。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
const int maxm = 1e4+5;
const int inf = 0x3f3f3f3f;
const int maxn = 1010;
struct edge{
	int to;
	int next;
	int f;
	int w;
}e[maxm<<2];
int head[maxn];
int dist[maxn];
int pre[maxn];
int num[maxn];
bool vis[maxn];
int tot;
inline void addedge(int x,int y,int f,int w)
{
	e[tot].to = y;
	e[tot].f = f;
	e[tot].w = w;
	e[tot].next = head[x];
	head[x] = tot++;
	
	e[tot].to = x;
	e[tot].f = 0;
	e[tot].w = -w;
	e[tot].next = head[y];
	head[y] = tot++;
}
bool spfa(int s,int t)
{
	memset(vis,false,sizeof(vis));
	memset(dist,0x3f,sizeof(dist));
	memset(pre,-1,sizeof(pre));
	memset(num,-1,sizeof(num));
	queue<int>q;
	q.push(s);
	dist[s] = 0;
	while(!q.empty()){
		int x = q.front();
		q.pop();
		vis[x] = false;
		for(int i = head[x];~i;i = e[i].next){
			if(e[i].f > 0 && dist[e[i].to] > dist[x] + e[i].w){
				dist[e[i].to] = dist[x] + e[i].w;
				pre[e[i].to] = x;
				num[e[i].to] = i;
				if(!vis[e[i].to]){
					q.push(e[i].to);
					vis[e[i].to] = true;
				}
			}
		}
	}
	return dist[t] < inf; 
}
int mcmf(int s,int t)
{
	int cost = 0;
	while(spfa(s,t)){
		int mf = inf;
		for(int i = t;i != s;i = pre[i]){
			mf = min(mf,e[num[i]].f);
		}
		for(int i = t;i != s;i = pre[i]){
			e[num[i]].f -= mf;
			e[num[i]^1].f += mf;
		}
		cost += mf*dist[t];
	}
	return cost;
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		tot = 0;
		memset(head,-1,sizeof(head));
		for(int i = 0;i < m;i++){
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			addedge(x,y,1,z);	addedge(y,x,1,z);
		}
		int s = 0,t = 1003;
		addedge(s,1,2,0);		addedge(n,t,2,0);	
		int ans = mcmf(s,t);
		printf("%d\n",ans);
	}
	return 0;
} 

愿你走出半生,归来仍是少年~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值