結果

問題 No.1615 Double Down
ユーザー ygussanyygussany
提出日時 2021-06-20 20:34:46
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 812 ms / 10,000 ms
コード長 5,700 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 35,200 KB
実行使用メモリ 16,660 KB
最終ジャッジ日時 2024-07-17 15:16:07
合計ジャッジ時間 13,727 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
10,452 KB
testcase_01 AC 109 ms
10,332 KB
testcase_02 AC 150 ms
12,528 KB
testcase_03 AC 150 ms
12,708 KB
testcase_04 AC 135 ms
12,752 KB
testcase_05 AC 134 ms
12,752 KB
testcase_06 AC 190 ms
15,616 KB
testcase_07 AC 188 ms
15,476 KB
testcase_08 AC 168 ms
15,500 KB
testcase_09 AC 634 ms
15,192 KB
testcase_10 AC 629 ms
15,316 KB
testcase_11 AC 648 ms
15,064 KB
testcase_12 AC 281 ms
14,944 KB
testcase_13 AC 251 ms
14,796 KB
testcase_14 AC 271 ms
14,940 KB
testcase_15 AC 190 ms
16,540 KB
testcase_16 AC 190 ms
16,660 KB
testcase_17 AC 215 ms
16,640 KB
testcase_18 AC 765 ms
15,244 KB
testcase_19 AC 812 ms
15,104 KB
testcase_20 AC 760 ms
15,224 KB
testcase_21 AC 734 ms
15,220 KB
testcase_22 AC 737 ms
15,108 KB
testcase_23 AC 750 ms
15,096 KB
testcase_24 AC 737 ms
15,072 KB
testcase_25 AC 687 ms
15,200 KB
testcase_26 AC 712 ms
15,208 KB
testcase_27 AC 21 ms
5,832 KB
testcase_28 AC 22 ms
5,700 KB
testcase_29 AC 35 ms
6,224 KB
testcase_30 AC 35 ms
6,220 KB
testcase_31 AC 34 ms
6,336 KB
testcase_32 AC 35 ms
6,068 KB
testcase_33 AC 50 ms
6,780 KB
testcase_34 AC 50 ms
6,904 KB
testcase_35 AC 49 ms
6,896 KB
testcase_36 AC 4 ms
5,376 KB
testcase_37 AC 4 ms
5,376 KB
testcase_38 AC 5 ms
5,376 KB
testcase_39 AC 5 ms
5,376 KB
testcase_40 AC 4 ms
5,376 KB
testcase_41 AC 4 ms
5,376 KB
testcase_42 AC 5 ms
5,376 KB
testcase_43 AC 5 ms
5,376 KB
testcase_44 AC 5 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 3 ms
5,376 KB
testcase_47 AC 3 ms
5,376 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 3 ms
5,376 KB
testcase_51 AC 3 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 3 ms
5,376 KB
testcase_54 AC 3 ms
5,376 KB
testcase_55 AC 3 ms
5,376 KB
testcase_56 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

typedef struct {
	int id, head, tail, flow, cap, cost;
} edge;

typedef struct List {
	struct List *next;
	edge *e;
} list;

const int sup = 1 << 30;

void chmin(int* a, int b)
{
	if (*a > b) *a = b;
}

int DFS(list *aux[], edge f[], int par[], int u)
{
	int i, w, c, argmin;
	for (; aux[u] != NULL; aux[u] = aux[u]->next) {
		if (aux[u]->e->flow == aux[u]->e->cap) continue;
		w = aux[u]->e->head;
		if (par[w] == -3) {
			for (i = aux[u]->e->id, c = sup; par[w] != -2; i = par[w]) {
				chmin(&c, f[i].cap - f[i].flow);
				w = f[i].tail;
				if (f[i].cap - f[i].flow == c) argmin = w;
			}
			for (i = aux[u]->e->id, w = aux[u]->e->head; par[w] != -2; i = par[w]) {
				f[i].flow += c;
				w = f[i].tail;
			}
			if (argmin != u) {
				par[u] = -1;
				return argmin;
			}
		} else if (par[w] == -1) {
			par[w] = aux[u]->e->id;
			w = DFS(aux, f, par, w);
			if (w > 0 && w != u) {
				par[u] = -1;
				return w;
			}
		}
	}
	return 0;
}

int Dinic(int N, int M, list* adj[], edge e[], int s, int t)
{
	int i, u, w, ans = 0, add, *par = (int*)malloc(sizeof(int) * (N + 1)), *dist = (int*)malloc(sizeof(int) * (N + 1)), *q = (int*)malloc(sizeof(int) * (N + 1)), head, tail;
	list **aux = (list**)malloc(sizeof(list*) * (N + 1)), *df = (list*)malloc(sizeof(list) * (M + 1)), *p;
	edge *f = (edge*)malloc(sizeof(edge) * (M + 1));
	for (i = 0; i < M; i++) f[i].flow = 0;
	do {
		for (u = 1; u <= N; u++) {
			dist[u] = -1;
			aux[u] = NULL;
		}
		dist[s] = 0;
		q[0] = s;
		for (head = 0, tail = 1; head < tail; head++) {
			u = q[head];
			for (p = adj[u]; p != NULL; p = p->next) {
				if (p->e->flow == p->e->cap) continue;
				w = p->e->head;
				if (dist[w] == -1) {
					dist[w] = dist[u] + 1;
					q[tail++] = w;
				}
				if (dist[w] == dist[u] + 1) {
					i = p->e->id;
					f[i].id = i;
					f[i].tail = u;
					f[i].head = w;
					f[i].flow = 0;
					f[i].cap = p->e->cap - p->e->flow;
					df[i].e = &(f[i]);
					df[i].next = aux[u];
					aux[u] = &(df[i]);
				}
			}
		}
		for (u = 1; u <= N; u++) par[u] = -1;
		par[s] = -2;
		par[t] = -3;
		DFS(aux, f, par, s);

		for (i = 0, add = 0; i < M; i++) {
			if (e[i].flow == e[i].cap || dist[e[i].head] != dist[e[i].tail] + 1 || f[i].flow == 0) continue;
			if (f[i].tail == s) add += f[i].flow;
			if (e[i^1].flow > 0) {
				e[i^1].flow -= f[i].flow;
				e[i].cap -= f[i].flow;
			} else {
				e[i].flow += f[i].flow;
				e[i^1].cap += f[i].flow;
			}
		}
		ans += add;
	} while (add != 0);
	
	free(par);
	free(dist);
	free(q);
	free(aux);
	free(df);
	free(f);
	return ans;
}

long long solve(int N, int M, int K, int L, int X[], int Y[], int Z[])
{
	int i, k, m = 0, u, w, s = N + M + 1, t = N + M + 2, bit[31], flag[200003] = {}, tmp[200003], q[200003], head, tail;
	long long ans = 0;
	list *adj[200003] = {}, d[800001], *p, *pp;
    edge e[800001];
	for (i = 1, bit[0] = 1; i <= K; i++) bit[i] = bit[i-1] << 1;
	for (i = 1; i <= N; i++) {
		u = s;
		w = i;
		e[m].id = m;
		e[m].tail = u;
		e[m].head = w;
		e[m].flow = 0;
		e[m].cap = 1;
		e[m].cost = K;
		d[m].e = &(e[m]);
		d[m].next = adj[u];
		adj[u] = &(d[m++]);
		e[m].id = m;
		e[m].tail = w;
		e[m].head = u;
		e[m].flow = 0;
		e[m].cap = 0;
		e[m].cost = K;
		d[m].e = &(e[m]);
		d[m].next = adj[w];
		adj[w] = &(d[m++]);
	}
	for (i = 1; i <= M; i++) {
		u = i + N;
		w = t;
		e[m].id = m;
		e[m].tail = u;
		e[m].head = w;
		e[m].flow = 0;
		e[m].cap = 1;
		e[m].cost = K;
		d[m].e = &(e[m]);
		d[m].next = adj[u];
		adj[u] = &(d[m++]);
		e[m].id = m;
		e[m].tail = w;
		e[m].head = u;
		e[m].flow = 0;
		e[m].cap = 0;
		e[m].cost = K;
		d[m].e = &(e[m]);
		d[m].next = adj[w];
		adj[w] = &(d[m++]);
	}
	for (k = K; k >= 0; k--) {
		for (i = 1; i <= L; i++) {
			u = X[i];
			w = Y[i] + N;
			if (Z[i] != k || flag[u] != 0 || flag[w] != 0) continue;
			e[m].id = m;
			e[m].tail = u;
			e[m].head = w;
			e[m].flow = 0;
			e[m].cap = 1;
			e[m].cost = k;
			d[m].e = &(e[m]);
			d[m].next = adj[u];
			adj[u] = &(d[m++]);
			e[m].id = m;
			e[m].tail = w;
			e[m].head = u;
			e[m].flow = 0;
			e[m].cap = 0;
			e[m].cost = k;
			d[m].e = &(e[m]);
			d[m].next = adj[w];
			adj[w] = &(d[m++]);
		}
		ans += (long long)Dinic(t, m, adj, e, s, t) * bit[k];
		
		for (u = 1; u <= t; u++) tmp[u] = 0;
		q[0] = s;
		tmp[s] = 1;
		for (head = 0, tail = 1; head < tail; head++) {
			u = q[head];
			for (pp = adj[u]; pp != NULL; pp = pp->next) {
				if (pp->e->flow == pp->e->cap || pp->e->head >= s) continue;
				w = pp->e->head;
				if (tmp[w] == 0) {
					tmp[w] = -tmp[u];
					q[tail++] = w;
				}
			}
		}
		q[0] = t;
		tmp[t] = 1;
		for (head = 0, tail = 1; head < tail; head++) {
			u = q[head];
			for (pp = adj[u]; pp != NULL; pp = pp->next) {
				if (e[pp->e->id ^ 1].flow == e[pp->e->id ^ 1].cap || pp->e->head >= s) continue;
				w = pp->e->head;
				if (tmp[w] == 0) {
					tmp[w] = -tmp[u];
					q[tail++] = w;
				}
			}
		}
		for (i = 0; i < m; i += 2) {
			u = e[i].tail;
			w = e[i].head;
			if (e[i].cap == 0 && e[i^1].cap == 0) continue;
			if (u == s) {
				if (tmp[w] == 0) {
					e[i].flow = 0;
					e[i].cap = 0;
					e[i^1].cap = 0;
				}
			} else if (w == t) {
				if (tmp[u] == 0) {
					e[i].flow = 0;
					e[i].cap = 0;
					e[i^1].cap = 0;
				}
			} else {
				if (tmp[u] >= 0 && tmp[w] >= 0) {
					e[i].cap = 0;
					e[i^1].cap = 0;
				}
			}
		}
		for (u = 1; u <= N + M; u++) if (flag[u] == 0 && tmp[u] >= 0) flag[u] = k;
	}
	return ans;
}

int main()
{
	int i, j, N, M, K, L, r, X[200001], Y[200001], Z[200001];
	scanf("%d %d %d %d", &N, &M, &K, &L);
	for (i = 1; i <= L; i++) scanf("%d %d %d", &(X[i]), &(Y[i]), &(Z[i]));
	printf("%lld\n", solve(N, M, K, L, X, Y, Z));
	fflush(stdout);
	return 0;
}
0