# [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

## 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, E;
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;
int dis;
int q, fr, re;
bool inq;

inline int MCMF(){
int mincost = 0;
do{
fr = re = 0, q[re++] = 0;
memset(inq, 0, sizeof(inq)), inq = 1;
memset(prev, 0, sizeof(prev));
memset(dis, 0x3f, sizeof(dis)), dis = 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){
int dflow = 0x3f3f3f3f, dcost = 0;
for(register Edge *ne = prev; ne; ne = prev[ne->from]) dflow = min(dflow, ne->cap);
for(register Edge *ne = prev; ne; ne = prev[ne->from])
ne->cap -= dflow, ne->rev->cap += dflow, dcost += ne->cost * dflow;
mincost += dcost;
}
} while(prev);
return mincost;
}

int main(){
scanf("%d%d", &N, &M);
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;
}
```

#### 评论

49750

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