답안 #253894

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253894 2020-07-29T04:31:43 Z super_j6 Cities (BOI16_cities) C++14
100 / 100
2203 ms 46572 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;
int a[mxk];
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 491 ms 42340 KB Output is correct
2 Correct 482 ms 41416 KB Output is correct
3 Correct 287 ms 35180 KB Output is correct
4 Correct 90 ms 36612 KB Output is correct
5 Correct 241 ms 39412 KB Output is correct
6 Correct 89 ms 36728 KB Output is correct
7 Correct 22 ms 27904 KB Output is correct
8 Correct 15 ms 27904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 27904 KB Output is correct
2 Correct 19 ms 27904 KB Output is correct
3 Correct 23 ms 27776 KB Output is correct
4 Correct 16 ms 27904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1101 ms 42392 KB Output is correct
2 Correct 963 ms 41660 KB Output is correct
3 Correct 669 ms 35236 KB Output is correct
4 Correct 602 ms 40300 KB Output is correct
5 Correct 179 ms 37468 KB Output is correct
6 Correct 102 ms 38904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2168 ms 42476 KB Output is correct
2 Correct 2203 ms 46572 KB Output is correct
3 Correct 2033 ms 45640 KB Output is correct
4 Correct 1439 ms 37356 KB Output is correct
5 Correct 1130 ms 44520 KB Output is correct
6 Correct 277 ms 41236 KB Output is correct
7 Correct 117 ms 42216 KB Output is correct