結果

問題 No.957 植林
ユーザー ianCKianCK
提出日時 2019-12-27 09:57:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 629 ms / 2,000 ms
コード長 2,397 bytes
コンパイル時間 442 ms
コンパイル使用メモリ 47,744 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-04-16 12:27:48
合計ジャッジ時間 15,846 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,888 KB
testcase_01 AC 4 ms
5,760 KB
testcase_02 AC 4 ms
5,888 KB
testcase_03 AC 25 ms
10,624 KB
testcase_04 AC 27 ms
10,496 KB
testcase_05 AC 25 ms
10,752 KB
testcase_06 AC 30 ms
11,008 KB
testcase_07 AC 26 ms
10,624 KB
testcase_08 AC 20 ms
10,752 KB
testcase_09 AC 20 ms
10,752 KB
testcase_10 AC 21 ms
11,008 KB
testcase_11 AC 20 ms
10,624 KB
testcase_12 AC 20 ms
10,624 KB
testcase_13 AC 17 ms
9,344 KB
testcase_14 AC 20 ms
11,264 KB
testcase_15 AC 20 ms
10,752 KB
testcase_16 AC 17 ms
9,728 KB
testcase_17 AC 18 ms
10,624 KB
testcase_18 AC 457 ms
10,752 KB
testcase_19 AC 477 ms
10,880 KB
testcase_20 AC 484 ms
10,880 KB
testcase_21 AC 512 ms
11,008 KB
testcase_22 AC 545 ms
11,136 KB
testcase_23 AC 561 ms
11,264 KB
testcase_24 AC 574 ms
11,264 KB
testcase_25 AC 629 ms
11,264 KB
testcase_26 AC 615 ms
11,392 KB
testcase_27 AC 620 ms
11,264 KB
testcase_28 AC 621 ms
11,136 KB
testcase_29 AC 620 ms
11,264 KB
testcase_30 AC 619 ms
11,136 KB
testcase_31 AC 456 ms
10,620 KB
testcase_32 AC 478 ms
11,008 KB
testcase_33 AC 485 ms
11,008 KB
testcase_34 AC 519 ms
11,008 KB
testcase_35 AC 546 ms
11,204 KB
testcase_36 AC 557 ms
11,008 KB
testcase_37 AC 569 ms
11,392 KB
testcase_38 AC 625 ms
11,264 KB
testcase_39 AC 616 ms
11,136 KB
testcase_40 AC 616 ms
11,264 KB
testcase_41 AC 18 ms
11,136 KB
testcase_42 AC 19 ms
11,264 KB
testcase_43 AC 23 ms
11,264 KB
testcase_44 AC 24 ms
11,264 KB
testcase_45 AC 3 ms
6,016 KB
testcase_46 AC 4 ms
5,760 KB
testcase_47 AC 4 ms
5,888 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:77:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   77 |         scanf("%d%d", &n, &m);
      |         ~~~~~^~~~~~~~~~~~~~~~
main.cpp:78:72: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   78 |         for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%lld", &g[i][j]);
      |                                                                   ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:79:43: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   79 |         for (int i = 1; i <= n; i++) scanf("%lld", &r[i]);
      |                                      ~~~~~^~~~~~~~~~~~~~~
main.cpp:80:43: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   80 |         for (int i = 1; i <= m; i++) scanf("%lld", &c[i]);
      |                                      ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;

#define PB push_back
constexpr int kN = int(1E5 + 10), kNN = int(3E2 + 10);
constexpr long long int kInf = (long long int) (1E16 + 10);

struct Edge {
	int to, rev;
	long long int cap;
};

vector<Edge> graph[kN];
int dep[kN], went[kN], iter[kN];

void AddEdge(int u, int v, long long int c) {
	graph[u].PB({v, int(graph[v].size()), c});
	graph[v].PB({u, int(graph[u].size()) - 1, 0});
	return ;
}

void bfs(int s, int t) {
	int nxt;
	queue<int> q;
	q.push(s);
	iter[s] = dep[s] = 0;
	went[s] = t;
	while (!q.empty()) {
		nxt = q.front();
		q.pop();
		for (Edge i : graph[nxt]) if (went[i.to] != t && i.cap > 0) {
			q.push(i.to);
			went[i.to] = t;
			dep[i.to] = dep[nxt] + 1;
			iter[i.to] = 0;
		}
	}
	return ;
}

long long int dfs(int u, int t, long long int nv) {
	if (u == t) return nv;
	int temp;
	for (int &i = iter[u]; i < int(graph[u].size()); i++) {
		Edge& nxt = graph[u][i];
		if (nxt.cap > 0 && dep[nxt.to] > dep[u]) {
			temp = dfs(nxt.to, t, min(nv, nxt.cap));
			if (temp > 0) {
				nxt.cap -= temp;
				graph[nxt.to][nxt.rev].cap += temp;
				return temp;
			}
		}
	}
	return 0;
}

long long int dinic(int s, int t) {
	long long int ans = 0, f;
	int cnt = 0;
	for (int i = 0; i < kN; i++) went[i] = cnt;
	while (true) {
		bfs(s, ++cnt);
		if (went[s] != went[t]) break;
		while ((f = dfs(s, t, kInf)) > 0) ans += f;
	}
	return ans;
}

long long int a[kNN], g[kNN][kNN], r[kNN], c[kNN];

int main() {
	int n, m, s, t;
	long long int ans = 0, tmp;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%lld", &g[i][j]);
	for (int i = 1; i <= n; i++) scanf("%lld", &r[i]);
	for (int i = 1; i <= m; i++) scanf("%lld", &c[i]);
	s = 0;
	t = n + m + 1;
	for (int i = 1; i <= n; i++) a[i] = 0;
	for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) a[i] += g[i][j];
	for (int i = 1; i <= n; i++) {
		tmp = min(r[i], a[i]);
		r[i] -= tmp;
		a[i] -= tmp;
	}
	for (int i = 1; i <= n; i++) ans += r[i];
	for (int i = 1; i <= m; i++) ans += c[i];
	for (int i = 1; i <= n; i++) AddEdge(s, i, a[i]);
	for (int i = 1; i <= m; i++) AddEdge(i + n, t, c[i]);
	for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) AddEdge(i, j + n, g[i][j]);
	printf("%lld\n", ans - dinic(s, t));
}
0