hdu 1569 最大点权独立集

参考网上大牛的方法做的,A了,但做法的可行性我还没有想明白。慢慢再想吧,先贴代码~

/*
* hdu1569/win.cpp
* Created on: 2011-10-5
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

typedef int typec;
const typec inf = 0x7fffffff;
const int MAXE = 40000;
const int MAXN = 3000;
typedef struct {
int x, y, nxt;
typec c;
} Edge;
Edge bf[MAXE];
int ne, head[MAXN], cur[MAXN], ps[MAXN], dep[MAXN];
int map[MAXN][MAXN];
typec sum;

inline void addedge(int x, int y, typec c) {
bf[ne].x = x;
bf[ne].y = y;
bf[ne].c = c;
bf[ne].nxt = head[x];
head[x] = ne++;
bf[ne].x = y;
bf[ne].y = x;
bf[ne].c = 0;
bf[ne].nxt = head[y];
head[y] = ne++;
}

typec flow(int n, int s, int t) {
typec tr, res = 0;
int i, j, k, f, r, top;
while (1) {
memset(dep, -1, n * sizeof(int));
for (f = dep[ps[0] = s] = 0, r = 1; f != r;) {
for (i = ps[f++], j = head[i]; j; j = bf[j].nxt) {
if (bf[j].c && -1 == dep[k = bf[j].y]) {
dep[k] = dep[i] + 1;
ps[r++] = k;
if (k == t) {
f = r;
break;
}
}
}
}
if (-1 == dep[t]) {
break;
}
memcpy(cur, head, n * sizeof(int));
for (i = s, top = 0;;) {
if (i == t) {
for (k = 0, tr = inf; k < top; ++k) {
if (bf[ps[k]].c < tr) {
tr = bf[ps[f = k]].c;
}
}
for (k = 0; k < top; k++) {
bf[ps[k]].c -= tr, bf[ps[k] ^ 1].c += tr;
}
res += tr;
i = bf[ps[top = f]].x;
}
for (j = cur[i]; cur[i]; j = cur[i] = bf[cur[i]].nxt) {
if (bf[j].c && dep[i] + 1 == dep[bf[j].y]) {
break;
}
}
if (cur[i]) {
ps[top++] = cur[i];
i = bf[cur[i]].y;
} else {
if (0 == top) {
break;
}
dep[i] = -1;
i = bf[ps[--top]].x;
}
}
}
return res;
}

void init(int m, int n) {
int s = m * n;
int t = s + 1;
typec value;
sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &value);
sum += value;
if ((i + j) % 2 == 0) {
addedge(s, i * n + j, value);
if (i > 0) {
addedge(i * n + j, (i - 1) * n + j, inf);
}
if (j > 0) {
addedge(i * n + j, i * n + j - 1, inf);
}
if (i < m - 1) {
addedge(i * n + j, (i + 1) * n + j, inf);
}
if (j < n - 1) {
addedge(i * n + j, i * n + j + 1, inf);
}
} else {
addedge(i * n + j, t, value);
}
}
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int n, m;
while (scanf("%d%d", &m, &n) != EOF) {
ne = 2;
memset(head, 0, sizeof(head));
init(m, n);
printf("%d\n", sum - flow(m * n + 2, m * n, m * n + 1));
}
return 0;
}



posted @ 2011-10-05 16:09  moonbay  阅读(163)  评论(0编辑  收藏  举报