[bzoj1877][网络流-费用流]晨跑

Elaxia是一位空手道爱好者,他希望通过晨跑来增强体质。本篇博客介绍了一个晨跑路线规划问题:如何设计晨跑路线以确保训练周期内不重复且总路程最短。通过使用拆点网络流和费用流算法,成功解决了这一问题。

Description

Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑、仰卧起坐等 等,不过到目前为止,他 坚持下来的只有晨跑。
现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街道,Elaxia只能从 一
个十字路口跑向另外一个十字路口,街道之间只在十字路口处相交。Elaxia每天从寝室出发 跑到学校,保证寝室 编号为1,学校编号为N。
Elaxia的晨跑计划是按周期(包含若干天)进行的,由于他不喜欢走重复的路线,所以
在一个周期内,每天的晨跑路线都不会相交(在十字路口处),寝室和学校不算十字路 口。Elaxia耐力不太好,
他希望在一个周期内跑的路程尽量短,但是又希望训练周期包含的天 数尽量长。 除了练空手道,Elaxia其他时间
都花在了学习和找MM上面,所有他想请你帮忙为他设计 一套满足他要求的晨跑计划。

Input

第一行:两个数N,M。表示十字路口数和街道数。 接下来M行,每行3个数a,b,c,表示路口a和路口b之间有条长度为c的街道(单向)。 N
≤ 200,M ≤ 20000。

Output

两个数,第一个数为最长周期的天数,第二个数为满足最长天数的条件下最短的路程长 度。

Sample Input

7 10
1 2 1
1 3 1
2 4 1
3 4 1
4 5 1
4 6 1
2 5 5
3 6 6
5 7 1
6 7 1

Sample Output

2 11

题解

拆点网络流+费用流,哈哈哈哈哈我好开心啊自己A的
十字路口拆点(in,out) in到out连一条流量为1,费用为0的边
st拆点(p,q) p到q连一条流量为inf,费用为0的边
ed拆点(l,r) l到r连一条流量为inf,费用为0的边
题面给出的边(x,y)的话 用xout到yin 连一条流量为1,费用为c的边
c为给出的长度
这样跑完网络流后再跑一次费用流就切掉啦

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
    int x,y,c,d,next,other,pp;
}a[81000];int len,last[2100];
void ins(int x,int y,int c,int d)
{
    int k1,k2;
    k1=++len;
    a[len].x=x;a[len].y=y;a[len].c=a[len].pp=c;a[len].d=d;
    a[len].next=last[x];last[x]=len;

    k2=++len;
    a[len].x=y;a[len].y=x;a[len].c=a[len].pp=0;a[len].d=-d;
    a[len].next=last[y];last[y]=len;
    a[k1].other=k2;
    a[k2].other=k1;
}
int list[81000],head,tail;
int h[81000],st,ed;
bool bt_h()
{
    list[1]=st;head=1;tail=2;
    memset(h,0,sizeof(h));
    h[st]=1;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(h[y]==0 && a[k].c>0)
            {
                h[y]=h[x]+1;
                list[tail++]=y;
            }
        }
        head++;
    }
    if(h[ed]>0)return true;
    return false;
}
int findflow(int x,int f)
{
    if(x==ed)return f;
    int s=0,t;
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(h[y]==h[x]+1 && a[k].c>0 && s<f)
        {
            s+=(t=findflow(y,min(a[k].c,f-s)));
            a[k].c-=t;a[a[k].other].c+=t;
        }
    }
    if(s==0)h[x]=0;
    return s;
}
int n,m;
bool v[2100];
int d[2100],pre[2100],pos[2100];
bool spfa()
{
    for(int i=1;i<=2*n;i++)d[i]=999999999;
    d[st]=0;memset(v,false,sizeof(v));v[st]=true;
    head=1;tail=2;list[1]=st;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(d[y]>d[x]+a[k].d && a[k].pp>0)
            {
                d[y]=d[x]+a[k].d;
                pre[y]=x;pos[y]=k;
                if(v[y]==false)
                {
                    v[y]=true;
                    list[tail++]=y;
                    if(tail==2*n+1)tail=1;
                }
            }
        }
        head++;if(head==2*n+1)head=1;
        v[x]=false;
    }
    if(d[ed]==999999999)return false;
    return true;
}
int main()
{
    scanf("%d%d",&n,&m);
    len=0;memset(last,0,sizeof(last));
    st=1;ed=n+n;
    for(int i=2;i<n;i++)ins(i,i+n,1,0);
    ins(1,1+n,999999999,0);ins(n,n+n,999999999,0);
    for(int i=1;i<=m;i++)
    {
        int x,y,c;
        scanf("%d%d%d",&x,&y,&c);
        ins(x+n,y,1,c);
    }
    int ans=0;
    while(bt_h())ans+=findflow(st,999999999);
    printf("%d ",ans);
    ans=0;
    while(spfa())
    {
        int x=ed;
        while(x!=st)
        {
            ans+=a[pos[x]].d;
            a[pos[x]].pp--;
            a[a[pos[x]].other].pp++;
            x=pre[x];
        }
    }
    printf("%d\n",ans);
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值