1 条题解

  • 0
    @ 2021-6-15 14:30:32

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    #define pb push_back
    #define mp make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    #define SZ(x) ((int)(x).size())
    typedef vector<int> VI;
    typedef long long ll;
    typedef pair<int,int> PII;
    const ll mod=1000000007;
    ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
    ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
    // head
    
    const int N=40,M=2010,uinf=1<<30;
    int n,m,c,ty[N][N],u[N][N],l[N][N],v[N][N],uc[N][N],lc[N][N],vc[N][N],cul[N][N],cuu[N][N],bel[M];
    pair<int,PII> val[M];
    
    namespace flow {
    	const int V=2010,E=20100;
    	ll inf=1ll<<60;
    	int q[V*30],vis[V],fst[V],pre[V],nxt[E],y[E],f[E],S,T,flow,tot,tn;
    	ll dis[V],c[E],cost;
    	void init(int s,int t,int Tn) {
    		tot=1; tn=Tn;
    		rep(i,0,tn) fst[i]=0;
    		S=s;T=t;
    	}
    	void add(int u,int v,int ff,ll cc) {
    		tot++;y[tot]=v;nxt[tot]=fst[u];f[tot]=ff;c[tot]=cc;fst[u]=tot;
    		tot++;y[tot]=u;nxt[tot]=fst[v];f[tot]=0;c[tot]=-cc;fst[v]=tot;
    	}
    	bool spfa() {
    		rep(i,0,tn) dis[i]=inf,vis[i]=0,pre[i]=0;
    		dis[S]=0;q[0]=S;vis[S]=1;
    		int t=1;
    		rep(i,0,t) {
    			int u=q[i];
    			for (int j=fst[u];j;j=nxt[j]) {
    				int v=y[j];
    				if (f[j]&&dis[v]>dis[u]+c[j]) {
    					dis[v]=dis[u]+c[j];
    					pre[v]=j;
    					if (!vis[v]) vis[v]=1,q[t++]=v;
    				}
    			}
    			vis[u]=0;
    		}
    		return dis[T]!=inf;
    	}
    	void augment() {
    		int p=T; int _f=(1<<30);
    		while (pre[p]) _f=min(_f,f[pre[p]]),p=y[pre[p]^1];
    		flow+=_f;cost+=_f*dis[T];
    		p=T;
    		while (pre[p]) f[pre[p]]-=_f,f[pre[p]^1]+=_f,p=y[pre[p]^1];
    	}
    	void solve() {
    		flow=0,cost=0;
    		while (spfa()) augment();
    	}
    }
    
    
    void input() {
    	scanf("%d%d",&n,&m);
    	rep(i,0,n) rep(j,0,m) scanf("%d",&ty[i][j]);
    	rep(i,0,n) rep(j,0,m) {
    		if (ty[i][j]==1) scanf("%d",&u[i][j]);
    		else if (ty[i][j]==2) scanf("%d",&l[i][j]);
    		else if (ty[i][j]==3) scanf("%d%d",&u[i][j],&l[i][j]);
    		else if (ty[i][j]==4) scanf("%d",&v[i][j]);
    	}
    	rep(i,0,n) rep(j,0,m) {
    		if (ty[i][j]==1) scanf("%d",&uc[i][j]);
    		else if (ty[i][j]==2) scanf("%d",&lc[i][j]);
    		else if (ty[i][j]==3) scanf("%d%d",&uc[i][j],&lc[i][j]);
    		else if (ty[i][j]==4) scanf("%d",&vc[i][j]);
    	}
    	rep(i,0,n) rep(j,0,m) if (l[i][j]!=0) {
    		val[c]=mp(l[i][j],mp(lc[i][j],0));
    		while (j+1<m&&ty[i][j+1]==4) cul[i][j+1]=c,j++;
    		c++;
    	}
    	rep(j,0,m) rep(i,0,n) if (u[i][j]!=0) {
    		val[c]=mp(u[i][j],mp(uc[i][j],1));
    		while (i+1<n&&ty[i+1][j]==4) cuu[i+1][j]=c,i++;
    		c++;
    	}
    }
    
    void addf(int u,int v,int w) {
    	bel[u]-=w; bel[v]+=w;
    }
    void gao() {
    	flow::init(c+1,c+2,c+3);
    	rep(i,0,c) {
    		if (val[i].se.se==0) {
    			addf(c,i,val[i].fi);
    			if (val[i].se.fi!=-1)
    				flow::add(c,i,uinf,val[i].se.fi),
    				flow::add(i,c,val[i].fi-1,val[i].se.fi);
    		}
    		if (val[i].se.se==1) {
    			addf(i,c,val[i].fi);
    			if (val[i].se.fi!=-1)
    				flow::add(i,c,uinf,val[i].se.fi),
    				flow::add(c,i,val[i].fi-1,val[i].se.fi);
    		}
    	}
    	rep(i,0,n) rep(j,0,m) if (ty[i][j]==4) {
    		addf(cul[i][j],cuu[i][j],v[i][j]);
    		if (vc[i][j]!=-1)
    			flow::add(cul[i][j],cuu[i][j],uinf,vc[i][j]),
    			flow::add(cuu[i][j],cul[i][j],v[i][j]-1,vc[i][j]);
    	}
    	int sf=0;
    	rep(i,0,c+1) if (bel[i]>=0) sf+=bel[i],flow::add(c+1,i,bel[i],0);
    		else flow::add(i,c+2,-bel[i],0);
    	flow::solve();
    	if (sf!=flow::flow) puts("-1");
    	else printf("%lld\n",flow::cost);
    }
    int main() {
    	input();
    	gao();
    }
    
    • 1

    信息

    ID
    1014
    时间
    1000ms
    内存
    512MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者