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

来源:互联网 发布:unity3d 关闭垂直同步 编辑:程序博客网 时间:2024/04/20 13:37


最小费用最大流

网络流的费用: 在实际应用中,与网络流有关的问题,不仅涉及流量,而且还有费用的因素。网络的每一条边(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 51 2 12 3 13 4 11 3 22 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 longusing 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
原创粉丝点击