Origin DinicFlow

听说SAP比较流行?
改了下费用流,凑合用吧

#include <set>
#include <ctime>
#include <queue>
#include <cstdio>
#include <bitset>
#include <cctype>
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf (1<<30)
#define INF (1ll<<62)
#define fi first
#define se second
#define rep(x,s,t) for(register int x=s,t_=t;x<t_;++x)
#define per(x,s,t) for(register int x=t-1,s_=s;x>=s_;--x)
#define travel(x) for(int I=last[x],to;I&&(to=e[I].to);I=e[I].nxt)
#define prt(x) cout<<#x<<":"<<x<<" "
#define prtn(x) cout<<#x<<":"<<x<<endl
#define pb(x) push_back(x)
#define hash asfmaljkg
#define rank asfjhgskjf
#define y1 asggnja
#define y2 slfvm
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
template<class T>void sc(T &x){
    int f=1;char c;x=0;
    while(c=getchar(),c<48)if(c=='-')f=-1;
    do x=x*10+(c^48);
    while(c=getchar(),c>47);
    x*=f;
}
template<class T>void nt(T x){
    if(!x)return;
    nt(x/10);
    putchar(x%10+'0');
}
template<class T>void pt(T x){
    if(x<0)putchar('-'),x=-x;
    if(!x)putchar('0');
    else nt(x);
}
template<class T>void ptn(T x){
    pt(x);putchar('\n');
}
template<class T>void pts(T x){
    pt(x);putchar(' ');
}
template<class T>inline void Max(T &x,T y){if(x<y)x=y;}
template<class T>inline void Min(T &x,T y){if(x>y)x=y;}

struct DinicFlow{
    static const int maxv=11000;//include in' and out' 
    static const int maxe=41000;//include reverse'
    int nn;//size of vertices
    int S,T;//source,dest

    int work[maxv];
    int head[maxv],lcnt;
    struct Lane{
        Lane(int a=0,int b=0,int c=0):
            to(a),nxt(b),flow(c){}
        int to,nxt,flow;
    }lane[maxe];

    void init(int st,int en,int tot){
        S=st;T=en;nn=tot;
        memset(head,-1,nn+1<<2);
        lcnt=0;
    }

    int newnode(){
        head[++nn]=-1;
        return nn;
    }

    inline void ins(int u,int v,int f){
        lane[lcnt]=Lane(v,head[u],f);head[u]=lcnt++;
        lane[lcnt]=Lane(u,head[v],0);head[v]=lcnt++;
    }

    int que[maxv*100];//STL?
    int dist[maxv];
    bool bfs(){
        int l,r;l=r=0;
        que[r++]=S;
        rep(i,0,nn+1)dist[i]=-1;
        dist[S]=0;
        while(l<r){
            int pre=que[l++];
            for(register int i=head[pre];~i;i=lane[i].nxt){
                int to=lane[i].to;
                if(lane[i].flow>0&&dist[to]==-1){
                    dist[to]=dist[pre]+1;
                    que[r++]=to;
                }
            }
        }
        return dist[T]!=-1;
    }

    bool vis[maxv];
    int dfs(int x,int flow){
        vis[x]=true;
        if(x==T)return flow;
        for(int y,tmp,&i=work[x];~i;i=lane[i].nxt){
            y=lane[i].to;
            if(lane[i].flow>0&&dist[y]==dist[x]+1&&!vis[y]){
                if(tmp=dfs(y,min(lane[i].flow,flow)),tmp>0){
                    lane[i].flow-=tmp;
                    lane[i^1].flow+=tmp;
                    return tmp;
                }
            }
        }
        return 0;
    }

    int flowpoly(){//ll?
        int ans=0;
        while(bfs()){
            rep(i,0,nn+1)work[i]=head[i];
            for(;;){
                rep(i,0,nn+1)vis[i]=false;
                int flow=dfs(S,inf);
                if(!flow)break;
                ans+=flow;
            }
        }
        return ans;
    }
}df;

int n,m,K;
int main(){
//  freopen("pro.in","r",stdin);
//  freopen("chk.out","w",stdout);
//  int cas;sc(cas);
//  while(cas--)solve();
    sc(n);sc(m);sc(K);

    #define idx(x,y) (x*(m+1)+y+1)
    int S=idx(n,0),T=idx(n,1);
    int tot=T;
    df.init(S,T,tot);

    rep(i,0,n){
        df.ins(S,idx(i,0),inf);
        df.ins(idx(i,m),T,inf);
        rep(j,0,m){
            int a;sc(a);
            df.ins(idx(i,j),idx(i,j+1),a==-1?inf:100-a);
        }
    }
    rep(i,0,K){
        int a,b;
        sc(a);sc(b);
        --a;--b;
        rep(j,0,m)df.ins(idx(a,j),idx(b,j+1),inf);
    }
    printf("%.2f\n",1.0*(100*n-df.flowpoly())/n);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值