首页 > 代码库 > POJ2135 Farm Tour 【最小费用最大流】

POJ2135 Farm Tour 【最小费用最大流】

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11782 Accepted: 4393

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

题意:FJ带朋友参观自己的农场,从自己的房子出发到农场,再从农场返回自己的房子,要求去回不走同一条路。房子的点数为1,农场为n,在1到n之间有很多点,给出n个顶点,m条边,然后m行每行有三个数,a,b,c代表a到c的路径长度为c,并且a到b是无向边,现在要求从1点到n点在从n点返回1点的最短路(摘自贰圣的博客

题解:将路的长度看成费用,路的条数看成流,然后添加附加源汇点就构成了最小费用最大流问题。第22行-f是因为每条路只能走一次,如果走了两次那么两次的费用相互抵消,相当于这条路没走过。

#include <stdio.h>
#include <string.h>
#include <queue>
#define inf 0x3f3f3f3f
#define maxn 1010
#define maxm 10010
using std::queue;

int head[maxn];
struct Node {
    int u, v, next, c, f; // c是容量,f是费用
} E[maxm << 2];
int n, m, id;
int dist[maxn], load[maxn];
bool vis[maxn];

void addEdge(int u, int v, int f, int c) {
    E[id].u = u; E[id].v = v; E[id].c = c;
    E[id].f = f; E[id].next = head[u];
    head[u] = id++;
    E[id].u = v; E[id].v = u; E[id].c = 0;
    E[id].f = -f; E[id].next = head[v];
    head[v] = id++;
}

void getMap() {
    memset(head, -1, sizeof(int) * (n + 2));
    int u, v, f; id = 0;
    while(m--) {
        scanf("%d%d%d", &u, &v, &f);
        addEdge(u, v, f, 1);
        addEdge(v, u, f, 1);
    }
    addEdge(0, 1, 0, 2); // u, v, f, c
    addEdge(n, n + 1, 0, 2);
}

bool SPFA(int start, int end) {
    queue<int> Q; int i, u, v;
    memset(vis, 0, sizeof(bool) * (n + 2));
    memset(load, -1, sizeof(int) * (n + 2));
    memset(dist, 0x3f, sizeof(int) * (n + 2));
    Q.push(start); vis[start] = 1; dist[start] = 0;
    while(!Q.empty()) {
        u = Q.front(); Q.pop();
        vis[u] = 0;
        for(i = head[u]; i != -1; i = E[i].next) {
            v = E[i].v;
            if(E[i].c && dist[v] > dist[u] + E[i].f) {
                dist[v] = dist[u] + E[i].f; 
                load[v] = i;
                if(!vis[v]) {
                    vis[v] = 1; Q.push(v);
                }
            }
        }
    }
    return dist[end] != inf;
}

int Min_Cost_Flow(int start, int end) {
    int ans_cost = 0;
    int u, minCut;
    while(SPFA(start, end)) {
        minCut = inf;
        for(u = load[end]; u != -1; u = load[E[u].u]) {
            if(minCut > E[u].c)
                minCut = E[u].c;            
        }
        for(u = load[end]; u != -1; u = load[E[u].u]) {
            E[u].c -= minCut;          
            E[u^1].c += minCut;
        }
        ans_cost += dist[end] * minCut;
    }
    return ans_cost;
}

int main() {
    // freopen("stdin.txt", "r", stdin); 
    while(scanf("%d%d", &n, &m) == 2) {
        getMap();
        printf("%d\n", Min_Cost_Flow(0, n + 1));
    }
    return 0;
}


POJ2135 Farm Tour 【最小费用最大流】