POJ 2135 Farm Tour【最小费用最大流(最短路)】

描述
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.
输入
* 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.
输出
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

题意:给你N个农田、M条无向边以及每条边的长度(也就是费用)。现在让你从1走到N再从N走回1,要求不能走重复的边。问你所走的路径和总长的最小值,题目保证有答案。
分析:
如果路径可以重复的话就是最短路裸题了,要求的是不重复的两条路径。
1->N两条路径的最短长度,可以抽象成1到n最大流为2情况下的最小费用了。把最大流限制为2,所然后拿MCMF去跑就行了。

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 1010
#define MAXM 50000+10
#define INF 0x3f3f3f3f
using namespace std;

int head[MAXN], edgenum;
int pre[MAXN], dist[MAXN];
bool vis[MAXN];
int N, M;

struct Edge {
    int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];

void init() {
    edgenum = 0;
    memset(head, -1, sizeof(head));
}

void addEdge(int u, int v, int w, int c) {
    Edge E1 = {u, v, w, 0, c, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, -c, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}

bool SPFA(int s, int t) {
    queue<int> Q;
    memset(dist, INF, sizeof(dist));
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    dist[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)
        {
            Edge E = edge[i];
            if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + E.cost;
                pre[E.to] = i;
                if(!vis[E.to])
                {
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
    }
    return pre[t] != -1;
}

void MCMF(int s, int t, int &cost) {
    cost = 0;
    while(SPFA(s, t))
    {
        int Min = INF;
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            Edge E = edge[i];
            Min = min(Min, E.cap-E.flow);
        }
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            edge[i].flow += Min;
            edge[i^1].flow -= Min;
            cost += edge[i].cost * Min;
        }
    }
}

int main()
{
    scanf("%d %d", &N, &M);
    init();
    int a, b, c;
    while(M--) {
        scanf("%d %d %d", &a, &b, &c);
        addEdge(a, b, 1, c);
        addEdge(b, a, 1, c);
    }
    addEdge(0, 1, 2, 0);
    addEdge(N, N + 1, 2, 0);
    int cost;
    MCMF(0, N + 1, cost);
    printf("%d\n", cost);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36368339/article/details/80738315