답안 #163283

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
163283 2019-11-12T11:01:09 Z MvC Cities (BOI16_cities) C++11
100 / 100
2966 ms 72380 KB
#include <iostream>
#include <climits>
#include <queue>

using namespace std;
typedef long long int Z;

int n, k, m;
vector<int> imp;
vector<vector<pair<int, Z>>> G;
Z d[32][202020];
Z huge = LLONG_MAX / 3;
bool seen[202020];

int main() {
	cin.sync_with_stdio(false);
	
	cin >> n >> k >> m;
	imp.resize(k);
	for(int i = 0; i < k; ++i) {
		cin >> imp[i];
		--imp[i];
	}
	
	G.resize(n);
	for(int i = 0; i < m; ++i) {
		int a, b;
		Z c;
		cin >> a >> b >> c;
		--a;
		--b;
		G[a].emplace_back(b, c);
		G[b].emplace_back(a, c);
	}
	
	fill((Z*)d, (Z*)d + 32 * 202020, huge);
	
	for(int i = 0; i < k; ++i) {
		d[1 << i][imp[i]] = 0;
	}
	
	priority_queue<pair<Z, int>> Q;
	for(int c = 1; c < (1 << k); ++c) {
		for(int a = 0; a < c; ++a) {
			if((a | c) != c) continue;
			int b = c ^ a;
			if(b > a) continue;
			for(int v = 0; v < n; ++v) {
				d[c][v] = min(d[c][v], d[a][v] + d[b][v]);
			}
		}
		fill(seen, seen + n, false);
		for(int v = 0; v < n; ++v) {
			if(d[c][v] == huge) continue;
			Q.emplace(-d[c][v], v);
		}
		if(c==(1<<k)-1)return cout<<-Q.top().first<<'\n',0;
		while(!Q.empty()) {
			Z cost = -Q.top().first;
			int v = Q.top().second;
			Q.pop();
			if(seen[v]) continue;
			seen[v] = true;
			for(auto edge : G[v]) {
				Z ec = cost + edge.second;
				if(ec < d[c][edge.first]) {
					d[c][edge.first] = ec;
					Q.emplace(-ec, edge.first);
				}
			}
		}
	}
	
	Z res = huge;
	for(int v = 0; v < n; ++v) {
		res = min(res, d[(1 << k) - 1][v]);
	}
	cout << res << '\n';
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 51064 KB Output is correct
2 Correct 44 ms 50936 KB Output is correct
3 Correct 44 ms 50908 KB Output is correct
4 Correct 46 ms 50936 KB Output is correct
5 Correct 44 ms 50948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 643 ms 72272 KB Output is correct
2 Correct 625 ms 71432 KB Output is correct
3 Correct 407 ms 62956 KB Output is correct
4 Correct 177 ms 63480 KB Output is correct
5 Correct 596 ms 70256 KB Output is correct
6 Correct 170 ms 63480 KB Output is correct
7 Correct 47 ms 51196 KB Output is correct
8 Correct 45 ms 51064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 51172 KB Output is correct
2 Correct 50 ms 51164 KB Output is correct
3 Correct 48 ms 51204 KB Output is correct
4 Correct 47 ms 51064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1322 ms 72312 KB Output is correct
2 Correct 1233 ms 71384 KB Output is correct
3 Correct 928 ms 63084 KB Output is correct
4 Correct 756 ms 68932 KB Output is correct
5 Correct 269 ms 64524 KB Output is correct
6 Correct 153 ms 65656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2525 ms 72380 KB Output is correct
2 Correct 2966 ms 72336 KB Output is correct
3 Correct 2506 ms 71304 KB Output is correct
4 Correct 1925 ms 62956 KB Output is correct
5 Correct 1392 ms 68976 KB Output is correct
6 Correct 402 ms 64928 KB Output is correct
7 Correct 165 ms 65528 KB Output is correct