bzoj 3931: [CQOI2015]网络吞吐量

来源:互联网 发布:dlp数据加密技术 编辑:程序博客网 时间:2024/06/05 02:01

这题考语文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 longusing 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);}




原创粉丝点击