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

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.

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

6

Hint 

The edge is undirected.

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

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

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

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

3:N到sink建边,容量为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;
}


 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

童话ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值