Submission #410716

#TimeUsernameProblemLanguageResultExecution timeMemory
410716antontsiorvasCities (BOI16_cities)C++14
100 / 100
2655 ms42224 KiB
#include <cstdio>
#include <vector>
#include <utility>
#include <queue>
#include <algorithm>
#define lli long long int
using namespace std;

int N, K, M, import[7], from, to;
lli weight, dp[33][100005];
vector< pair<int, lli> > data[100005];
priority_queue< pair<lli, int> > pq; 
lli inf = 9000000000000000000;

int main(){
	scanf("%d%d%d",&N,&K,&M);
	for(int i=1; i<=K; i++) scanf("%d",&import[i]);
	for(int i=1; i<=M; i++){
		scanf("%d%d%lld",&from,&to,&weight);
		data[from].push_back(make_pair(to,weight));
		data[to].push_back(make_pair(from,weight));
	}
	
	int to = (1 << K);
	for(int i=0; i<to; i++){
		for(int j=1; j<=N; j++) dp[i][j] = inf;
	}
	for(int i=0; i<K; i++) dp[1 << i][import[i+1]] = 0;
	
	for(int i=1; i<to; i++){
		for(int prev=0; prev<i; prev++){
			int b_or = (prev | i), b_xor = (prev ^ i);
			if(b_or != i || prev < b_xor) continue;
			for(int j=1; j<=N; j++){
				dp[i][j] = min(dp[i][j],dp[prev][j]+dp[b_xor][j]);
			}
		}
		
		for(int j=1; j<=N; j++){
			if(dp[i][j] != inf) pq.push(make_pair(-dp[i][j],j));
		}
		
		while(!pq.empty()){
			int city = pq.top().second;
			lli dpnum = -pq.top().first;
			pq.pop();
			int len = data[city].size();
			for(int j=0; j<len; j++){
				int next_city = data[city][j].first;
				lli cost = data[city][j].second;
				if(dpnum + cost < dp[i][next_city]){
					dp[i][next_city] = dpnum + cost;
					pq.push(make_pair(-dp[i][next_city],next_city));
				}
			}
		}
	}
	
	lli ans = inf;
	for(int i=1; i<=N; i++) ans = min(ans, dp[to-1][i]);
	printf("%lld",ans);
}

Compilation message (stderr)

cities.cpp: In function 'int main()':
cities.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |  scanf("%d%d%d",&N,&K,&M);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~
cities.cpp:17:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |  for(int i=1; i<=K; i++) scanf("%d",&import[i]);
      |                          ~~~~~^~~~~~~~~~~~~~~~~
cities.cpp:19:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   scanf("%d%d%lld",&from,&to,&weight);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...