最小费用最大流问题----poj 2135


最小费用最大流

网络流的费用: 在实际应用中,与网络流有关的问题,不仅涉及流量,而且还有费用的因素。网络的每一条边(v,w)除了给定容量cap(v,w)外,还定义了一个单位流量费用cost(v,w)。对于网络中一个给定的流flow。

最小费用最大流问题 给定网络G,要求G的在最大用流flow(就是这个图的最大流)的情况下,使流的总费用最小。

其思想与求最大流的增广路算法类似,不断在残流网络中寻找从源s到汇t的最小费用路,即残流网络中从s到t的以费用为权的最短路,然后沿最小费用路增流,直至找到最小费用流。
当残流网络中边(v,w)是向前边时,其费用为cost(v,w); 当(v,w)是向后边时,其费用为-cost(w,v)。

最小费用路算法的复杂度主要依靠于求最短路的方法,由于负权的存在,不会选择dijstra等算法,一般bellman-ford,spfa等用来解决费用流的最短路问题。

poj 2135

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

从1到N,来回是不同路径,等价于求从1到N有两条不同的路径,可以转换化流量的问题,设一个超级源点,超级源点到1的容量是2,费用为0,一个超级汇点,N到超级汇点的容量为2,费用为0。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int MAXN=1010;
struct Edge
{
    int from,to,cap,flow,cost;
};
vector<Edge> edges;
vector<int> g[MAXN];
bool inq[MAXN];
int d[MAXN];
int pre[MAXN];
int a[MAXN];//从源点到当前结点的增加流
int s,t;
int sumflow,sumcost;
void init()
{
    for(int i=0;i<=MAXN;i++)
        g[i].clear();
    edges.clear();
}
void addedge(int from,int to,int cap,int cost)
{
    edges.push_back((Edge){from,to,cap,0,cost});
    edges.push_back((Edge){to,from,0,0,-cost});
    int num=edges.size();
    g[from].push_back(num-2);
    g[to].push_back(num-1);
}
bool spfa()
{
    memset(inq,0,sizeof(inq));
    memset(d,0x3f,sizeof(d));
    memset(a,0x3f,sizeof(a));
    d[s]=0;
    inq[s]=1;
    a[s]=inf;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=0;
        for(int i=0;i<g[u].size();i++)
        {
            Edge e=edges[g[u][i]];
            if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
            {
                d[e.to]=d[u]+e.cost;
                pre[e.to]=g[u][i];
                a[e.to]=min(a[u],e.cap-e.flow);
                if(!inq[e.to])
                {
                    q.push(e.to);
                    inq[e.to]=1;
                }
            }
        }
    }
    if(d[t]==inf)
        return false;

    sumflow+=a[t];
    sumcost+=d[t]*a[t];
    int x=t;
    while(x!=s)
    {
        edges[pre[x]].flow+=a[t];
        edges[pre[x]^1].flow-=a[t];//异或1就是edges中的上一条变
        x=edges[pre[x]].from;
    }
    return true;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        init();
        while(m--)
        {
            int a,b,c;
            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);
        s=0,t=n+1;
        sumflow=0;
        sumcost=0;
        while(spfa());
        cout<<sumcost<<endl;
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值