結果

問題 No.957 植林
ユーザー uwiuwi
提出日時 2019-12-20 20:17:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,261 ms / 2,000 ms
コード長 2,662 bytes
コンパイル時間 2,154 ms
コンパイル使用メモリ 214,328 KB
実行使用メモリ 21,352 KB
最終ジャッジ日時 2024-07-16 04:02:17
合計ジャッジ時間 30,683 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,816 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 205 ms
18,888 KB
testcase_04 AC 169 ms
18,184 KB
testcase_05 AC 164 ms
19,412 KB
testcase_06 AC 148 ms
20,024 KB
testcase_07 AC 195 ms
18,584 KB
testcase_08 AC 94 ms
19,268 KB
testcase_09 AC 94 ms
19,132 KB
testcase_10 AC 112 ms
19,944 KB
testcase_11 AC 111 ms
19,392 KB
testcase_12 AC 103 ms
19,216 KB
testcase_13 AC 79 ms
16,952 KB
testcase_14 AC 100 ms
20,288 KB
testcase_15 AC 100 ms
19,016 KB
testcase_16 AC 81 ms
17,096 KB
testcase_17 AC 84 ms
18,364 KB
testcase_18 AC 853 ms
19,012 KB
testcase_19 AC 910 ms
19,400 KB
testcase_20 AC 972 ms
19,540 KB
testcase_21 AC 1,043 ms
19,924 KB
testcase_22 AC 1,032 ms
20,160 KB
testcase_23 AC 1,184 ms
20,576 KB
testcase_24 AC 1,127 ms
20,964 KB
testcase_25 AC 1,258 ms
21,100 KB
testcase_26 AC 1,205 ms
21,160 KB
testcase_27 AC 1,154 ms
21,212 KB
testcase_28 AC 1,180 ms
21,212 KB
testcase_29 AC 1,164 ms
21,100 KB
testcase_30 AC 1,243 ms
21,224 KB
testcase_31 AC 845 ms
19,012 KB
testcase_32 AC 916 ms
19,148 KB
testcase_33 AC 955 ms
19,412 KB
testcase_34 AC 1,012 ms
19,840 KB
testcase_35 AC 1,033 ms
20,312 KB
testcase_36 AC 1,184 ms
20,576 KB
testcase_37 AC 1,131 ms
20,692 KB
testcase_38 AC 1,261 ms
20,968 KB
testcase_39 AC 1,202 ms
21,196 KB
testcase_40 AC 1,181 ms
21,200 KB
testcase_41 AC 43 ms
20,588 KB
testcase_42 AC 91 ms
20,968 KB
testcase_43 AC 46 ms
20,796 KB
testcase_44 AC 48 ms
21,352 KB
testcase_45 AC 3 ms
6,944 KB
testcase_46 AC 5 ms
6,948 KB
testcase_47 AC 4 ms
6,944 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: warning: 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: warning: 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