結果

問題 No.957 植林
ユーザー uwiuwi
提出日時 2019-12-20 20:19:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,454 ms / 2,000 ms
コード長 2,656 bytes
コンパイル時間 2,368 ms
コンパイル使用メモリ 210,256 KB
実行使用メモリ 20,712 KB
最終ジャッジ日時 2023-09-23 03:51:53
合計ジャッジ時間 37,742 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,316 KB
testcase_01 AC 4 ms
6,252 KB
testcase_02 AC 4 ms
6,336 KB
testcase_03 AC 235 ms
18,284 KB
testcase_04 AC 193 ms
17,412 KB
testcase_05 AC 183 ms
18,676 KB
testcase_06 AC 158 ms
19,200 KB
testcase_07 AC 207 ms
17,932 KB
testcase_08 AC 106 ms
18,548 KB
testcase_09 AC 107 ms
18,548 KB
testcase_10 AC 125 ms
19,280 KB
testcase_11 AC 124 ms
18,812 KB
testcase_12 AC 120 ms
18,444 KB
testcase_13 AC 90 ms
16,468 KB
testcase_14 AC 136 ms
19,936 KB
testcase_15 AC 123 ms
18,720 KB
testcase_16 AC 93 ms
16,472 KB
testcase_17 AC 99 ms
17,712 KB
testcase_18 AC 953 ms
18,388 KB
testcase_19 AC 1,041 ms
18,728 KB
testcase_20 AC 1,124 ms
19,000 KB
testcase_21 AC 1,254 ms
19,364 KB
testcase_22 AC 1,220 ms
19,532 KB
testcase_23 AC 1,308 ms
19,904 KB
testcase_24 AC 1,192 ms
20,176 KB
testcase_25 AC 1,373 ms
20,660 KB
testcase_26 AC 1,299 ms
20,652 KB
testcase_27 AC 1,278 ms
20,600 KB
testcase_28 AC 1,306 ms
20,596 KB
testcase_29 AC 1,335 ms
20,632 KB
testcase_30 AC 1,454 ms
20,712 KB
testcase_31 AC 949 ms
18,476 KB
testcase_32 AC 998 ms
18,744 KB
testcase_33 AC 1,103 ms
19,016 KB
testcase_34 AC 1,214 ms
19,252 KB
testcase_35 AC 1,227 ms
19,648 KB
testcase_36 AC 1,383 ms
19,756 KB
testcase_37 AC 1,350 ms
20,060 KB
testcase_38 AC 1,432 ms
20,600 KB
testcase_39 AC 1,372 ms
20,624 KB
testcase_40 AC 1,426 ms
20,608 KB
testcase_41 AC 47 ms
19,804 KB
testcase_42 AC 99 ms
20,368 KB
testcase_43 AC 53 ms
19,804 KB
testcase_44 AC 54 ms
20,600 KB
testcase_45 AC 4 ms
5,984 KB
testcase_46 AC 4 ms
6,348 KB
testcase_47 AC 4 ms
6,260 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of ‘void Dinic<MAXV, T>::addEdge(int, int, T, bool) [with int MAXV = 91000; T = int]’:
main.cpp:86:13:   required from here
main.cpp:17:41: 警告: narrowing conversion of ‘((Dinic<91000>*)this)->Dinic<91000>::adj[b].std::vector<Dinic<91000>::edge, std::allocator<Dinic<91000>::edge> >::size()’ from ‘std::vector<Dinic<91000>::edge, std::allocator<Dinic<91000>::edge> >::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
   17 |         adj[a].push_back({b, adj[b].size(), cap, 0});
      |                              ~~~~~~~~~~~^~
main.cpp:18:44: 警告: narrowing conversion of ‘(((Dinic<91000>*)this)->Dinic<91000>::adj[a].std::vector<Dinic<91000>::edge, std::allocator<Dinic<91000>::edge> >::size() - 1)’ from ‘std::vector<Dinic<91000>::edge, std::allocator<Dinic<91000>::edge> >::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
   18 |         adj[b].push_back({a, adj[a].size() - 1, isDirected ? 0 : cap, 0});
      |                              ~~~~~~~~~~~~~~^~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <int MAXV, class T = int> struct Dinic {
    const static bool SCALING = true; // non-scaling = V^2E, Scaling=VElog(U) with higher constant
    int lim = 1;
    const T INF = numeric_limits<T>::max();
    struct edge {
        int to, rev;
        T cap, flow;
    };
    int s = MAXV - 2, t = MAXV - 1;

    int level[MAXV], ptr[MAXV];
    vector<edge> adj[MAXV];
    void addEdge(int a, int b, T cap, bool isDirected = true) {
        adj[a].push_back({b, adj[b].size(), cap, 0});
        adj[b].push_back({a, adj[a].size() - 1, isDirected ? 0 : cap, 0});
    }
    bool bfs() {
        queue<int> q({s});
        fill_n(level, MAXV, -1);
        level[s] = 0;
        while (!q.empty() && level[t] == -1) {
            int v = q.front();
            q.pop();
            for (auto e : adj[v]) {
                if (level[e.to] == -1 && e.flow < e.cap && (!SCALING || e.cap - e.flow >= lim)) {
                    q.push(e.to);
                    level[e.to] = level[v] + 1;
                }
            }
        }
        return level[t] != -1;
    }
    T dfs(int v, T flow) {
        if (v == t || !flow)
            return flow;
        for (; ptr[v] < adj[v].size(); ptr[v]++) {
            edge &e = adj[v][ptr[v]];
            if (level[e.to] != level[v] + 1)
                continue;
            if (T pushed = dfs(e.to, min(flow, e.cap - e.flow))) {
                e.flow += pushed;
                adj[e.to][e.rev].flow -= pushed;
                return pushed;
            }
        }
        return 0;
    }
    long long calc() {
        long long flow = 0;
        for (lim = SCALING ? (1 << 29) : 1; lim > 0; lim >>= 1) {
            while (bfs()) {
		        fill_n(ptr, MAXV, 0);
                while (T pushed = dfs(s, INF))
                    flow += pushed;
            }
        }
        return flow;
    }
};

int n, m;
int G[305][305];
int R[305];
int C[305];
Dinic<91000> D;

int main()
{
	scanf("%d %d", &n, &m);
	for(int i = 0;i < n;i++){
		for(int j = 0;j < m;j++){
			scanf("%d", &G[i][j]);
		}
	}
	for(int i = 0;i < n;i++){
		scanf("%d", &R[i]);
	}
	for(int i = 0;i < m;i++){
		scanf("%d", &C[i]);
	}
	for(int i = 0;i < n;i++){
		for(int j = 0;j < m;j++){
			D.addEdge(i*m+j, D.t, G[i][j]);
		}
	}
	long long ans = 0;
	for(int i = 0;i < n;i++){
		D.addEdge(D.s, n*m+i, R[i]);
		ans += R[i];
		for(int j = 0;j < m;j++){
			D.addEdge(n*m+i, i*m+j, R[i]);
		}
	}
	for(int i = 0;i < m;i++){
		D.addEdge(D.s, n*m+n+i, C[i]);
		ans += C[i];
		for(int j = 0;j < n;j++){
			D.addEdge(n*m+n+i, j*m+i, C[i]);
		}
	}

	ans -= D.calc();
	cout << ans << endl;

	return 0;
}
0