Submission #59947

#TimeUsernameProblemLanguageResultExecution timeMemory
59947IvanCCities (BOI16_cities)C++17
100 / 100
3284 ms83672 KiB
#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 (stderr)

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);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...