bzoj 3931: [CQOI2015]网络吞吐量

这题考语文2333

其实是源点到汇点的流量一定要延最短路,可以有多条,求最大流。

要开long long。

code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define pa pair<int,int>
#define LL long long
using namespace std;
const LL inf=(1LL<<40);
struct node{
    int x,y,next,other;
    LL c;
}b[200010],a[2110000];
int last[2010],h[2010],s[2010],st,ed,len=0;
int n,m;
LL v[510];
bool u[510];
void ins1(int x,int y,LL c)
{
    b[++len].x=x;b[len].y=y;b[len].c=c;
    b[len].next=last[x];last[x]=len;
}
void ins(int x,int y,LL 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;
}
LL findflow(int x,LL f)
{
    if(x==ed) return f;
    LL 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;
}
void dijkstra()
{
    priority_queue<pa,vector<pa>,greater<pa> >q;
    memset(v,127,sizeof(v));v[st]=0LL;
    memset(u,false,sizeof(u));
    q.push(make_pair(0,1));
    while(!q.empty())
    {
        int x=q.top().second;q.pop();
        if(u[x]) continue;
        u[x]=true;
        for(int i=last[x];i;i=b[i].next)
        {
            int y=b[i].y;
            if(v[y]>v[x]+b[i].c)
            {
                v[y]=v[x]+b[i].c;
                q.push(make_pair(v[y],y));
            }
        }
    }
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y;LL c;scanf("%d %d %lld",&x,&y,&c);
        ins1(x,y,c);ins1(y,x,c);
    }
    st=1;ed=n;dijkstra();
    int tot=len;
    memset(last,0,sizeof(last));len=0;
    for(int i=1;i<=n;i++)
    {
        LL x;scanf("%lld",&x);
        if(i==1||i==n) x=inf;
        ins(i,i+n,x);
    }
    for(int i=1;i<=tot;i++)
        if(v[b[i].x]+b[i].c==v[b[i].y])
            ins(b[i].x+n,b[i].y,inf);
    LL ans=0;st=1;ed=2*n;
    while(bt_h()){ans+=findflow(st,inf);}
    printf("%lld",ans);
}



  
  


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值