結果

問題 No.1541 ゅゅさんのテスト勉強
ユーザー SSRSSSRS
提出日時 2021-06-06 18:22:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,996 bytes
コンパイル時間 1,893 ms
コンパイル使用メモリ 178,948 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 00:33:37
合計ジャッジ時間 3,675 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 7 ms
4,376 KB
testcase_30 AC 7 ms
4,376 KB
testcase_31 AC 7 ms
4,376 KB
testcase_32 AC 7 ms
4,376 KB
testcase_33 AC 7 ms
4,384 KB
testcase_34 AC 7 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 10000000000000000;
template <typename Cap>
struct ford_fulkerson{
	struct edge{
		int to, rev;
		Cap cap;
		edge(int to, int rev, Cap cap): to(to), rev(rev), cap(cap){
		}
	};
	int N;
	vector<vector<edge>> G;
	ford_fulkerson(){
	}
	ford_fulkerson(int N): N(N), G(N){
	}
	void add_edge(int from, int to, Cap cap){
		int id1 = G[from].size();
		int id2 = G[to].size();
		G[from].push_back(edge(to, id2, cap));
		G[to].push_back(edge(from, id1, 0));
	}
	Cap max_flow(int s, int t){
		Cap flow = 0;
		while (1){
			vector<Cap> m(N, INF);
			vector<int> pv(N, -1);
			vector<int> pe(N, -1);
			vector<bool> used(N, false);
			queue<int> Q;
			Q.push(s);
			used[s] = true;
			while (!Q.empty()){
				int v = Q.front();
				Q.pop();
				int cnt = G[v].size();
				for (int i = 0; i < cnt; i++){
					int w = G[v][i].to;
					if (!used[w] && G[v][i].cap > 0){
						used[w] = true;
						m[w] = min(m[v], G[v][i].cap);
						pv[w] = v;
						pe[w] = i;
						Q.push(w);
					}
				}
			}
			if (!used[t]){
				break;
			}
			Cap f = m[t];
			for (int i = t; i != s; i = pv[i]){
				G[pv[i]][pe[i]].cap -= f;
				G[i][G[pv[i]][pe[i]].rev].cap += f;
			}
			flow += f;
		}
		return flow;
	}
};
int main(){
  int N, M;
  cin >> N >> M;
  vector<long long> S(N, M);
  ford_fulkerson<long long> G(N + 2);
  for (int i = 0; i < N; i++){
    int k, C;
    cin >> k >> C;
    S[i] -= C;
    vector<int> A(k);
    for (int j = 0; j < k; j++){
      cin >> A[j];
      A[j]--;
    }
    vector<int> B(k);
    for (int j = 0; j < k; j++){
      cin >> B[j];
    }
    for (int j = 0; j < k; j++){
      S[A[j]] += B[j];
      G.add_edge(i, A[j], B[j]);
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++){
    if (S[i] > 0){
      ans += S[i];
      G.add_edge(N, i, 0);
      G.add_edge(i, N + 1, S[i]);
    } else {
      G.add_edge(N, i, -S[i]);
      G.add_edge(i, N + 1, 0);
    }
  }
  cout << ans - G.max_flow(N, N + 1) << endl;
}
0