結果

問題 No.19 ステージの選択
ユーザー yuustiyuusti
提出日時 2014-12-06 03:13:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,918 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 92,432 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 23:26:49
合計ジャッジ時間 1,719 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 4 ms
4,384 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 2 ms
4,376 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 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <climits>
#include <bitset>
#include <cassert>
#include <random>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int N = 110;

int dist[N][N];
int L[N], S[N];

struct UnionFind{
	int par[N];
	UnionFind(int n){
		rep(i, n) par[i] = i;
	}

	int find(int x){
		if (par[x] == x) return x;
		return par[x] = find(par[x]);
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}

	void unite(int x, int y){
		par[find(x)] = find(y);
	}
};

int used[N];
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	int ans = 0;
	rep(i, n){
		cin >> L[i] >> S[i];
		--S[i];
		dist[S[i]][i] = 1;
		if (S[i] == i) used[i] = 1, ans += L[i] * 2;
	}

	rep(i, n) rep(j, n) rep(k, n) dist[j][k] = dist[j][k] || (dist[j][i] && dist[i][k]);

	UnionFind uf(n);

	rep(i, n) rep(j, n){
		if (i == j)continue;
		if (dist[i][j] && dist[j][i]) uf.unite(i, j);
	}

	vector<int> v[110];
	rep(i, n) if (!used[i]) v[uf.find(i)].push_back(L[i]);

	rep(i, n){
		ans += accumulate(ALL(v[i]), 0);
		if (v[i].size() > 1){
			ans += *min_element(ALL(v[i]));
		}
	}

	cout.setf(ios::fixed);
	cout.precision(1);
	cout << ans / 2. << endl;
	

	return 0;
}
0