답안 #894668

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894668 2023-12-28T16:12:39 Z LCJLY Cities (BOI16_cities) C++14
100 / 100
1627 ms 51944 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
//typedef pair<pii,pii>pi2;
typedef array<pii,5>pi2;

void solve(){	
	int n,k,m;
	cin >> n >> k >> m;
	
	int city[k];
	for(int x=0;x<k;x++){
		cin >> city[x];
	}
	
	vector<pii>adj[n+5];
	int temp,temp2,temp3;
	
	for(int x=0;x<m;x++){
		cin >> temp >> temp2 >> temp3;
		adj[temp].push_back({temp2,temp3});
		adj[temp2].push_back({temp,temp3});
	}
	
	int dist[35][n+5];
	memset(dist,63,sizeof(dist));
	
	for(int x=0;x<k;x++){
		dist[1<<x][city[x]]=0;
	}
	
	for(int mask=1;mask<(1<<k);mask++){
		//find submask
		for(int y=1;y<mask;y++){
			if((y|mask)!=mask) continue;
			int hold=mask^y;
			for(int i=1;i<=n;i++){
				dist[mask][i]=min(dist[mask][i],dist[y][i]+dist[hold][i]);
			}
		}
		
		priority_queue<pii,vector<pii>,greater<pii>>pq;
		
		for(int y=1;y<=n;y++){
			if(dist[mask][y]<1e16){
				pq.push({dist[mask][y],y});
			}
		}
		
		while(!pq.empty()){
			pii cur=pq.top();
			pq.pop();
			int index=cur.second;
			int d=cur.first;
			if(dist[mask][index]!=d) continue;
			for(auto it:adj[index]){
				int next=it.first;
				int nd=d+it.second;
				if(nd>=dist[mask][next]) continue;
				dist[mask][next]=nd;
				pq.push({nd,next});
			}
		}
	}
	
	int best=LONG_LONG_MAX;
	
	for(int x=1;x<=n;x++){
		best=min(best,dist[(1<<k)-1][x]);
	}
	
	cout << best;
}

int32_t main(){										
	ios::sync_with_stdio(0);	
	cin.tie(0);
	//freopen("in.txt", "r", stdin);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}	
}



		


		
		
	
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 368 ms 51156 KB Output is correct
2 Correct 365 ms 51836 KB Output is correct
3 Correct 190 ms 41132 KB Output is correct
4 Correct 46 ms 13244 KB Output is correct
5 Correct 203 ms 49864 KB Output is correct
6 Correct 45 ms 13372 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 860 KB Output is correct
2 Correct 4 ms 856 KB Output is correct
3 Correct 3 ms 868 KB Output is correct
4 Correct 3 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 783 ms 51632 KB Output is correct
2 Correct 775 ms 50968 KB Output is correct
3 Correct 432 ms 41236 KB Output is correct
4 Correct 443 ms 33532 KB Output is correct
5 Correct 114 ms 17076 KB Output is correct
6 Correct 50 ms 15188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1591 ms 51944 KB Output is correct
2 Correct 1627 ms 51444 KB Output is correct
3 Correct 1461 ms 51656 KB Output is correct
4 Correct 987 ms 41316 KB Output is correct
5 Correct 802 ms 33408 KB Output is correct
6 Correct 183 ms 17168 KB Output is correct
7 Correct 78 ms 15520 KB Output is correct