結果

問題 No.1678 Coin Trade (Multiple)
ユーザー SSRSSSRS
提出日時 2021-09-10 21:46:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 684 ms / 5,000 ms
コード長 2,556 bytes
コンパイル時間 2,553 ms
コンパイル使用メモリ 184,204 KB
実行使用メモリ 15,776 KB
最終ジャッジ日時 2023-09-02 16:44:01
合計ジャッジ時間 14,766 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 60 ms
8,372 KB
testcase_04 AC 288 ms
12,080 KB
testcase_05 AC 153 ms
14,508 KB
testcase_06 AC 119 ms
13,368 KB
testcase_07 AC 239 ms
9,164 KB
testcase_08 AC 167 ms
10,016 KB
testcase_09 AC 166 ms
14,432 KB
testcase_10 AC 44 ms
5,504 KB
testcase_11 AC 221 ms
11,576 KB
testcase_12 AC 40 ms
5,608 KB
testcase_13 AC 499 ms
13,692 KB
testcase_14 AC 120 ms
8,012 KB
testcase_15 AC 171 ms
11,460 KB
testcase_16 AC 67 ms
11,380 KB
testcase_17 AC 173 ms
14,576 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 684 ms
14,856 KB
testcase_49 AC 657 ms
14,784 KB
testcase_50 AC 646 ms
14,996 KB
testcase_51 AC 649 ms
14,812 KB
testcase_52 AC 632 ms
14,720 KB
testcase_53 AC 636 ms
14,788 KB
testcase_54 AC 635 ms
14,760 KB
testcase_55 AC 619 ms
14,996 KB
testcase_56 AC 654 ms
14,840 KB
testcase_57 AC 641 ms
14,992 KB
testcase_58 AC 322 ms
15,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000;
const long long INF2 = 1000000000000000000;
template <typename Cap, typename Cost>
struct primal_dual{
	struct edge{
		int to, rev;
		Cap cap;
		Cost cost;
		edge(int to, int rev, Cap cap, Cost cost): to(to), rev(rev), cap(cap), cost(cost){
		}
	};
	int N;
	vector<vector<edge>> G;
	primal_dual(){
	}
	primal_dual(int N): N(N), G(N){
	}
	void add_edge(int from, int to, Cap cap, Cost cost){
		int id1 = G[from].size();
		int id2 = G[to].size();
		G[from].push_back(edge(to, id2, cap, cost));
		G[to].push_back(edge(from, id1, 0, - cost));
	}
	pair<Cap, Cost> min_cost_flow(int s, int t, Cap F){
		Cap flow = 0;
		Cost cost = 0;
		vector<Cost> h(N, 0);
		while (flow < F){
			vector<Cap> m(N, INF2);
			vector<Cost> d(N, INF2);
			vector<int> pv(N, -1);
			vector<int> pe(N, -1);
			vector<bool> used(N, false);
			priority_queue<pair<Cost, int>, vector<pair<Cost, int>>, greater<pair<Cost, int>>> pq;
			pq.push(make_pair(0, s));
			d[s] = 0;
			while (!pq.empty()){
				int v = pq.top().second;
				pq.pop();
				if (!used[v]){
					used[v] = true;
					if (v == t){
						break;
					}
					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){
							Cost tmp = G[v][i].cost - h[w] + h[v];
							if (d[w] > d[v] + tmp){
								d[w] = d[v] + tmp;
								m[w] = min(m[v], G[v][i].cap);
								pv[w] = v;
								pe[w] = i;
								pq.push(make_pair(d[w], w));
							}
						}
					}
				}
			}
			if (!used[t]){
				break;
			}
			for (int i = 0; i < N; i++){
				if (used[i]){
					h[i] -= d[t] - d[i];
				}
			}
			Cap c = min(m[t], F - flow);
			for (int i = t; i != s; i = pv[i]){
				G[pv[i]][pe[i]].cap -= c;
				G[i][G[pv[i]][pe[i]].rev].cap += c;
			}
			flow += c;
			cost += c * (- h[s]);
		}
		return make_pair(flow, cost);
	}
};
int main(){
  int N, K;
  cin >> N >> K;
  vector<int> A(N), M(N);
  vector<vector<int>> B(N);
  for (int i = 0; i < N; i++){
    cin >> A[i] >> M[i];
    B[i] = vector<int>(M[i]);
    for (int j = 0; j < M[i]; j++){
      cin >> B[i][j];
      B[i][j]--;
    }
  }
  primal_dual<long long, long long> G(N);
  for (int i = 0; i < N - 1; i++){
    G.add_edge(i, i + 1, K, INF);
  }
  for (int i = 0; i < N; i++){
    for (int j = 0; j < M[i]; j++){
      if (A[i] - A[B[i][j]] > 0){
        G.add_edge(B[i][j], i, 1, INF * (i - B[i][j]) - (A[i] - A[B[i][j]]));
      }
    }
  }
  cout << (N - 1) * K * INF - G.min_cost_flow(0, N - 1, K).second << endl;
}
0