結果
問題 | No.19 ステージの選択 |
ユーザー | r_dream0 |
提出日時 | 2017-02-09 15:15:42 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,445 bytes |
コンパイル時間 | 1,074 ms |
コンパイル使用メモリ | 76,492 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-07 18:00:12 |
合計ジャッジ時間 | 2,145 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | WA | - |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | WA | - |
ソースコード
#include <iostream> #include <vector> using namespace std; typedef vector<vector<int> > matrix; typedef int weight; const int inf = 1 << 30; /* FROM Spaghetti Source */ void backward_traverse(int v, int s, int r, matrix &g, vector<int> &no, vector< vector<int> > &comp, vector<int> &prev, vector<weight> &mcost, vector<int> &mark, weight &cost, bool &found) { const int n = g.size(); if (mark[v]) { vector<int> temp = no; found = true; do { cost += mcost[v]; v = prev[v]; if (v != s) { while (comp[v].size() > 0) { no[comp[v].back()] = s; comp[s].push_back(comp[v].back()); comp[v].pop_back(); } } } while (v != s); for (int j = 0; j < n; ++j) if (j != r && no[j] == s) for (int i = 0; i < n; ++i) if (no[i] != s && g[i][j] < inf) g[i][j] -= mcost[ temp[j] ]; } mark[v] = true; for (int i = 0; i < n; ++i) if (no[i] != no[v] && prev[ no[i] ] == v) if (!mark[ no[i] ] || i == s) backward_traverse(i, s, r, g, no, comp, prev, mcost, mark, cost, found); } weight minimum_spanning_arborescence(int r, matrix &g) { const int n = g.size(); vector<int> no(n); vector< vector<int> > comp(n); for (int i = 0; i < n; ++i) { no[i] = i; comp[i].push_back(i); } weight cost = 0; while (1) { vector<int> prev(n, -1); vector<weight> mcost(n, inf); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j == r) continue; if (no[i] != no[j] && g[i][j] < inf) { if (g[i][j] < mcost[ no[j] ]) { mcost[ no[j] ] = g[i][j]; prev[ no[j] ] = no[i]; } } } } bool stop = true; vector<int> mark(n); for (int i = 0; i < n; ++i) { if (i == r || mark[i] || comp[i].size() == 0) continue; bool found = false; backward_traverse(i, i, r, g, no, comp, prev, mcost, mark, cost, found); if (found) stop = false; } if (stop) { for (int i = 0; i < n; ++i) if (prev[i] >= 0) cost += mcost[i]; return cost; } } } int main() { int32_t N; cin >> N; matrix M(N + 1, vector<int>(N + 1, inf)); int L[N], S[N]; for(int i = 0; i < N; i++) { cin >> L[i] >> S[i]; M[0][i + 1] = L[i] * 2; M[S[i]][i + 1] = L[i]; } cout << minimum_spanning_arborescence(0, M) * 0.5 << endl; }