bzoj3931 [CQOI2015]网络吞吐量(Dijkstra+最大流)

先Dijkstra求出dis数组,然后判断每条边是否可以经过,如果可以就建边,容量为inf。点权限制,拆点。最大流就是答案了。

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 1010
#define pa pair
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,h[N],num=1,cur[N],lev[N],S,T;
ll dis[N],ans=0;
bool inq[N];
struct edge{
    int fr,to,next,val;
}data[410000];
inline void add(int x,int y,int val){
    data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;
    data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].val=0;
}
inline void add1(int x,int y,int val){
    data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;
    data[num].fr=x;++num;
}
inline bool bfs(){
    queue<int>q;memset(lev,0,sizeof(lev));
    q.push(S);lev[S]=1;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=h[x];i;i=data[i].next){
            int y=data[i].to;if(lev[y]||!data[i].val) continue;
            lev[y]=lev[x]+1;if(y==T) return 1;q.push(y);
        }
    }return 0;
}
inline ll dinic(int x,ll low){
    if(x==T) return low;ll tmp=low;
    for(int &i=cur[x];i;i=data[i].next){
        int y=data[i].to;if(lev[y]!=lev[x]+1||!data[i].val) continue;
        ll res=dinic(y,min(tmp,(ll)data[i].val));
        if(!res) lev[y]=0;else tmp-=res,data[i].val-=res,data[i^1].val+=res;
        if(!tmp) return low;
    }return low-tmp;
}
inline void Dijkstra(){
    priority_queuevector,greater >q;
    memset(dis,127,sizeof(dis));
    dis[1]=0;q.push(make_pair(dis[1],1));
    while(!q.empty()){
        int x=q.top().second;q.pop();
        if(inq[x]) continue;inq[x]=1;
        for(int i=h[x];i;i=data[i].next){
            int y=data[i].to;
            if(dis[x]+data[i].valint main(){
//  freopen("a.in","r",stdin);
    n=read();m=read();S=1+n;T=n;
    for(int i=1;i<=m;++i){
        int x=read(),y=read(),val=read();
        add1(x,y,val);add1(y,x,val);
    }Dijkstra();int num1=num;num=1;memset(h,0,sizeof(h));
    for(int i=2;i<=num1;i+=2){
        int x=data[i].fr,y=data[i].to;
        if(dis[x]+data[i].val==dis[y]) add(x+n,y,inf);
    }for(int i=1;i<=n;++i) add(i,i+n,read());
    while(bfs()){memcpy(cur,h,sizeof(h));ans+=dinic(S,1LL<<60);}
    printf("%lld\n",ans);
    return 0;
}

你可能感兴趣的:(bzoj,最短路,-----网络流-------,最大流)