zoukankan      html  css  js  c++  java
  • 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1834

    我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的isap错了T_T,可是完全没错啊!!!!

    这题其实第一个问很简单,跑一次最大流即可。第二个问就是在跑完最大流的残量网络上每条边都扩充容量为oo,费用为边的费用,然后设个超级源连一条容量为k的边到点1,再跑一次费用流即可。

    理由很简单,自己想,我就不说了。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << #x << " = " << x << endl
    #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    inline const int max(const int &a, const int &b) { return a>b?a:b; }
    inline const int min(const int &a, const int &b) { return a<b?a:b; }
    
    const int N=1010, M=2000000, oo=~0u>>1;
    int ihead[N], cnt=1, cur[N], gap[N], d[N], p[N], n, m, k, vis[N], q[N], front, tail, nd[M][3];
    struct ED { int from, to, cap, w, next; } e[M];
    inline void add(const int &u, const int &v, const int &c, const int &w) {
    	e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].from=u; e[cnt].cap=c; e[cnt].w=w;
    	e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].from=v; e[cnt].cap=0; e[cnt].w=-w;
    }
    int isap(const int &s, const int &t, const int &n) {
    	for1(i, 0, n) cur[i]=ihead[i];
    	int ret=0, i, f, u=s;
    	gap[0]=n;
    	while(d[s]<n) {
    		for(i=cur[u]; i; i=e[i].next) if(e[i].cap && d[u]==d[e[i].to]+1) break;
    		if(i) {
    			p[e[i].to]=cur[u]=i; u=e[i].to;
    			if(u==t) {
    				for(f=oo; u!=s; u=e[p[u]].from) f=min(f, e[p[u]].cap);
    				for(u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-=f, e[p[u]^1].cap+=f;
    				ret+=f;
    			}
    		}
    		else {
    			if(! (--gap[d[u]]) ) break;
    			d[u]=n; cur[u]=ihead[u];
    			for(i=ihead[u]; i; i=e[i].next) if(e[i].cap && d[u]>d[e[i].to]+1) d[u]=d[e[i].to]+1;
    			++gap[d[u]];
    			if(u!=s) u=e[p[u]].from;
    		}
    	}
    	return ret;
    }
    inline const bool spfa(const int &s, const int &t) {
    	for1(i, s, t) d[i]=1000000000, vis[i]=0;
    	vis[s]=1; d[s]=front=tail=0; q[tail++]=s;
    	int u, v, i;
    	while(front!=tail) {
    		u=q[front++]; if(front==N) front=0;
    		for(i=ihead[u]; i; i=e[i].next) if(e[i].cap && d[v=e[i].to]>d[u]+e[i].w) {
    			d[v]=d[u]+e[i].w; p[v]=i;
    			if(!vis[v]) {
    				vis[v]=1, q[tail++]=v;
    				if(tail==N) tail=0;
    			}
    		}
    		vis[u]=0;
    	}
    	return d[t]!=1000000000;
    }
    int mcf(const int &s, const int &t) {
    	int ret=0, f, u;
    	while(spfa(s, t)) {
    		for(f=oo, u=t; u!=s; u=e[p[u]].from) f=min(f, e[p[u]].cap);
    		for(u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-=f, e[p[u]^1].cap+=f;
    		ret+=d[t]*f;
    	}
    	return ret;
    }
    int main() {
    	read(n); read(m); read(k);
    	int s=1, t=n, u, v, c;
    	for1(i, 1, m) {
    		read(u); read(v); read(c); read(nd[i][2]);
    		add(u, v, c, 0);
    		nd[i][0]=u; nd[i][1]=v;
    	}
    	printf("%d ", isap(s, t, t+1));
    	for1(i, 1, m) add(nd[i][0], nd[i][1], oo, nd[i][2]);
    	s=0; add(s, 1, k, 0);
    	print(mcf(s, t));
    	return 0;
    }
    

    Description

    给定一张有向图,每条边都有一个容量C和一个扩容费用W。这里扩容费用是指将容量扩大1所需的费用。求: 1、 在不扩容的情况下,1到N的最大流; 2、 将1到N的最大流增加K所需的最小扩容费用。

    Input

    输入文件的第一行包含三个整数N,M,K,表示有向图的点数、边数以及所需要增加的流量。 接下来的M行每行包含四个整数u,v,C,W,表示一条从u到v,容量为C,扩容费用为W的边。

    Output

    输出文件一行包含两个整数,分别表示问题1和问题2的答案。

    Sample Input

    5 8 2
    1 2 5 8
    2 5 9 9
    5 1 6 2
    5 1 1 8
    1 2 8 7
    2 5 4 9
    1 2 1 1
    1 4 2 1

    Sample Output

    13 19
    30%的数据中,N<=100
    100%的数据中,N<=1000,M<=5000,K<=10

    HINT

    Source

  • 相关阅读:
    微软校园招聘  研发工程师A
    版本号排序
    腾讯2018校园招聘  研发工程师笔试题(三)
    好词好句
    HikariCP 连接最快的连接池
    Ubuntu 18.04.1 安装java8
    Ubuntu 18.04.1 安装mysql 5.7.27
    markdown 插入链接
    面试题 int(3) int(10) 区别
    采购单(京东2017秋招真题)
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3934643.html
Copyright © 2011-2022 走看看