BZOJ 3993: [SDOI2015]星际战争 [二分答案 二分图]

3993: [SDOI2015]星际战争

题意:略


R1D2T1考了裸二分答案+二分图最大匹配...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=105, M=1e5+5, INF=1e9+5;
const double eps=1e-12;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

int n, m, s, t, a[N], b[N], g[55][55], sum;
struct edge{int v, ne; double c, f;}e[M];
int cnt=1, h[N];
inline void ins(int u, int v, double c) { //printf("ins %d %d %lf\n",u,v,c);
	e[++cnt]=(edge){v, h[u], c, 0}; h[u]=cnt;
	e[++cnt]=(edge){u, h[v], 0, 0}; h[v]=cnt;
}
int q[N], head, tail, d[N], vis[N];
bool bfs() { //puts("bfs");
	memset(vis, 0, sizeof(vis));
	head=tail=1;
	q[tail++]=s; d[s]=0; vis[s]=1;
	while(head != tail) {
		int u=q[head++]; //printf("u %d\n",u);
		for(int i=h[u];i;i=e[i].ne) 
			if(!vis[e[i].v] && e[i].c>e[i].f) {
				vis[e[i].v]=1; d[e[i].v]=d[u]+1;
				q[tail++]=e[i].v;
				if(e[i].v == t) return true;
			}
	}
	return false;
}
int cur[N];
double dfs(int u, double a) {
	if(u==t || a==0) return a;
	double flow=0, f;
	for(int &i=cur[u];i;i=e[i].ne) 
		if(d[e[i].v] == d[u]+1 && (f = dfs(e[i].v, min(a, e[i].c-e[i].f)))>0 ) {
			flow += f;
			e[i].f += f;
			e[i^1].f -= f;
			a -= f;
			if(a==0) break;
		}
	if(a) d[u]=-1;
	return flow;
}
double dinic() {
	double flow=0;
	while(bfs()) {
		for(int i=s; i<=t; i++) cur[i]=h[i];
		flow += dfs(s, INF); //printf("flow %lf\n",flow);
	}
	//printf("flow %lf\n", flow);
	return flow;
}

bool check(double mid) {
	cnt=1; memset(h, 0, sizeof(h));
	for(int i=1; i<=m; i++) ins(s, i, b[i]*mid);
	for(int i=1; i<=n; i++) ins(m+i, t, a[i]);
	for(int i=1; i<=m; i++)
		for(int j=1; j<=n; j++) if(g[i][j]) ins(i, m+j, INF);

	double flow=dinic();
	return abs(flow-sum) < eps ? 1 : 0;
}
double l, r;
void solve() {
	while(r-l > 1e-4) {
		double mid=(l+r)/2.0;
		if(check(mid)) r=mid;
		else l=mid;
	}
	printf("%lf", l);
}
int main() {
	freopen("in","r",stdin);
	n=read(); m=read(); s=0; t=n+m+1;
	for(int i=1; i<=n; i++) a[i]=read(), r+=a[i], sum+=a[i];
	for(int i=1; i<=m; i++) b[i]=read();
	for(int i=1; i<=m; i++) for(int j=1; j<=n; j++) g[i][j]=read();
	solve();
}

posted @ 2017-04-06 21:28  Candy?  阅读(263)  评论(0编辑  收藏  举报