답안 #59947

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
59947 2018-07-23T11:09:58 Z IvanC Cities (BOI16_cities) C++17
100 / 100
3284 ms 83672 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int,int> leve;
typedef pair<ll,int> ii;
typedef tuple<ll,int,int> trinca;
 
const int MAXN = 1e5 + 10;
const int MAXK = 7;
const int MAXL = 35;
const ll INF = 1e18;
 
vector<leve> grafo[MAXN]; 
ll matriz[MAXN][MAXK],menor_distancia[MAXN][MAXL];
int N,K,M,qual[MAXK],processado[MAXN];
int jafoi[MAXN][MAXL];
 
void Dijkstra(int idx){
	memset(processado,0,sizeof(processado));
	priority_queue<ii, vector<ii> , greater<ii> > pq;
	pq.push(ii(0,qual[idx]));
	while(!pq.empty()){
		ii davez = pq.top();
		pq.pop();
		int v = davez.second;
		ll path = davez.first;
		if(processado[v]) continue;
		processado[v] = 1;
		matriz[v][idx] = path;
		for(ii aresta : grafo[v]){
			int u = aresta.first;
			ll w = aresta.second;
			if(!processado[u]) pq.push(ii(path + w,u));
		}
	}
}
 
int main(){
	scanf("%d %d %d",&N,&K,&M);
	for(int i = 0;i<K;i++){
		scanf("%d",&qual[i]);
	}
	for(int i = 1;i<=M;i++){
		int u,v,w;
		scanf("%d %d %d",&u,&v,&w);
		grafo[u].push_back(leve(v,w));
		grafo[v].push_back(leve(u,w));
	}
	for(int i = 0;i<K;i++) Dijkstra(i);
	ll resposta = INF;
	int alvo = (1 << K) - 1;
	for(int i = 1;i<=N;i++){
		for(int bitmask = 0;bitmask <= alvo;bitmask++){
			menor_distancia[i][bitmask] = INF;
		}
	}
	for(int i = 1;i<=N;i++){
		for(int j = 0;j<K;j++){
			menor_distancia[i][(1 << j)] = matriz[i][j];
		}
	}
	for(int bitmask = 1;bitmask < alvo;bitmask++){
		if(!(bitmask & (1 << 0))) continue;
		priority_queue<ii,vector<ii>,greater<ii>> pq;
		for(int i = 1;i<=N;i++){
			if(menor_distancia[i][bitmask] != INF) pq.push(ii(menor_distancia[i][bitmask],i)); 
		}
		while(!pq.empty()){
			ii davez = pq.top();
			pq.pop();
			ll path = davez.first;
			int v = davez.second;
			if(jafoi[v][bitmask]) continue;
			jafoi[v][bitmask] = 1;
			for(leve aresta : grafo[v]){
				int u = aresta.first, w = aresta.second;
				if(path + w < menor_distancia[u][bitmask]){
					menor_distancia[u][bitmask] = path + w;
					pq.push(ii(path + w,u)); 
				}
			}
		}
		for(int j = 0;j<K;j++){
			if(bitmask & (1 << j)) continue;
			int nbitmask = (bitmask) | (1 << j); 
			for(int i = 1;i<=N;i++){
				menor_distancia[i][nbitmask] = min(menor_distancia[i][nbitmask], menor_distancia[i][bitmask] + matriz[i][j] );
			}
		}
	}
	for(int i = 1;i<=N;i++) resposta = min(resposta,menor_distancia[i][alvo]);
	printf("%lld\n",resposta);
	
	return 0;
	
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d",&N,&K,&M);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
cities.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&qual[i]);
   ~~~~~^~~~~~~~~~~~~~~
cities.cpp:46:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d",&u,&v,&w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3064 KB Output is correct
2 Correct 7 ms 3176 KB Output is correct
3 Correct 6 ms 3384 KB Output is correct
4 Correct 7 ms 3384 KB Output is correct
5 Correct 7 ms 3384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1150 ms 66788 KB Output is correct
2 Correct 986 ms 70928 KB Output is correct
3 Correct 539 ms 70928 KB Output is correct
4 Correct 329 ms 70928 KB Output is correct
5 Correct 583 ms 76468 KB Output is correct
6 Correct 232 ms 76468 KB Output is correct
7 Correct 11 ms 76468 KB Output is correct
8 Correct 8 ms 76468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 76468 KB Output is correct
2 Correct 14 ms 76468 KB Output is correct
3 Correct 11 ms 76468 KB Output is correct
4 Correct 10 ms 76468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1866 ms 83416 KB Output is correct
2 Correct 1762 ms 83416 KB Output is correct
3 Correct 1179 ms 83416 KB Output is correct
4 Correct 1113 ms 83416 KB Output is correct
5 Correct 517 ms 83416 KB Output is correct
6 Correct 343 ms 83416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3113 ms 83416 KB Output is correct
2 Correct 3284 ms 83672 KB Output is correct
3 Correct 3278 ms 83672 KB Output is correct
4 Correct 2335 ms 83672 KB Output is correct
5 Correct 1924 ms 83672 KB Output is correct
6 Correct 706 ms 83672 KB Output is correct
7 Correct 495 ms 83672 KB Output is correct