[POJ 2135] Farm Tour【费用流】

  • 2018-01-20
  • 0
  • 0

Problem:

Time Limit: 1000MS Memory Limit: 65536K

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

USACO 2003 February Green

Solution:

这题数据有毒。。。N 的范围必须开到 2000。。

题目本身是一道很好的最小费用最大流模板题,求的是从 1 -> N 再从 N -> 1 的不重复路径最短路

由于每条路径只能走一次,所以每条边的容量为 1。而需要求的是最短路,所以将路径长度作为费用。

注意本题的边均为无向边,当成两条有向边处理即可,这样的话总边数要开到 4 * M

最小费用最大流可以用 SPFA 版的 Edmonds-Karp 算法求解。

Code: O(VkE2), k为节点平均入队次数 [1624K, 32MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int N, M;

struct Edge{
	int from, np, cap, cost;
	Edge *nxt, *rev;
};

struct Graph{
	Edge *V[2005], E[40010];
	int tope;
	
	inline void addedge(int u, int v, int cap, int cost = 0){
		E[++tope].from = u, E[tope].np = v, E[tope].cap = cap, E[tope].cost = cost;
		E[tope].nxt = V[u], E[tope].rev = &E[tope + 1], V[u] = &E[tope];
		
		E[++tope].from = v, E[tope].np = u, E[tope].cap = 0, E[tope].cost = -cost;
		E[tope].nxt = V[v], E[tope].rev = &E[tope - 1], V[v] = &E[tope];
	}
} G;

Edge *prev[2005];
int dis[2005];
int q[2005], fr, re;
bool inq[2005];

inline int MCMF(){
	int mincost = 0;
	do{
		fr = re = 0, q[re++] = 0;
		memset(inq, 0, sizeof(inq)), inq[0] = 1;
		memset(prev, 0, sizeof(prev));
		memset(dis, 0x3f, sizeof(dis)), dis[0] = 0;
		while(fr != re){
			int u = q[fr++]; inq[u] = 0;
			for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
				if(ne->np && ne->cap && dis[u] + ne->cost < dis[ne->np]){
					dis[ne->np] = dis[u] + ne->cost;
					prev[ne->np] = ne;
					if(!inq[ne->np]) q[re++] = ne->np, inq[ne->np] = 1;
				}
		}
		if(prev[2001]){
			int dflow = 0x3f3f3f3f, dcost = 0;
			for(register Edge *ne = prev[2001]; ne; ne = prev[ne->from]) dflow = min(dflow, ne->cap);
			for(register Edge *ne = prev[2001]; ne; ne = prev[ne->from])
				ne->cap -= dflow, ne->rev->cap += dflow, dcost += ne->cost * dflow;
			mincost += dcost;
		}
	} while(prev[2001]);
	return mincost;
}

int main(){
	scanf("%d%d", &N, &M);
	G.addedge(0, 1, 2), G.addedge(N, 2001, 2);
	for(register int i = 1; i <= M; i++){
		int st, en, len;
		scanf("%d%d%d", &st, &en, &len);
		G.addedge(st, en, 1, len), G.addedge(en, st, 1, len);
	}
	int mincost = MCMF();
	printf("%d\n", mincost);
	return 0;
}

评论

还没有任何评论,你来说两句吧



新博客地址: darkleafin.cf

常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai