【POJ 2135 Farm】网络流 & 最小费用最大流

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17964 Accepted: 6949
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 个景点,一个人从 1 号景点走到 n 号景点,再从 n 号景点走到 1 号景点(不能重复,不必走完所有的景点,只要从 1 到 n 就行),给出一些景点间的路径长度,问最短需要走多少路

思路 : 最小费用最大流
最小费用就是路径长度的总和,最大流就是来回的 两条路。
由于去和回来可以看成:2条从1到n的不同的路。 所以转化成求从1到n的两条不同的路。
建一个源点 s -> 1 ,费用为 0,流为 2
同理建一个汇点,n -> e,费用为 0,流为 2
x和y之间已有容量为 z 的路,按最小费用流建图
x -> y,费用为 z,流为 1
y -> x,,费用为 z,流为 1
这样求得的最小费用最大流的最小费用就是最短路径的长度

AC代码:

#include<cstdio>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 5e5 + 10;
const int INF = (1e9 + 7) / 2;
typedef long long LL;
int head[MAX],nl;
struct node{
    int from,to,cost,flow,next;
}st[MAX];
void add(int x,int y,int z,int a){
    st[nl] = node{x,y,z,a,head[x]},head[x] = nl++;
    st[nl] = node{y,x,-z,0,head[y]},head[y] = nl++;
}
void init(){
    nl = 0;
    memset(head,-1,sizeof head);
}
int n,m,vis[MAX],p[MAX],w[MAX];
bool DFS(int s,int t){
    memset(vis,0,sizeof vis);
    memset(w,-1,sizeof w);
    fill(p,p + MAX,INF);
    p[s] = 0,vis[s] = 1;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int o = q.front();
        q.pop();
        vis[o] = 0;
        for(int i = head[o]; i != -1; i = st[i].next){
            int a = st[i].to;
            if(p[a] > p[o] + st[i].cost && st[i].flow){
                p[a] = p[o] + st[i].cost;
                w[a] = i;
               if(!vis[a]){
                    vis[a] = 1;
                    q.push(a);
               }
            }
        }
    }
    return w[t] != -1;
}
int Mincost(int s,int t){
    int Maxflow = 0,cost = 0;
    while(DFS(s,t)){
        int flow = INF;
        for(int i = w[t]; i != -1; i = w[st[i].from]){
            if(flow > st[i].flow)
                flow = st[i].flow;
        }
        for(int i = w[t]; i != -1; i = w[st[i].from]){
            st[i].flow -= flow;
            st[i ^ 1].flow += flow;
            cost += flow * st[i].cost;
        }
        Maxflow += flow;
    }
    return cost;
}
int main()
{
    while(~scanf("%d %d",&n,&m)){
        init();
        int s = 0,e = n + 1;
        add(s,1,0,2);
        add(n,e,0,2);
        while(m--){
            int x,y,z;
            scanf("%d %d %d",&x,&y,&z);
            add(x,y,z,1);
            add(y,x,z,1);
        }
        printf("%d\n",Mincost(s,e));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值