poj 2135 Farm Tour 【最小费用最大流】

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13464   Accepted: 5101

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


题意:给你N个农田、M条无向边以及每条边的长度。现在让你从1走到N再从N走回1,要求不能走重复的边。问你所走的路径总长的最小值。题目保证1到N至少会存在两条边不重复的路径。


很简单的题目,就不多说了。


建图:超级源点source,超级汇点sink

1,source连点1,容量为2,费用为0;

2,对题目给出的无向边建双向边,容量为1(意味着该边只能走一次),费用为边的长度;

3,N到sink建边,容量为2,费用为0。

最后跑一次最小费用最大流就可以了。



AC代码:数据不会超int


#include 
#include 
#include 
#include 
#include 
#include 
#define MAXN 1010
#define MAXM 50000+10
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
struct Edge
{
    int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int dist[MAXN];
int pre[MAXN];
bool vis[MAXN];
int N, M;
int source, sink;
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++;
}
void getMap()
{
    int a, b, c;
    source = 0, sink = N+1;
    while(M--)
    {
        scanf("%d%d%d", &a, &b, &c);
        addEdge(a, b, 1, c);//建双向边
        addEdge(b, a, 1, c);
    }
    addEdge(source, 1, 2, 0);//超级源点连起点
    addEdge(N, sink, 2, 0);//终点连超级汇点
}
bool SPFA(int s, int t)
{
    queue 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()
{
    while(scanf("%d%d", &N, &M) != EOF)
    {
        init();
        getMap();
        int cost;//最小费用
        MCMF(source, sink, cost);
        printf("%d\n", cost);
    }
    return 0;
}


你可能感兴趣的:(网络流)