zoukankan      html  css  js  c++  java
  • BZOJ 3931 Dijkstra+网络流

    思路:
    (我能说按照题意模拟么)

    用long long
    inf 要开大…….

    //By SiriusRen
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define int long long
    #define N 1017
    #define M 999999
    #define inf 0x3f3f3f3f3f3f3f3fll
    int n,m,xx[M],yy[M],zz[M],first[N],next[M],v[M],w[M],tot,dis[N],vis[N],tmp,ans;
    struct Node{int wei,now;}jy;
    bool operator < (Node a,Node b){return a.wei>b.wei;}
    void Add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
    struct Dijkstra{
        void add(int x,int y,int z){Add(x,y,z),Add(y,x,z);}
        void solve(){
            memset(dis,0x3f,sizeof(dis)),dis[1]=0;
            priority_queue<Node>pq;jy.now=1,pq.push(jy);
            while(!pq.empty()){
                Node t=pq.top();pq.pop();
                if(vis[t.now])continue;
                vis[t.now]=1;
                for(int i=first[t.now];~i;i=next[i])
                    if(dis[v[i]]>dis[t.now]+w[i])
                        dis[v[i]]=dis[t.now]+w[i],
                        jy.now=v[i],jy.wei=dis[v[i]],pq.push(jy);
            }
        }
    }dijk;
    struct Dinic{
        void add(int x,int y,int z){Add(x,y,z),Add(y,x,0);}
        bool tell(){
            memset(vis,-1,sizeof(vis)),vis[1]=0;
            queue<int>q;q.push(1);
            while(!q.empty()){
                int t=q.front();q.pop();
                for(int i=first[t];~i;i=next[i])
                    if(!~vis[v[i]]&&w[i])
                        vis[v[i]]=vis[t]+1,q.push(v[i]);
            }return ~vis[2*n];
        }
        int zeng(int x,int y){
            if(x==2*n)return y;
            int r=0;
            for(int i=first[x];~i;i=next[i])
                if(vis[v[i]]==vis[x]+1&&w[i]){
                    int t=zeng(v[i],min(y-r,w[i]));
                    w[i]-=t,w[i^1]+=t,r+=t;
                }
            if(!r)vis[x]=-1;
            return r;
        }
        void flow(){
            while(tell())while(tmp=zeng(1,inf))ans+=tmp;
            printf("%lld
    ",ans);
        }
    }dinic;
    void init(){memset(first,-1,sizeof(first)),tot=0;}
    signed main(){
        init(),scanf("%lld%lld",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%lld%lld%lld",&xx[i],&yy[i],&zz[i]);
            dijk.add(xx[i],yy[i],zz[i]);
        }
        dijk.solve(),init();
        for(int i=1;i<=m;i++){
            if(dis[yy[i]]==dis[xx[i]]+zz[i])dinic.add(xx[i]+n,yy[i],inf);
            if(dis[xx[i]]==dis[yy[i]]+zz[i])dinic.add(yy[i]+n,xx[i],inf);
        }
        for(int i=1;i<=n;i++){
            scanf("%lld",&tmp);
            if(i!=1&&i!=n)dinic.add(i,i+n,tmp);
            else dinic.add(i,i+n,inf);
        }
        dinic.flow();
    }

    这里写图片描述

  • 相关阅读:
    Poj3126
    Poj1426
    2806 红与黑
    3100 蜗牛
    1225 八数码难题
    2549 自然数和分解
    2547 东方辉针城
    2928 你缺什么
    1629 01迷宫
    1029 遍历问题
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532113.html
Copyright © 2011-2022 走看看