QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#314538#7737. Extending DistanceEasonLiangWA 157ms15392kbC++145.2kb2024-01-25 19:55:262024-01-25 19:55:26

Judging History

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

  • [2024-01-25 19:55:26]
  • 评测
  • 测评结果:WA
  • 用时:157ms
  • 内存:15392kb
  • [2024-01-25 19:55:26]
  • 提交

answer

#include <bits/stdc++.h>
#define FileIO_(file) \
	freopen (file ".in", "r", stdin); \
	freopen (file ".out", "w", stdout);

using namespace std;

template<typename Tp>
inline void chmin (Tp &x, const Tp &y) { if (x > y) x = y; }
template<typename Tp>
inline void chmax (Tp &x, const Tp &y) { if (x < y) x = y; }

typedef double dbl;
typedef long long ll;
typedef long double ldb;
const int maxnm = 5e2 + 20;

void init () {}

namespace Dinic {

const int maxvcnt = maxnm * maxnm;
const int maxecnt = 2 * 3 * maxnm * maxnm;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
int s, t, ecnt, h[maxvcnt], cur[maxvcnt];
ll dis[maxvcnt];
bool vis[maxvcnt];

struct Data {
	ll w, c;
	void operator+= (const Data &oth) {
		w += oth.w; c += oth.c;
	}
};

struct Edge {
	Data d; int to, nxt;
} e[maxecnt];

void init (int s, int t) {
	Dinic::s = s; Dinic::t = t; ecnt = 0;
	memset (h, ~0, sizeof (h));
}

int addEdge (int u, int v, const Data &d) {
	e[ecnt] = {d, v, h[u]}; return h[u] = ecnt++;
}

int link (int u, int v, ll w, ll c) {
	addEdge (v, u, {c ? 0 : w, -c});
	return addEdge (u, v, {w, c});
}

bool bfs () {
	memcpy (cur, h, sizeof (cur));
	memset (dis, ~0, sizeof (dis));
	queue<int, list<int> > q;
	dis[s] = 0; q.emplace (s);
	
	while (!q.empty ()) {
		int u = q.front (); q.pop ();
		
		for (int i = h[u]; ~i; i = e[i].nxt) {
			int v = e[i].to;
			if (e[i].d.w && !~dis[v]) {
				dis[v] = dis[u] + 1;
				q.emplace (v);
			}
		}
	}
	
	return ~dis[t];
}

bool spfa () {
	memcpy (cur, h, sizeof (cur));
	memset (vis, 0, sizeof (vis));
	memset (dis, 0x3f, sizeof (dis));
	queue<int, list<int> > q;
	dis[s] = 0; q.emplace (s);
	
	while (!q.empty ()) {
		int u = q.front (); q.pop (); vis[u] = 0;
		
		for (int i = h[u]; ~i; i = e[i].nxt) {
			int v = e[i].to;
			if (e[i].d.w) {
				ll nd = dis[u] + e[i].d.c;
				if (nd < dis[v]) {
					dis[v] = nd;
					if (!vis[v]) {
						vis[v] = 1;
						q.emplace (v);
					}
				}
			}
		}
	}
	
	return dis[t] != lnf;
}

ll dfsFlow (int u, ll flow) {
	if (u == t) return flow;
	
	ll res = 0;
	
	for (int &i = cur[u]; ~i; i = e[i].nxt) {
		int v = e[i].to; ll &w = e[i].d.w;
		if (v != t && (!w || dis[v] != dis[u] + 1)) continue;
		ll ext = dfsFlow (v, min (w, flow - res));
		w -= ext; e[i ^ 1].d.w += ext; res += ext;
		if (res == flow) break;
	}
	
	return res;
}

Data dfsCost (int u, ll flow) {
	vis[u] = 1;
	
	if (u == t) return {flow, 0};
	
	Data res = {0, 0};
	
	for (int &i = cur[u]; ~i; i = e[i].nxt) {
		int v = e[i].to; ll &w = e[i].d.w, c = e[i].d.c;
		if (!w || dis[v] != dis[u] + c || (v != t && vis[v])) continue;
		Data ext = dfsCost (v, min (w, flow - res.w));
		w -= ext.w; e[i ^ 1].d.w += ext.w;
		res += {ext.w, ext.c + ext.w * c};
		if (res.w == flow) break;
	}
	
	return res;
}

ll workFlow () {
	ll res = 0;
	while (bfs ())
		res += dfsFlow (s, lnf);
	return res;
}

Data workCost () {
	Data res = {0, 0};
	while (spfa ()) do {
		memset (vis, 0, sizeof (vis));
		res += dfsCost (s, lnf);
	} while (vis[t]);
	return res;
}

} // namespace Dinic

int n, m, k, vs, vu, vd, vl, vr;
int r[maxnm][maxnm], c[maxnm][maxnm];
ll er[maxnm][maxnm][2], ec[maxnm][maxnm][2];

int getId (int x, int y) {
	if (x <= 0) return vu;
	if (x >= n) return vd;
	if (y <= 0) return vl;
	if (y >= m) return vr;
	return (x - 1) * m + y;
}

void solve () {
	scanf ("%d%d%d", &n, &m, &k);
	
	vs = n * m;
	vu = n * m + 1; vd = n * m + 2;
	vl = n * m + 3; vr = n * m + 4;
	
	Dinic::init (vu, vd);

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j < m; ++j) {
			scanf ("%d", &r[i][j]);
			Dinic::link (
				getId (i - 1, j),
				getId (i, j),
				r[i][j], 0
			);
		}
	}
	
	for (int i = 1; i < n; ++i) {
		for (int j = 1; j <= m; ++j) {
			scanf ("%d", &c[i][j]);
			if (1 < j && j < m)
				Dinic::link (
					getId (i, j - 1),
					getId (i, j),
					c[i][j], 0
				);
		}
	}
	
	Dinic::workFlow ();
	Dinic::link (vs, Dinic::s, k, 0);
	Dinic::s = vs;

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j < m; ++j) {
			er[i][j][0] = Dinic::link (
				getId (i - 1, j),
				getId (i, j),
				Dinic::lnf, 1
			);
			er[i][j][1] = Dinic::link (
				getId (i, j),
				getId (i - 1, j),
				Dinic::lnf, 1
			);
		}
	}
	
	for (int i = 1; i < n; ++i) {
		for (int j = 1; j <= m; ++j) {
			ec[i][j][0] = Dinic::link (
				getId (i, j - 1),
				getId (i, j),
				Dinic::lnf, 1
			);
			ec[i][j][1] = Dinic::link (
				getId (i, j),
				getId (i, j - 1),
				Dinic::lnf, 1
			);
		}
	}
	
	printf ("%lld\n", Dinic::workCost ().c);

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j < m; ++j) {
			printf ("%lld%c", r[i][j] +
				(Dinic::lnf - Dinic::e[er[i][j][0]].d.w) +
				(Dinic::lnf - Dinic::e[er[i][j][1]].d.w),
				" \n"[j == m - 1]
			);
		}
	}
	
	for (int i = 1; i < n; ++i) {
		for (int j = 1; j <= m; ++j) {
			printf ("%lld%c", c[i][j] +
				(Dinic::lnf - Dinic::e[ec[i][j][0]].d.w) +
				(Dinic::lnf - Dinic::e[ec[i][j][1]].d.w),
				" \n"[j == m]
			);
		}
	}
}

int main () {
//	#ifndef LSY
//	FileIO_("");
//	#endif
	int t = 1; init ();
	scanf ("%d", &t);
	while (t--) solve ();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 14532kb

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
2 6 15
8 1 9
13 5 3
3 6 1 2
5 2 15 3
4
1 4
2 3
3 3
1 1 1
2 2 2

result:

ok Correct. (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 157ms
memory: 15392kb

input:

125
4 4 48
33 9 39
38 74 3
18 44 9
20 91 19
76 95 17 16
61 88 50 49
68 18 33 84
4 4 14
54 69 42
47 90 28
2 73 59
1 20 90
43 22 74 19
27 67 46 43
42 21 78 80
4 4 93
12 67 38
13 85 39
74 68 77
71 80 64
92 97 53 46
66 6 30 20
66 70 71 24
4 4 34
43 86 55
49 34 73
78 77 90
99 49 5
55 4 63 47
80 24 15 3
8...

output:

87
33 57 39
38 74 3
29 72 9
20 91 19
76 95 17 16
61 88 50 49
68 18 33 84
14
54 69 42
47 90 28
2 73 59
2 33 90
43 22 74 19
27 67 46 43
42 21 78 80
166
12 79 119
59 85 66
74 68 77
71 80 64
92 97 53 46
66 6 30 20
66 70 71 24
34
45 86 55
49 37 73
78 77 90
99 49 34
55 4 63 47
80 24 15 3
85 12 6 31
45
45 ...

result:

wrong answer The length of shortest path shoult extend exactly K. (test case 7)