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

  • 题目大意:
    一个农场主,要带人逛他的农场,农场里有一些谷仓,还有个大谷仓,他想先到大谷仓,再回到家,在这个过程中,把所有的点都走一遍。每一条路都有个距离,还要求距离最小而且相同的边不能走第二次。
    抽象一下就是,有一个图有n个结点,m条边,要求从1号结点走到n号结点,在走回1号结点,经过所有的点,并要求路程最短而且相同的边不能走第二次。
  • 解题思路
    我们把这个问题抽象成一个最小费用最大流问题。
    1.因为每条边只能走一次,所以把每条边的容量都设为1
    2.因为只需要过去,再回来,所以只要两条路就够了,我们只需找两次增广路就够了
    3.因为要求最短的路,所以我们每次找增广路的时候,我们用spfa找1-n的最短路。

因为这是我第一个最小费用最大流的问题,就在这里简单介绍一下我理解的最小费用最大流,菜鸡一个,如有不对,欢迎指正。

  • 最小费用最大流
    定义:每个边 G=(v,u)除了给定的容量C(u,v)之外,还给了一个单位流量的费用 W(u,j),所谓的最小费用最大流问题就是求一个最大流f,使得流得总运输费最小。
    简单来说就是,找费用最小的最大流。我的这篇博客里有对最大流的详细解释https://blog.csdn.net/weixin_43179892/article/details/84351135
    那么我们具体怎么解决呢?
    首先我们想到在解决最大流问题的时候,我们是不停的找从源点到汇点的增广路,在这里,我们还是找增广路,但是要求费用最小,所以我们就每次找从源点到汇点的最短路,用SPFA找。不停的更新最大流,一直到更新不能更新为止,因为我们每次找的都是最短路,所以,每一条都是最小的花费,所以总的花费就是最小的了。
    具体求解步骤:(引自大佬博客大佬博客
    1)找到一条从源点到达汇点的“距离最短”的路径,“距离”使用该路径上的边的单位费用之和来衡量。
    (2)然后找出这条路径上的边的容量的最小值f,则当前最大流max_flow扩充f,同时当前最小费用min_cost扩充 f*min_dist(s,t)。
    (3)将这条路径上的每条正向边的容量都减少f,每条反向边的容量都增加f。
    (4)重复(1)–(3)直到无法找到从源点到达汇点的路径。

当然具体的实现细节实在不好一一在这里解释,很繁琐。个人认为可以把这个当成板子,然后理解好每一行代码的含义,会用就好。我这个是我看了别人的板子,自己敲得,加了些注释,希望能让你更好的理解一下。

  • AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
struct node{
	int to;
	int cap;///还能过多少流量
	int flow;//已经过了多少流量
	int cost;//花费
	int next;
}side[maxn];
int n,m;//点的个数 边的个数
int head[maxn];
int pre[maxn];
int path[maxn];
int dis[maxn];
int vis[maxn];
int cnt=0;
void init()
{
	memset(head,-1,sizeof(head));
	memset(pre,-1,sizeof(pre));
	cnt=0;
}
void  add_side(int u,int v,int cap ,int cost)
{
	side[cnt].to=v;
	side[cnt].cap=cap;
	side[cnt].cost=cost;
	side[cnt].next=head[u];
	side[cnt].flow=0;
	head[u]=cnt++;
	///反向弧
	side[cnt].to=u;
	side[cnt].cap=0;
	side[cnt].cost=-cost;///注意这个地方是-cost
	side[cnt].next=head[v];
	side[cnt].flow=0;
	head[v]=cnt++;
}

bool spfa(int s,int t)//找最短路,就是找一条最小花费的增广路
{
	memset(pre,-1,sizeof(pre));
	memset(dis,0x3f3f3f3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	vis[s]=1;
	dis[s]=0;
	queue<int> q;
	q.push(s);
	while(q.size())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i!=-1;i=side[i].next)
		{
			int v=side[i].to;
			if(side[i].cap>side[i].flow&& dis[u]+side[i].cost<dis[v])///两个条件 首先得有残余量,其次要能松弛
			{
				dis[v]=dis[u]+side[i].cost;
				pre[v]=i;//v指的边的编号
				if(!vis[v])
				{
					vis[v]=1;
					q.push(v);
				}
				
			}
		}
	}
	if(pre[t]==-1)//说明没有找到最后一个点 也就是没有增广路了
		return false ;
	return true;
}

int MincostMaxflow(int s,int t,int &cost)///返回最大流 并 把最小花费储存在cost里
{
	int flow=0;
	cost=0;
	while(spfa(s,t))
	{
		spfa(s,t);
		int f=0x3f3f3f3f;
		for(int u=pre[t];u!=-1;u=pre[side[u^1].to])
		{
			if(f>side[u].cap-side[u].flow)//一条增广路上的最小的流就是增广的值,cap-flow 增广的值
				f=side[u].cap-side[u].flow;
		}
		flow+=f;///加到总的流量上

		for(int i=pre[t];i!=-1;i=pre[side[i^1].to])//更新边 加 反向边
		{
			side[i].flow+=f;
			side[i^1].flow-=f;
			cost+=side[i].cost*f;
		}
		if(flow==2)
			return flow;
	}
	return flow;
}
int main()
{
	cin>>n>>m;
	init();
	while(m--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		add_side(a,b,1,c);//流量设为1
		add_side(b,a,1,c);
	}
	int cost=0;

	MincostMaxflow(1,n,cost);
	cout<<cost<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43179892/article/details/84576610