답안 #253895

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253895 2020-07-29T04:47:51 Z super_j6 Cities (BOI16_cities) C++14
100 / 100
2256 ms 42592 KB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<ll, ll>
#define f first
#define s second

const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mxn = 100000, mxk = 5;
int n, m, k;
ll d[1 << mxk][mxn];
vector<pi> g[mxn];
priority_queue<pi, vector<pi>, greater<pi>> pq;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> k >> m;
	
	memset(d, 0x3f, sizeof(d));
	memset(d[0], 0, sizeof(d[0]));
	for(int i = 0; i < k; i++){
		int x;
		cin >> x;
		d[1 << i][--x] = 0;
	}
	
	for(int i = 0; i < m; i++){
		int u, v, w;
		cin >> u >> v >> w;
		u--, v--;
		g[u].push_back({v, w});
		g[v].push_back({u, w});
	}
	
	ll ret = inf;
	for(int l = 1; l < (1 << k); l++){
		for(int i = 0; i < n; i++){
			for(int j = (l - 1) & l; j; j = (j - 1) & l){
				d[l][i] = min(d[l][i], d[j][i] + d[l ^ j][i]);
			} 
			if(l == (1 << k) - 1) ret = min(ret, d[l][i]);
			else if(d[l][i] != inf) pq.push({d[l][i], i});
		}
		while(!pq.empty()){
			ll dc = pq.top().f, c = pq.top().s;
			pq.pop();
			if(dc != d[l][c]) continue;
			for(pi i : g[c]){
				if(dc + i.s < d[l][i.f]){
					pq.push({d[l][i.f] = dc + i.s, i.f});
				}
			}
		}
	}
	
	cout << ret << endl;

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 27776 KB Output is correct
2 Correct 14 ms 27776 KB Output is correct
3 Correct 14 ms 27776 KB Output is correct
4 Correct 13 ms 27776 KB Output is correct
5 Correct 18 ms 27776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 468 ms 42380 KB Output is correct
2 Correct 441 ms 41444 KB Output is correct
3 Correct 255 ms 35208 KB Output is correct
4 Correct 90 ms 36600 KB Output is correct
5 Correct 240 ms 39284 KB Output is correct
6 Correct 85 ms 36600 KB Output is correct
7 Correct 17 ms 27904 KB Output is correct
8 Correct 14 ms 27836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 27904 KB Output is correct
2 Correct 18 ms 27904 KB Output is correct
3 Correct 16 ms 27776 KB Output is correct
4 Correct 18 ms 27904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1141 ms 42592 KB Output is correct
2 Correct 910 ms 41464 KB Output is correct
3 Correct 727 ms 35180 KB Output is correct
4 Correct 623 ms 40300 KB Output is correct
5 Correct 191 ms 37340 KB Output is correct
6 Correct 94 ms 38904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2132 ms 42348 KB Output is correct
2 Correct 2256 ms 42348 KB Output is correct
3 Correct 2043 ms 41440 KB Output is correct
4 Correct 1649 ms 35180 KB Output is correct
5 Correct 1061 ms 40296 KB Output is correct
6 Correct 279 ms 37340 KB Output is correct
7 Correct 110 ms 38780 KB Output is correct