QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#314501#7737. Extending DistancezltWA 1ms3836kbC++146.2kb2024-01-25 19:07:302024-01-25 19:07:30

Judging History

你现在查看的是最新测评结果

  • [2024-01-25 19:07:30]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3836kb
  • [2024-01-25 19:07:30]
  • 提交

answer

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;

const int maxn = 510;
const int maxm = 10050;
const int inf = 0x3f3f3f3f;

int n, m, K, a[maxn][maxn], b[maxn][maxn], id[maxn][maxn];
int head[maxm], len, S, T, nt;
struct edge {
	int to, next, cap, flow, cost;
} edges[maxm];

inline void add_edge(int u, int v, int c, int f, int co = 0) {
	edges[++len].to = v;
	edges[len].next = head[u];
	edges[len].cap = c;
	edges[len].flow = f;
	edges[len].cost = co;
	head[u] = len;
}

struct Dinic {
	int d[maxm], cur[maxm];
	bool vis[maxm];
	
	inline void add(int u, int v, int c) {
		add_edge(u, v, c, 0);
		add_edge(v, u, 0, 0);
	}
	
	inline bool bfs() {
		for (int i = 1; i <= nt; ++i) {
			d[i] = -1;
			vis[i] = 0;
		}
		queue<int> q;
		q.push(S);
		vis[S] = 1;
		d[S] = 0;
		while (q.size()) {
			int u = q.front();
			q.pop();
			for (int i = head[u]; i; i = edges[i].next) {
				edge &e = edges[i];
				if (!vis[e.to] && e.cap > e.flow) {
					vis[e.to] = 1;
					d[e.to] = d[u] + 1;
					q.push(e.to);
				}
			}
		}
		return vis[T];
	}
	
	int dfs(int u, int a) {
		if (u == T || !a) {
			return a;
		}
		int flow = 0, f;
		for (int &i = cur[u]; i; i = edges[i].next) {
			edge &e = edges[i];
			if (d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i ^ 1].flow -= f;
				flow += f;
				a -= f;
				if (!a) {
					break;
				}
			}
		}
		return flow;
	}
	
	inline int solve() {
		int flow = 0;
		while (bfs()) {
			for (int i = 1; i <= nt; ++i) {
				cur[i] = head[i];
			}
			flow += dfs(S, inf);
		}
		return flow;
	}
} solver;

struct MCMF {
	int d[maxm], cur[maxm];
	bool vis[maxm];
	
	inline void add(int u, int v, int c, int co) {
		add_edge(u, v, c, 0, co);
		add_edge(v, u, 0, 0, -co);
	}
	
	inline bool spfa() {
		for (int i = 1; i <= nt; ++i) {
			d[i] = inf;
			vis[i] = 0;
		}
		queue<int> q;
		q.push(S);
		d[S] = 0;
		vis[S] = 1;
		while (q.size()) {
			int u = q.front();
			q.pop();
			vis[u] = 0;
			for (int i = head[u]; i; i = edges[i].next) {
				edge &e = edges[i];
				if (d[e.to] > d[u] + e.cost && e.cap > e.flow) {
					d[e.to] = d[u] + e.cost;
					if (!vis[e.to]) {
						vis[e.to] = 1;
						q.push(e.to);
					}
				}
			}
		}
		return d[T] < inf;
	}
	
	int dfs(int u, int a, int &cost) {
		if (u == T || !a) {
			return a;
		}
		vis[u] = 1;
		int flow = 0, f;
		for (int &i = cur[u]; i; i = edges[i].next) {
			edge &e = edges[i];
			if (d[e.to] == d[u] + e.cost && !vis[e.to] && e.cap > e.flow) {
				if ((f = dfs(e.to, min(a, e.cap - e.flow), cost)) > 0) {
					cost += f * e.cost;
					e.flow += f;
					edges[i ^ 1].flow -= f;
					flow += f;
					a -= f;
					if (!a) {
						break;
					}
				}
			}
		}
		vis[u] = 0;
		return flow;
	}
	
	inline pii solve() {
		int flow = 0, cost = 0;
		while (spfa()) {
			for (int i = 1; i <= nt; ++i) {
				cur[i] = head[i];
			}
			flow += dfs(S, inf, cost);
		}
		return mkp(flow, cost);
	}
} mcmf;

struct edg {
	int u, v, f;
	edg(int a = 0, int b = 0, int c = 0) : u(a), v(b), f(c) {}
};

void solve() {
	scanf("%d%d%d", &n, &m, &K);
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j < m; ++j) {
			scanf("%d", &a[i][j]);
		}
		a[i][0] = a[i][m] = 0;
	}
	len = 1;
	nt = 0;
	for (int i = 1; i < n; ++i) {
		for (int j = 1; j <= m; ++j) {
			scanf("%d", &b[i][j]);
		}
		for (int j = 1; j <= m + 1; ++j) {
			id[i][j] = ++nt;
		}
	}
	S = ++nt;
	T = ++nt;
	for (int i = 1; i <= nt; ++i) {
		head[i] = 0;
	}
	len = 1;
	for (int i = 1; i <= n - 2; ++i) {
		for (int j = 1; j <= m + 1; ++j) {
			solver.add(id[i][j], id[i + 1][j], a[i + 1][j - 1]);
			solver.add(id[i + 1][j], id[i][j], a[i + 1][j - 1]);
		}
	}
	for (int i = 1; i < n; ++i) {
		for (int j = 1; j <= m; ++j) {
			solver.add(id[i][j], id[i][j + 1], b[i][j]);
			solver.add(id[i][j + 1], id[i][j], b[i][j]);
		}
	}
	for (int i = 1; i <= m + 1; ++i) {
		solver.add(S, id[1][i], a[1][i - 1]);
		solver.add(id[n - 1][i], T, a[n][i - 1]);
	}
	solver.solve();
	vector<edg> vc;
	for (int i = 2; i <= len; i += 2) {
		if (edges[i].flow < edges[i].cap) {
			vc.pb(edges[i ^ 1].to, edges[i].to, edges[i].cap - edges[i].flow);
		}
	}
	int ps = S;
	S = ++nt;
	for (int i = 1; i <= nt; ++i) {
		head[i] = 0;
	}
	len = 1;
	mcmf.add(S, ps, K, 0);
	for (edg e : vc) {
		mcmf.add(e.u, e.v, e.f, 0);
	}
	for (int i = 1; i <= n - 2; ++i) {
		for (int j = 1; j <= m + 1; ++j) {
			mcmf.add(id[i][j], id[i + 1][j], inf, 1);
			mcmf.add(id[i + 1][j], id[i][j], inf, 1);
		}
	}
	for (int i = 1; i < n; ++i) {
		for (int j = 1; j <= m; ++j) {
			mcmf.add(id[i][j], id[i][j + 1], inf, 1);
			mcmf.add(id[i][j + 1], id[i][j], inf, 1);
		}
	}
	for (int i = 1; i <= m + 1; ++i) {
		mcmf.add(ps, id[1][i], inf, 1);
		mcmf.add(id[n - 1][i], T, inf, 1);
	}
	pii ans = mcmf.solve();
	printf("%d\n", ans.scd);
	for (int i = 1; i < n; ++i) {
		for (int j = 1; j <= m + 1; ++j) {
			int u = id[i][j];
			for (int k = head[u]; k; k = edges[k].next) {
				edge e = edges[k];
				if (!e.cost) {
					continue;
				}
				if (e.to == ps) {
					a[1][j - 1] += edges[k ^ 1].flow;
				} else if (e.to == T) {
					a[n][j - 1] += e.flow;
				} else {
					if (k & 1) {
						continue;
					}
					if (i > 1 && e.to == id[i - 1][j]) {
						a[i][j - 1] += e.flow;
					} else if (i <= n - 2 && e.to == id[i + 1][j]) {
						a[i + 1][j - 1] += e.flow;
					} else if (j > 1 && e.to == id[i][j - 1]) {
						b[i][j - 1] += e.flow;
					} else if (j <= m && e.to == id[i][j + 1]) {
						b[i][j] += e.flow;
					}
				}
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j < m; ++j) {
			printf("%d ", a[i][j]);
		}
		putchar('\n');
	}
	for (int i = 1; i < n; ++i) {
		for (int j = 1; j <= m; ++j) {
			printf("%d ", b[i][j]);
		}
		putchar('\n');
	}
}

int main() {
	int T = 1;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3836kb

input:

2
3 4 6
2 1 15
7 1 9
13 3 2
3 6 1 2
5 2 15 3
3 3 3
1 1
2 2
3 3
1 1 1
2 2 2

output:

9
3 5 15 
9 1 9 
13 4 3 
3 6 1 2 
5 2 15 3 
4
1 2 
2 2 
3 3 
1 1 1 
2 2 2 

result:

wrong answer The output T doesn't match number of operations. (test case 2)