結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,716 KB
testcase_01 AC 4 ms
6,632 KB
testcase_02 AC 4 ms
6,484 KB
testcase_03 AC 237 ms
18,768 KB
testcase_04 AC 194 ms
17,856 KB
testcase_05 AC 183 ms
19,044 KB
testcase_06 AC 171 ms
19,800 KB
testcase_07 AC 220 ms
18,108 KB
testcase_08 AC 108 ms
19,032 KB
testcase_09 AC 102 ms
19,020 KB
testcase_10 AC 119 ms
19,540 KB
testcase_11 AC 121 ms
19,172 KB
testcase_12 AC 112 ms
18,904 KB
testcase_13 AC 87 ms
16,832 KB
testcase_14 AC 124 ms
20,080 KB
testcase_15 AC 114 ms
18,760 KB
testcase_16 AC 96 ms
16,896 KB
testcase_17 AC 97 ms
18,264 KB
testcase_18 AC 958 ms
18,564 KB
testcase_19 AC 1,058 ms
19,068 KB
testcase_20 AC 1,126 ms
19,312 KB
testcase_21 AC 1,202 ms
19,532 KB
testcase_22 AC 1,154 ms
19,852 KB
testcase_23 AC 1,390 ms
20,344 KB
testcase_24 AC 1,318 ms
20,660 KB
testcase_25 AC 1,543 ms
20,872 KB
testcase_26 AC 1,482 ms
20,744 KB
testcase_27 AC 1,438 ms
20,772 KB
testcase_28 AC 1,546 ms
20,908 KB
testcase_29 AC 1,462 ms
20,928 KB
testcase_30 AC 1,484 ms
20,800 KB
testcase_31 AC 970 ms
18,736 KB
testcase_32 AC 1,018 ms
18,908 KB
testcase_33 AC 1,065 ms
19,284 KB
testcase_34 AC 1,254 ms
19,528 KB
testcase_35 AC 1,170 ms
19,844 KB
testcase_36 AC 1,306 ms
20,204 KB
testcase_37 AC 1,324 ms
20,788 KB
testcase_38 AC 1,567 ms
20,784 KB
testcase_39 AC 1,533 ms
20,808 KB
testcase_40 AC 1,449 ms
20,884 KB
testcase_41 AC 49 ms
20,132 KB
testcase_42 AC 100 ms
20,616 KB
testcase_43 AC 52 ms
20,120 KB
testcase_44 AC 54 ms
20,940 KB
testcase_45 AC 4 ms
6,208 KB
testcase_46 AC 5 ms
6,496 KB
testcase_47 AC 4 ms
6,608 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of ‘void Dinic<MAXV, T>::addEdge(int, int, T, bool) [with int MAXV = 100000; T = int]’:
main.cpp:86:13:   required from here
main.cpp:17:41: 警告: narrowing conversion of ‘((Dinic<100000, int>*)this)->Dinic<100000, int>::adj[b].std::vector<Dinic<100000, int>::edge, std::allocator<Dinic<100000, int>::edge> >::size()’ from ‘std::vector<Dinic<100000, int>::edge, std::allocator<Dinic<100000, int>::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<100000, int>*)this)->Dinic<100000, int>::adj[a].std::vector<Dinic<100000, int>::edge, std::allocator<Dinic<100000, int>::edge> >::size() - 1)’ from ‘std::vector<Dinic<100000, int>::edge, std::allocator<Dinic<100000, int>::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 << 30) : 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<100000, int> 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