結果

問題 No.114 遠い未来
ユーザー krotonkroton
提出日時 2014-12-29 01:37:58
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,519 bytes
コンパイル時間 2,940 ms
コンパイル使用メモリ 148,948 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-03 19:51:43
合計ジャッジ時間 140,802 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,723 ms
4,376 KB
testcase_01 AC 4,723 ms
4,380 KB
testcase_02 AC 4,740 ms
4,376 KB
testcase_03 AC 4,723 ms
4,380 KB
testcase_04 AC 4,726 ms
4,380 KB
testcase_05 AC 4,723 ms
4,380 KB
testcase_06 AC 4,724 ms
4,380 KB
testcase_07 AC 4,724 ms
4,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 4,726 ms
4,380 KB
testcase_11 AC 4,725 ms
4,380 KB
testcase_12 AC 4,725 ms
4,380 KB
testcase_13 AC 4,723 ms
4,380 KB
testcase_14 AC 4,725 ms
4,380 KB
testcase_15 AC 4,725 ms
4,376 KB
testcase_16 AC 4,723 ms
4,380 KB
testcase_17 AC 4,725 ms
4,380 KB
testcase_18 AC 4,723 ms
4,380 KB
testcase_19 AC 4,725 ms
4,380 KB
testcase_20 AC 4,724 ms
4,376 KB
testcase_21 AC 4,724 ms
4,376 KB
testcase_22 AC 4,724 ms
4,376 KB
testcase_23 AC 4,726 ms
4,376 KB
testcase_24 AC 4,729 ms
4,380 KB
testcase_25 AC 4,723 ms
4,376 KB
testcase_26 AC 4,721 ms
4,380 KB
testcase_27 AC 4,723 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int INF = 1 << 26;
struct Edge {
	int u, v, c;
	bool operator<(const Edge& other) const {
		return c < other.c;
	}
};

int N, M, T, ts[50];
Edge E[777];
int dist[50][50];

int root[50];
inline int find(int x){
	if(x == root[x])return x;
	return root[x] = find(root[x]);
}
inline bool conn(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y)return false;

	root[x] = y;
	return true;
}


int rest_v[50], use_rest[50];
int use[50];
inline int min_spanning_tree(int now){
	for(int i=0;i<N;i++)root[i] = i;

	int res = 0;
	for(int i=0;i<M;i++){
		int u = E[i].u, v = E[i].v, c = E[i].c;
		if(!use[u] || !use[v])continue;
		if(conn(u, v)){
			res += c;
			if(res >= now)break;
		}
	}

	if(res >= now)return INF;

	for(int i=0;i<T;i++){
		if(find(ts[i]) != find(ts[0]))return INF;
	}

	return res;
}

int main(){
	clock_t start = clock();

	cin >> N >> M >> T;
	for(int i=0;i<M;i++){
		int u, v, c;
		cin >> u >> v >> c;
		E[i] = Edge{u - 1, v - 1, c};
	}
	for(int i=0;i<T;i++)cin >> ts[i], ts[i]--;

	int res = INF;
	for(int i=0;i<T;i++)use[ts[i]] = true;

	int rest = 0;
	for(int i=0;i<N;i++)if(!use[i]){
		rest_v[rest++] = i;
	}

	sort(E, E + M);
	
	while(clock() - start <= 4.7 * CLOCKS_PER_SEC){
		int sz = 0;
		for(int i=0;i<rest;i++){
			if(rand() & 1){
				use[rest_v[i]] = true;
				use_rest[sz++] = rest_v[i];
			}
		}

		res = min(res, min_spanning_tree(res));

		for(int i=0;i<sz;i++){
			use[use_rest[i]] = false;
		}
	}

	cout << res << endl;
	return 0;
}
0