結果

問題 No.114 遠い未来
ユーザー HIR180HIR180
提出日時 2020-04-22 14:21:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,830 ms / 5,000 ms
コード長 3,161 bytes
コンパイル時間 3,426 ms
コンパイル使用メモリ 228,660 KB
実行使用メモリ 12,844 KB
最終ジャッジ日時 2024-04-19 14:54:15
合計ジャッジ時間 16,593 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
12,664 KB
testcase_01 AC 617 ms
12,800 KB
testcase_02 AC 1,783 ms
12,748 KB
testcase_03 AC 30 ms
5,376 KB
testcase_04 AC 6 ms
12,784 KB
testcase_05 AC 9 ms
12,844 KB
testcase_06 AC 1,797 ms
12,744 KB
testcase_07 AC 4 ms
12,640 KB
testcase_08 AC 5 ms
12,640 KB
testcase_09 AC 9 ms
12,672 KB
testcase_10 AC 87 ms
12,692 KB
testcase_11 AC 206 ms
12,800 KB
testcase_12 AC 620 ms
12,800 KB
testcase_13 AC 601 ms
12,800 KB
testcase_14 AC 1,830 ms
12,788 KB
testcase_15 AC 1,788 ms
12,800 KB
testcase_16 AC 261 ms
5,376 KB
testcase_17 AC 276 ms
5,376 KB
testcase_18 AC 1,747 ms
5,376 KB
testcase_19 AC 116 ms
5,376 KB
testcase_20 AC 212 ms
5,376 KB
testcase_21 AC 10 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Let's join Kaede Takagaki Fan Club !!
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}

int n, m, t;
int w[36][36];
vector<P1>edge;
vector<int>imp;

int dp[(1<<16)][36];
void solve_steiner(){
	repn(i, (1<<16)-1) rep(j, 36) dp[i][j] = INF;
	rep(i, imp.size()){
		rep(j, n){
			dp[(1<<i)][j] = w[imp[i]][j];
		}
	}
	for(int x=0;x<(1<<t);x++){
		if(__builtin_popcount(x) <= 1) continue;
		int sub = x;
		while(1){
			rep(y, n) chmin(dp[x][y], dp[sub][y] + dp[sub^x][y]);
			if(sub == 0) break;
			sub = (sub-1) & x;
		}
		rep(y, n) rep(z, n){
			chmin(dp[x][y], dp[x][z] + w[y][z]);
		}
	}
	int ans = INF;
	rep(x, n) chmin(ans, dp[(1<<t)-1][x]);
	cout << ans << endl;
}
struct uf{
	int par[36];
	void init(){ rep(i, 36) par[i] = i; }
	int find(int x){ if(x == par[x]) return x; else return par[x] = find(par[x]); }
	void unite(int x, int y){
		x = find(x); y = find(y); if(x == y) return;
		par[x] = y;
	}
	bool same(int x, int y){ return find(x) == find(y); }
}kaede;

void solve_MST(){
	vector<int>ump;
	bool ex[36]={};
	rep(i, imp.size()) ex[imp[i]] = 1;
	rep(i, n) if(!ex[i]) ump.pb(i);
	int sz = ump.size(), ans = INF;
	rep(mask, (1<<sz)){
		int N = t + __builtin_popcount(mask);
		int cnt = 0, use = 0;
		bool eex[36] = {};
		rep(i, sz) if(((mask>>i)&1)) eex[ump[i]] = 1;
		kaede.init();
		rep(i, edge.size()){
			int a = edge[i].sc.fi, b = edge[i].sc.sc;
			if(!ex[a] && !eex[a]) continue;
			if(!ex[b] && !eex[b]) continue;
			if(kaede.same(a, b)) continue;
			kaede.unite(a, b);
			cnt ++;
			use += edge[i].fi;
		}
		if(cnt == N-1) chmin(ans, use);
	}
	cout << ans << endl;
}
int main(){
	cin >> n >> m >> t;
	rep(i, 36) rep(j, 36){
		if(i != j) w[i][j] = 1e7;
	}
	rep(i, m){
		int a, b, c; cin >> a >> b >> c;
		a --; b --; 
		w[a][b] = w[b][a] = c;
		edge.pb(mp(c, mp(a, b)));
	}
	sort(all(edge));
	imp.resize(t);
	rep(i, t){
		cin >> imp[i];
		imp[i] --;
	}
	rep(x, n) rep(i, n) rep(j, n) chmin(w[i][j], w[i][x] + w[x][j]);
	if(t <= 16) solve_steiner();
	else solve_MST();
}
0