Power OJ 1683(最小费用最大流)

来源:互联网 发布:淘宝内衣买家秀看不到 编辑:程序博客网 时间:2024/05/23 13:10

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 ofwhich contains the big barn. A total M (1 <= M <= 10000) paths that connectthe fields in various ways. Each path connects two different fields and has anonzero length smaller than 35,000. To show off his farm in the best way, hewalks a tour that starts at his house, potentially travels through some fields,and ends at the barn. Later, he returns (potentially through some fields) backto his house again. He wants his tour to be as short as possible, however hedoesn't want to walk on any given path more than once. Calculate the shortesttour 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: Thestarting field, the end field, and the path's length.

Output 

A single line containing the length ofthe shortest tour.

Sample Input

4 5 1 2 1 2 3 1 3 4 1 1 3 2 2 4 2

Sample Output

6

Hint 

The edge is undirected.

题意:给你n个点,也就是农田,m条无向边以及每条边长度,让你从1到n再到1,要求不能走重复的边,问你所走路径总长的最小值。

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

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

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

3:Nsink建边,容量为2,费用为0

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

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
const int maxm=50004;
const int INF=0x3f3f3f3f;
int vis[maxn],dis[maxn],pre[maxn],head[maxn];
int n,m,tot=0;
struct node
{
    int to,next,flow,cost;
    node(){}
    node(int to,int next,int flow,int cost):to(to),next(next),flow(flow),cost(cost){}//这里没有用cap也就是容量上限,因此flow表示剩余流量
}edge[maxm];
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w,int cost)

{
    edge[tot]=node(v,head[u],w,cost);//倒着计算的,所以反向边剩余流量为w,代价为cost
    head[u]=tot++;
    edge[tot]=node(u,head[v],0,-cost);//正向边剩余流量为0,代价相应的就是-cost
    head[v]=tot++;
}
int spfa(int st,int en)//跑一遍spfa求出可行的最短路
{
    memset(vis,0,sizeof(vis));
    memset(dis,INF,sizeof(dis));
    dis[st]=0;
    vis[st]=1;
    queue<int>q;
    q.push(st);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].flow==0) continue;//当前边剩余流量为0跳过
            if(dis[v]>dis[u]+edge[i].cost)//spfa松弛
            {
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])//标记是否走过
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return dis[en]!=INF;如果为INF返回0,说明没走过,反之返回1
}
int FM(int st,int en)
{
    int ans=0;
    while(spfa(st,en))
    {
        int flow=INF;
        for(int i=en;i!=st;i=edge[pre[i]^1].to)//倒着计算,理解清楚正向边和反向边关系,也就是正向边和反向边为抑或关系,可以自己手推验证一下
        {
            flow=min(flow,edge[pre[i]].flow);
        }
        for(int i=en;i!=st;i=edge[pre[i]^1].to)
        {
            edge[pre[i]].flow-=flow;
            edge[pre[i]^1].flow+=flow;
            ans+=flow*edge[pre[i]].cost;//流量*代价就是最后的答案
        }
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        int u,v,w;
        for(int i=1;i<=m;i++)//无向边
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,1,w);//初始流量为1
            add(v,u,1,w);
        }
        int source=0,sink=n+1;
        add(source, 1, 2, 0);//超级源点连起点
        add(n,sink, 2, 0);//终点连超级汇点
        int ans=FM(0,n+1);
        printf("%d\n",ans);
    }
    return 0;
}


0 0
原创粉丝点击