Farm Tour POJ - 2135 最小费用最大流(模板)

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

#include <cstdio>
#include <queue>
#define min(a,b) (a>b?b:a)
#define MAXV 1100
#define MAXM 40100
#include<string.h>
using namespace std;
typedef struct
{
    int s,t,next,w,r;
} Edge;

Edge edge[MAXM];
int source,sink,n,m,mincost,edgesum;
int head[MAXV],d[MAXV],parent[MAXV],M[MAXV];

void addedge(int a,int b,int c,int r)
{
    edge[edgesum].s=a;
    edge[edgesum].t=b;
    edge[edgesum].r=r;
    edge[edgesum].w=c;
    edge[edgesum].next=head[a];
    head[a]=edgesum++;

    edge[edgesum].s=b;
    edge[edgesum].t=a;
    edge[edgesum].r=0;
    edge[edgesum].w=-c;
    edge[edgesum].next=head[b];
    head[b]=edgesum++;
}

int spfa()
{
    queue <int>q;
    int v,i,tmp;
    bool vis[MAXV];
    for(i=0; i<=sink; i++) d[i]=0x3f3f3f3f;
    memset(vis,false,sizeof(vis));
    memset(parent,-1,sizeof(parent));
    memset(M,0x3f3f3f3f,sizeof(M));
    q.push(source);
    vis[source]=true;
    d[source]=0;
    while(!q.empty())
    {
        v=q.front();
        q.pop();
        vis[v]=false;
        for(i=head[v]; i!=-1; i=edge[i].next)
        {
            tmp=edge[i].t;
            if(edge[i].r && edge[i].w+d[v]<d[tmp])
            {
                d[tmp]=edge[i].w+d[v];
                parent[tmp]=i;
                M[tmp]=min(M[edge[i].s],edge[i].r);
                if(!vis[tmp])
                {
                    q.push(tmp);
                    vis[tmp]=true;
                }
            }
        }
    }
    return 0;
}

void MCMF()
{
    int u;
    mincost=0;
    while(1)
    {
        spfa();
        if(parent[sink]==-1) break;

        u=parent[sink];		//这里不用求出增光路径上的最小流量
        while(u!=-1) 		//因为最小流量一定为1
        {
            edge[u].r-=M[sink];
            edge[u^1].r+=M[sink];
            u=parent[edge[u].s];
        }
        mincost+=d[sink];
    }
}

int main()
{
    int i,a,b,c;
    while(~scanf("%d%d",&n,&m))
    {
        memset(head,-1,sizeof(head));
        edgesum=0,source=0,sink=n+1;
        for(i=1; i<=m; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c,1);
            addedge(b,a,c,1);
        }
        addedge(source,1,0,2);
        addedge(n,sink,0,2);

        MCMF();
        printf("%d\n",mincost);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值