【网络流 FOJ 2143 Board Game

费用流。。。。拆边,把一条边拆成k条边,这样k条边的费用是单调递增的。然后奇偶建图。跑费用流的时候费用为正就退出。。。。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 105
#define maxm 100005
#define eps 1e-7
#define mod 1000000009
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
//head

struct Edge
{
	int v, c, w, next;
	Edge() {}
	Edge(int v, int c, int w, int next) : v(v), c(c), w(w), next(next) {}
}E[maxm];

queue<int> q;
int H[maxn], cntE;
int cap[maxn];
int vis[maxn];
int dis[maxn];
int cur[maxn];
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int flow, cost, T, s, t;
int n, m;

void addedges(int u, int v, int c, int w)
{
	E[cntE] = Edge(v, c, w, H[u]);
	H[u] = cntE++;
	E[cntE] = Edge(u, 0, -w, H[v]);
	H[v] = cntE++;
}

bool spfa()
{
	memset(dis, INF, sizeof dis);
	cur[s] = -1;
	vis[s] = ++T;
	cap[s] = INF;
	dis[s] = 0;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		vis[u] = T - 1;
		for(int e = H[u]; ~e; e = E[e].next) {
			int v = E[e].v, c = E[e].c, w = E[e].w;
			if(c && dis[v] > dis[u] + w) {
				dis[v] = dis[u] + w;
				cap[v] = min(cap[u], c);
				cur[v] = e;
				if(vis[v] != T) {
					vis[v] = T;
					q.push(v);
				}
			}
		}
	}
	if(dis[t] >= 0) return false;
	cost += cap[t] * dis[t];
	flow += cap[t];
	for(int e = cur[t]; ~e; e = cur[E[e ^ 1].v]) {
		E[e].c -= cap[t];
		E[e ^ 1].c += cap[t];
	}
	return true;
}

int mfmc()
{
	flow = cost = 0;
	while(spfa());
	return cost;
}

void init()
{
	cntE = T = 0;
	memset(H, -1, sizeof H);
	memset(vis, 0, sizeof vis);
}

inline int calc(int i, int j)
{
	return (i - 1) * m + j;
}

void work(int _)
{
	int kk, x;
	scanf("%d%d%d", &n, &m, &kk);
	int res = 0;
	s = 0, t = n * m + 1;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) {
			scanf("%d", &x);
			int tt = x * x;
			res += tt;
			if((i+j) % 2) {
				for(int k = 0; k < 4; k++) {
					int ti = i + dir[k][0];
					int tj = j + dir[k][1];
					if(ti <= 0 || ti > n || tj <= 0 || tj > m) continue;
					addedges(calc(i, j), calc(ti, tj), INF, 0);
				}
			}
			for(int k = 1; k <= kk; k++) {
				if((i+j) % 2) addedges(s, calc(i, j), 1, (x - k) * (x - k) - tt);
				else addedges(calc(i, j), t, 1, (x - k) * (x - k) - tt);
				tt = (x - k) * (x - k);
			}
		}
	printf("Case %d: %d\n", _, mfmc() + res);
}

int main()
{
	int _;
	scanf("%d", &_);
	for(int i = 1; i <= _; i++) {
		init();
		work(i);
	}
	
	
	return 0;
}


你可能感兴趣的:(网络流)