网络流之最小费用流(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;
}
全部评论

相关推荐

10-29 22:30
吉林大学 Java
同专业学长学姐,去互联网大厂的起薪&nbsp;15k+,去国企&nbsp;IT&nbsp;岗的也有&nbsp;12k+,就连去中小厂的都基本&nbsp;13k&nbsp;起步😤&nbsp;我投的传统行业技术岗,拼死拼活拿到&nbsp;1Woffer,本来还挺开心,结果逛了圈牛客直接破防,同是校招生,行业差距怎么就这么大啊!
喵喵喵6_6:应该哪里不对吧,大厂都是20k以上的,10k那种对于985本的学生基本就是点击一下过了笔试就送的,我前两天刚拿了一个11k,笔试完第2天就打电话了,非科班。坏消息是c++岗开这么低真是刷新认知了
校招生月薪1W算什么水平
点赞 评论 收藏
分享
10-20 16:50
门头沟学院 Java
牛客68421677...:同是天涯沦落人啊,我也是26届0实习,不知道怎么办了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务