bzoj 1570: [JSOI2008]Blue Mary的旅行

题解:

很裸的二分+网络流分层图。
我是将每条边连向目标的下一天。
最后ans-1,因为其实第一天飞第一天就到了。
code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
const int inf=(1<<28);
using namespace std;
struct node{
    int x,y,next,c,other;
}a[6400000],e[2460];int last[100000],len=0;
int h[100000],s[100000],st,ed;
void ins(int x,int y,int c)
{
    int k1=++len;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=len;
    int k2=++len;
    a[len].x=y;a[len].y=x;a[len].c=0;
    a[len].next=last[y];last[y]=len;
    a[k1].other=k2;a[k2].other=k1;
}
bool bt_h()
{
    memset(h,0,sizeof(h));
    int l=1,r=2;s[l]=st;h[st]=1;
    while(l!=r)
    {
        int x=s[l];
        for(int i=last[x];i;i=a[i].next)
        {
            int y=a[i].y;
            if(h[y]==0&&a[i].c>0) h[y]=h[x]+1,s[r++]=y;
        }
        l++;
    }
    return h[ed]!=0;
}
int findflow(int x,int f)
{
    if(x==ed) return f;
    int t,ans=0;
    for(int i=last[x];i;i=a[i].next)
    {
        int y=a[i].y;
        if(h[x]+1==h[y]&&a[i].c>0&&ans<f)
        {
            ans+=(t=findflow(y,min(a[i].c,f-ans)));
            a[i].c-=t;a[a[i].other].c+=t;
        }
    }
    if(ans==0) h[x]=0;
    return ans;
}
int n,m,p;
bool check(int X)
{
    len=0;memset(last,0,sizeof(last));
    st=0;ed=n*X+1;
    for(int d=1;d<=X;d++)
    {
        if(d!=X)
            for(int i=1;i<=n;i++)
                ins((i-1)*X+d,(i-1)*X+d+1,inf);
        for(int i=1;i<=m;i++)
        {
            if(d==X) break;
            int x=e[i].x,y=e[i].y;
            ins((x-1)*X+d,(y-1)*X+d+1,e[i].c);
        }
        ins((n-1)*X+d,ed,inf);
    }
    ins(st,1,p);
    int ans=0;
    while(bt_h()) ans+=findflow(st,inf);
    return ans==p;
}
int main()
{
    scanf("%d %d %d",&n,&m,&p);
    for(int i=1;i<=m;i++) scanf("%d %d %d",&e[i].x,&e[i].y,&e[i].c);
    int l=1,r=n+p,ans;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d",ans-1);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值