UVa 12534 Binary Matrix 2 zkw费用流模版题

题目链接:点击打开链接

思路:

我们首先假设这个图都是全0的

用n个点代表行,m个点代表列

用源点向行连一个值x 表示每行1的个数,向列连一个y表示每列y个1

则若行i和列j之间流过一个流量就表示 (i,j) 点填了1

那么若原来图中(i,j)点为0 则花费就是1

若原图中(i,j)点是1,则花费是-1

如此枚举x跑个费用流就好了

==居然把我多年的白书费用流坑掉了。。。

zkw走起啊

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
#define maxn 110
#define maxm 100000
#define inf 0x3f3f3f3f
struct MaxFlow
{
	int size, n;
	int st, en, maxflow, mincost;
	bool vis[maxn];
	int net[maxn], pre[maxn], cur[maxn], dis[maxn];
	std::queue <int> Q;
	struct EDGE
	{
		int v, cap, cost, next;
		EDGE(){}
		EDGE(int a, int b, int c, int d)
		{
			v = a, cap = b, cost = c, next = d;
		}
	}E[maxm<<1];
	void init(int _n)
	{
		n = _n, size = 0;
		memset(net, -1, sizeof(net));
	}
	void add(int u, int v, int cap, int cost)
	{
		E[size] = EDGE(v, cap, cost, net[u]);
		net[u] = size++;
		E[size] = EDGE(u, 0, -cost, net[v]);
		net[v] = size++;
	}
	bool adjust()
	{
		int v, min = inf;
		for(int i = 0; i <= n; i++)
		{
			if(!vis[i])
				continue;
			for(int j = net[i]; v = E[j].v, j != -1; j = E[j].next)
				if(E[j].cap)
					if(!vis[v] && dis[v]-dis[i]+E[j].cost < min)
						min = dis[v] - dis[i] + E[j].cost;
		}
		if(min == inf)
			return false;
		for(int i = 0; i <= n; i++)
			if(vis[i])
				cur[i] = net[i], vis[i] = false, dis[i] += min;
		return true;
	}
	int augment(int i, int flow)
	{
		if(i == en)
		{
			mincost += dis[st] * flow;
			maxflow += flow;
			return flow;
		}
		vis[i] = true;
		for(int j = cur[i], v; v = E[j].v, j != -1; j = E[j].next)
		{
			if(!E[j].cap)
				continue;
			if(vis[v] || dis[v]+E[j].cost != dis[i])
				continue;
			int delta = augment(v, std::min(flow, E[j].cap));
			if(delta)
			{
				E[j].cap -= delta;
				E[j^1].cap += delta;
				cur[i] = j;
				return delta;
			}
		}
		return 0;
	}
	void spfa()
	{
		int u, v;
		for(int i = 0; i <= n; i++)
			vis[i] = false, dis[i] = inf;
		dis[st] = 0;
		Q.push(st);
		vis[st] = true;
		while(!Q.empty())
		{
			u = Q.front(), Q.pop();
			vis[u] = false;
			for(int i = net[u]; v = E[i].v, i != -1; i = E[i].next)
			{
				if(!E[i].cap || dis[v] <= dis[u] + E[i].cost)
					continue;
				dis[v] = dis[u] + E[i].cost;
				if(!vis[v])
				{
					vis[v] = true;
					Q.push(v);
				}
			}
		}
		for(int i = 0; i <= n; i++)
			dis[i] = dis[en] - dis[i];
	}
	int zkw(int s, int t, int need)
	{
		st = s, en = t;
		spfa();
		mincost = maxflow = 0;
		for(int i = 0; i <= n; i++)
			vis[i] = false, cur[i] = net[i];
		do
		{
			while(augment(st, inf))
				memset(vis, false, sizeof(vis));
		}while(adjust());
		if(maxflow < need)
			return -1;
		return mincost;
	}
}zkw;
char mp[50][50];
int n, m, siz;
int work(int x){
	int all = n*x;
	if(all % m || all > n*m)return inf;
	int y = all / m;
	zkw.init(n+m+2);
	int from = n+m+1, to = n + m + 2;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
			if(mp[i][j]=='1')
				zkw.add(i, n+j, 1, -1);
			else
				zkw.add(i, n+j, 1, 1);
	}
	for(int i = 1; i <= n; i++)
		zkw.add(from, i, x, 0);
	for(int i = 1; i <= m; i++)
		zkw.add(n+i, to, y, 0);
	return siz + zkw.zkw(from, to, -1);
}
void input(){
	scanf("%d %d",&n,&m);
	siz = 0;
	for(int i = 1; i <= n; i++) {
		scanf("%s", mp[i]+1);
		for(int j = 1; j <= m; j++)
			siz += mp[i][j] == '1';
	}
}
int main(){
	int Cas = 0, T; scanf("%d",&T);
	while(T--){
		input();
		int ans = inf;
		for(int i = 0; i <= m; i++)
			ans = min(ans, work(i));
		printf("Case %d: %d\n", ++Cas, ans);
	}
	return 0;
}

UVa 12534 Binary Matrix 2 zkw费用流模版题,布布扣,bubuko.com

时间: 2024-12-25 01:13:03

UVa 12534 Binary Matrix 2 zkw费用流模版题的相关文章

POJ 3422 kaka&#39;s matrix trvals(费用流)

#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <cma

POJ训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次只能向右和向下走,走到一个格子上加上格子的数,可以走k次.问最大的和是多少. 思路: 建图:每个格子掰成两个点,分别叫"出点","入点", 入点到出点间连一个容量1,费用为格子数的边,以及一个容量∞,费用0的边. 同时,一个格子的"出点"向它右.下的格子的"入点"连边,容量∞,费用0. 源点向(0,0)的入点连一个容量K的边,(N-1,N-1)的出点向汇点连一

POJ 3422 Kaka&#39;s Matrix Travels(费用流)

POJ 3422 Kaka's Matrix Travels 题目链接 题意:一个矩阵,从左上角往右下角走k趟,每次走过数字就变成0,并且获得这个数字,要求走完之后,所获得数字之和最大 思路:有点类似区间k覆盖的建图方法,把点拆了,每个点有值的只能选一次,其他都是无值的,利用费用流,入点出点之间连一条容量1,有费用的边,和一条容量k - 1,费用0的边,然后其他就每个点和右边和下边2个点连边,然后跑费用流 代码: #include <cstdio> #include <cstring&g

学习了ZKW费用流

所谓ZKW费用流,其实就是Dinic. 若干年前有一个人发明了最小增广路算法,每次用BFS找一条增广路,时间O(nm^2) 然后被DinicD飞了:我们为什么不可以在长度不变时多路增广呢?时间O(n^2m) 于是可以用到费用流里来:我们为什么不可以在s到t最短路不变时多路增广呢? 实现做法要从t逆向做SPFA,然后多路增广,具体可以见代码 #include<cstdio> #include<cctype> #include<queue> #include<cstr

poj 3422 Kaka&#39;s Matrix Travels (费用流)

Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7743   Accepted: 3111 Description On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka mo

ZKW费用流

bool spfa() { for(int i=0;i<=T;i++)dis[i]=inf; int head=0,tail=1; dis[0]=0;q[0]=0;inq[0]=1; while(head!=tail) { int now=q[head++];if(head==1601)head=0; for(int i=last[now];i;i=e[i].next) if(e[i].v&&e[i].c+dis[now]<dis[e[i].to]) { dis[e[i].to

POJ 3686 The Windy&#39;s(思维+费用流好题)

The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5362   Accepted: 2249 Description The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The ma

HDU 3376 &amp;&amp; 2686 方格取数 最大和 费用流裸题

题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.只能走最短路 3.走过的点不能再走 问最大和. 对每个点拆点限流为1即可满足3. 费用流流量为2满足1 最大费用流,先给图取负,结果再取负,满足2 #include <stdio.h> #include <string.h> #include <iostream> #include <math.h> #include <queue> #include <set&

UVA 10746 Crime Wave – The Sequel(费用流)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1687 ProblemG CrimeWave – The Sequel Input: StandardInput Output: StandardOutput TimeLimit: 2 Seconds n bankshave been robbed this fine day. m (grea