답안 #79689

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
79689 2018-10-15T12:20:48 Z Vardanyan Cities (BOI16_cities) C++14
0 / 100
501 ms 99024 KB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
const long long INF = 1000000000000005;
const int N = 1000 * 100 + 7;
map<int,long long> dist[N];
long long dp[33][N];
bool colour[33][N];
int A[N];
vector<pair<int, long long> > g[N];
int n, k, m;
void diji(int v){
	/*if (v == 3){
		cout << 0 << endl;
	}*/
	for (int i = 1; i <= n; i++) dist[v][i] = INF;
	dist[v][v] = 0;
	priority_queue<pair<long long, int> > pq;
	pq.push({ 0, v });
	while (!pq.empty()){
		pair<long long, int> gag = pq.top();
		pq.pop();
		if (colour[v][gag.second]) continue;
		colour[v][gag.second] = 1;
		for (int i = 0; i < g[gag.second].size(); i++){
			int to = g[gag.second][i].first;
			long long d = g[gag.second][i].second;
			if (d + (-gag.first) < dist[v][to]){
				dist[v][to] = d + (-gag.first);
				pq.push({ -dist[v][to], to });
			}
		}
	}
	//for (int i = 1; i <= n; i++) cout << dist[v][i] << " ";
	//cout << endl;
}
bool check(int a, int b){
	for (int i = 0; i < k; i++){
		if ((b >> i) & 1 && !((a >> i) & 1)) return false;
	}
	return true;
}
int rev(int a, int b){
	int ans = 0;
	for (int i = 0; i < k; i++){
		if (!((b >> i) & 1) && ((a >> i) & 1)) ans += (1 << i);
	}
	return ans;
}
int main(){
//	int c = 1 & 1;
//	cout << c << endl;
	cin >> n >> k >> m;
	for (int i = 1; i <= k; i++) cin >> A[i];
	while (m--){
		int x, y;
		long long z;
		cin >> x >> y >> z;
		g[x].push_back({ y, z });
		g[y].push_back({ x, z });
	}
	for (int i = 0; i <= (1 << k); i++)
		for (int j = 0; j <= n; j++) dp[i][j] = INF;
	for (int i = 1; i <= k; i++){
		diji(A[i]);
		for (int j = 1; j <= n; j++){
		//	cout << dist[A[i]][j] << " ";
			dp[(1 << (i-1))][j] = dist[A[i]][j];
		}
	//	cout << endl;
	}
	long long ans = INF;
	for (int i = 0; i < (1 << k); i++){
		for (int j = 0; j < (1 << k); j++){
			if (!check(i, j)) continue;
			//int jj = rev(i,j);
			int jj = i - j;
			for (int q = 1; q <= n; q++){
				if (dp[j][q]+dp[jj][q]<dp[i][q])
				dp[i][q] = dp[j][q] + dp[jj][q];
		//		cout << dp[i][q] << endl;
				if (i == ((1 << k) - 1)) ans = min(ans, dp[i][q]);
			}
		}
	}
	cout << ans << endl;
	return 0;
}

Compilation message

cities.cpp: In function 'void diji(int)':
cities.cpp:29:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < g[gag.second].size(); i++){
                   ~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 7 ms 7420 KB Output is correct
3 Correct 8 ms 7480 KB Output is correct
4 Incorrect 8 ms 7656 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 405 ms 61464 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 28 ms 61464 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 440 ms 74044 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 501 ms 99024 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -