HDU 2448 Mining Station on the Sea（KM最大匹配+floyd)

2018年10月11日 ⁄ 综合 ⁄ 共 1943字 ⁄ 字号 评论关闭

HDU 2448 Mining Station on the Sea

```#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int MAXNODE = 105;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct KM {
int n, m;
Type g[MAXNODE][MAXNODE];
Type Lx[MAXNODE], Ly[MAXNODE], slack[MAXNODE];
int left[MAXNODE], right[MAXNODE];
bool S[MAXNODE], T[MAXNODE];

void init(int n, int m) {
this->n = n;
this->m = m;
}

void add_Edge(int u, int v, Type val) {
g[u][v] = val;
}

bool dfs(int i) {
S[i] = true;
for (int j = 0; j < m; j++) {
if (T[j]) continue;
Type tmp = Lx[i] + Ly[j] - g[i][j];
if (!tmp) {
T[j] = true;
if (left[j] == -1 || dfs(left[j])) {
left[j] = i;
right[i] = j;
return true;
}
} else slack[j] = min(slack[j], tmp);
}
return false;
}

void update() {
Type a = INF;
for (int i = 0; i < m; i++)
if (!T[i]) a = min(a, slack[i]);
for (int i = 0; i < n; i++)
if (S[i]) Lx[i] -= a;
for (int i = 0; i < m; i++)
if (T[i]) Ly[i] += a;
}

Type km() {
memset(left, -1, sizeof(left));
memset(right, -1, sizeof(right));
memset(Ly, 0, sizeof(Ly));
for (int i = 0; i < n; i++) {
Lx[i] = -INF;
for (int j = 0; j < m; j++)
Lx[i] = max(Lx[i], g[i][j]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) slack[j] = INF;
while (1) {
memset(S, false, sizeof(S));
memset(T, false, sizeof(T));
if (dfs(i)) break;
else update();
}
}
Type ans = 0;
for (int i = 0; i < n; i++)
ans += g[i][right[i]];
return -ans;
}
} gao;

const int N = 305;

int n, m, k, p;
int st[N], g[N][N];

int main() {
while (~scanf("%d%d%d%d", &n, &m, &k, &p)) {
for (int i = 0; i < n; i++) {
scanf("%d", &st[i]);
st[i]--;
}
int u, v, w;
int tot = n + m;
for (int i = 0; i < tot; i++)
for (int j = 0; j < tot; j++) {
if (i == j) g[i][j] = 0;
else g[i][j] = INF;
}
while (k--) {
scanf("%d%d%d", &u, &v, &w);
u--; v--;
g[u][v] = g[v][u] = min(g[u][v], w);
}
while (p--) {
scanf("%d%d%d", &u, &v, &w);
u--; v--;
g[u + m][v] = g[v][u + m] = min(g[u + m][v], w);
}
for (int k = 0; k < m; k++)
for (int i = 0; i < tot; i++)
for (int j = 0; j < tot; j++)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
gao.init(n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {