답안 #204835

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
204835 2020-02-27T10:36:04 Z nickyrio Cities (BOI16_cities) C++17
100 / 100
2413 ms 72172 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);
		}
		
		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 32 ms 50936 KB Output is correct
2 Correct 33 ms 50940 KB Output is correct
3 Correct 32 ms 50932 KB Output is correct
4 Correct 34 ms 50936 KB Output is correct
5 Correct 33 ms 50936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 665 ms 68332 KB Output is correct
2 Correct 623 ms 67556 KB Output is correct
3 Correct 383 ms 61164 KB Output is correct
4 Correct 117 ms 63352 KB Output is correct
5 Correct 377 ms 72172 KB Output is correct
6 Correct 114 ms 63352 KB Output is correct
7 Correct 41 ms 51184 KB Output is correct
8 Correct 33 ms 51192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 51192 KB Output is correct
2 Correct 37 ms 51112 KB Output is correct
3 Correct 38 ms 51064 KB Output is correct
4 Correct 35 ms 51064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1229 ms 68332 KB Output is correct
2 Correct 1167 ms 71384 KB Output is correct
3 Correct 812 ms 62956 KB Output is correct
4 Correct 684 ms 68972 KB Output is correct
5 Correct 217 ms 64612 KB Output is correct
6 Correct 126 ms 65656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2413 ms 68580 KB Output is correct
2 Correct 2413 ms 72172 KB Output is correct
3 Correct 2340 ms 71264 KB Output is correct
4 Correct 1599 ms 62956 KB Output is correct
5 Correct 1263 ms 68972 KB Output is correct
6 Correct 323 ms 64864 KB Output is correct
7 Correct 135 ms 65528 KB Output is correct