POJ - 2135 Farm Tour

来源:互联网 发布:金山数据恢复账号购买 编辑:程序博客网 时间:2024/05/22 06:12
POJ - 2135点我点我:-)
Farm Tour
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

SubmitStatus

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 51 2 12 3 13 4 11 3 22 4 2

Sample Output

6


本题是最小费用最大流,主要是建模的过程:

题意:FJ有N个农场,M条路,FJ要领朋友游玩,从1走到N,再回到1,不走重复路,每条路长度不一样,问最短路长为多少。


我们把每一条路的流量看做1,(注意是双向的)费用看做是路的长度,这样的话FJ每条边最多走一遍了,然后建一个超级源点,

一个超级汇点,源点和汇点分别与1,n两点连一条流量为2,费用为0的边即可。


(MCMF算法)

#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<queue>using namespace std;#define MAXN (2000+5)#define MAXM (40000+5)#define INF 0x3f3f3f3f#define pb push_back#define Set(a, v) memset(a, v, sizeof(a))#define For(i, a, b) for(int i = (a); i <= (b); i++)#define Fort(i, a, b) for(int i = (a); i < (b); i++)struct Edge{int from, to, cap, flow, cost;};struct MCMF{int n, m, s, t;vector<Edge> edges;vector<int> G[MAXN];int p[MAXN], a[MAXN], d[MAXN];bool inq[MAXN];void init(int n){this->n = n;For(i, 0, n+1) G[i].clear();edges.clear();}void Addedge(int from, int to, int cap, int cost){edges.pb((Edge){from, to, cap, 0, cost});edges.pb((Edge){to, from, 0, 0, -cost});m = edges.size();G[from].pb(m-2);G[to].pb(m-1);}bool BellmanFord(int &flow, int &cost){Set(inq, 0); Set(d, INF);queue<int> q;q.push(s);d[s] = 0; inq[s] = true; p[s] = 0; a[s] = INF;while(!q.empty()){int now = q.front(); q.pop();inq[now] = false;Fort(i, 0, G[now].size()){Edge &e = edges[G[now][i]];if(e.flow < e.cap && (d[e.to] > d[now]+e.cost)){d[e.to] = d[now]+e.cost;p[e.to] = G[now][i];a[e.to] = min(a[now], e.cap-e.flow);if(!inq[e.to]) q.push(e.to), inq[e.to] = true;}}}if(d[t] == INF) return false;flow += a[t]; cost += a[t]*d[t];int u = t;while(u != s){edges[p[u]].flow += a[t];edges[p[u]^1].flow -= a[t];u = edges[p[u]].from;}return true;}int Maxflow(int S, int T){s = S; t = T;int flow = 0, cost = 0;while(BellmanFord(flow, cost)); //printf("flow = %d, cost = %d\n", flow, cost);return cost;}}mcmf;int main(){freopen("test.in", "r", stdin);freopen("test.out", "w", stdout);int n, m;scanf("%d%d", &n, &m);For(i, 1, m){int u, v, d;scanf("%d%d%d", &u, &v, &d);mcmf.Addedge(u, v, 1, d);mcmf.Addedge(v, u, 1, d);}mcmf.Addedge(0, 1, 2, 0);mcmf.Addedge(n, n+1, 2, 0);printf("%d\n", mcmf.Maxflow(0, n+1));return 0;}



1 0