网络流之最小费用流(POJ-2135 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.

输入:
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
输出:
6

思路
把边的长度当做费用,每条边的容量1,然后将u->v,v->u这两条边加进去。建图完成之后,走一遍流量为2的最小费用流就是答案。

AC代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
struct Edge {
	int to, next, cap, flow, cost;
}edge[maxn];
int head[maxn], tol;
int pre[maxn], dis[maxn];
bool vis[maxn];
int N;
void init(int n){
	N = n;
	tol = 0;
	memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cap, int cost){
	edge[tol].to = v;
	edge[tol].cap = cap;
	edge[tol].cost = cost;
	edge[tol].flow = 0;
	edge[tol].next = head[u];
	head[u] = tol++;
	edge[tol].to = u;
	edge[tol].cap = 0;
	edge[tol].cost = -cost;
	edge[tol].flow = 0;
	edge[tol].next = head[v];
	head[v] = tol++;
}
bool spfa(int s, int e){
	queue<int>q;
	for (int i = 0; i < N; i++) {
		dis[i] = INF;
		vis[i] = false;
		pre[i] = -1;
	}
	dis[s] = 0;
	vis[s] = true;
	q.push(s);
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		vis[u] = false;
		for (int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].to;
			if (edge[i].cap > edge[i].flow&& dis[v] > dis[u] + edge[i].cost) {
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i;
				if (!vis[v]) {
					vis[v] = true;
					q.push(v);
				}
			}
		}
	}
	if (pre[e] == -1)
		return false;
	else
		return true;
}
int min_cost_flow(int s, int e){
	int cost = 0;
	while (spfa(s, e)) {
		int Min = INF;
		for (int i = pre[e]; i != -1; 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[e]; i != -1; i = pre[edge[i ^ 1].to]) {
			edge[i].flow += Min;
			edge[i ^ 1].flow -= Min;
			cost += edge[i].cost * Min;
		}
	}
	return cost;
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, m;
	while (scanf("%d%d", &n, &m) != EOF) {
		init(n + 2);//初始化建图,增加源点和汇点,源点到1,汇点到n,费用为0,流量为2;
		int s, e, c;
		for (int i = 0; i < m; i++) {
			cin >> s >> e >> c;
			addedge(s, e, 1, c);
			addedge(e, s, 1, c);
		}
		addedge(0, 1, 2, 0);//源点到1
		addedge(n, n + 1, 2, 0);//n到汇点
		cout << min_cost_flow(0, n + 1) << endl;
	}
	return 0;
}
发布了40 篇原创文章 · 获赞 6 · 访问量 1403

猜你喜欢

转载自blog.csdn.net/qq_43321732/article/details/104181820