답안 #1007722

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1007722 2024-06-25T11:34:25 Z NintsiChkhaidze Cities (BOI16_cities) C++17
74 / 100
6000 ms 87720 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define s second
#define f first
#define pii pair <int,int>
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
#define int ll
using namespace std;
 
const int N = 2e5 + 5,inf = 1e18;
int arr[N],dist[8][N],par[N],n,cur;
bool fix[N];
int dd[N];
int DD[105][N];
vector <pii> v[N];
 
signed main (){
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
 
    int k,m;
    cin>>n>>k>>m;
 
    for (int i = 1; i <= k; i++)
        cin >> arr[i];
 
    for (int i =1; i <= m; i++){
        int a,b,c;
        cin>>a>>b>>c;
 
        v[a].pb({b,c});
        v[b].pb({a,c});
    }
 
	for (int i = 1; i <= k; i++){
		for (int j = 1; j <= n; j++)
			dist[i][j] = inf;
 
		priority_queue<pii> pq;
		dist[i][arr[i]] = 0;
		pq.push({0,arr[i]});
		while (pq.size()){
			int D = -pq.top().f,x = pq.top().s;
			pq.pop();
			if (dist[i][x] != D) continue;
 
			for (auto [to,w]: v[x]){
				if (dist[i][to] > D + w){
					dist[i][to] = D + w;
					pq.push({-dist[i][to],to});
				}
			}
		}
	}
    
	int ans=inf;
	for (int i = 1; i <= n; i++){
		int cur=0;
		for (int j = 1; j <= k; j++)
			cur += dist[j][i];
		ans=min(ans,cur);
	}
 
	if (k < 4){
		cout<<ans<<endl;
		exit(0);
	}
	
	for (int mask1=0;mask1<(1<<k); mask1++)
		for(int i=1;i<=n;i++)
			DD[mask1][i] = inf;

	for (int mask1 = 0; mask1 < (1<<k); mask1++){
		for (int mask2 = 0; mask2 < (1<<k); mask2++){
			int y = (mask1 & mask2);
			if (y > 0) continue;
			
			for (int i = 1; i <= n; i++){
				dd[i] = 0;
				for (int j = 0; j < k; j++){
					if (((mask1>>j) & 1)) dd[i] += dist[j + 1][i];
				}
			}
		
			priority_queue <pii> pq;
			for (int i = 1; i <= n; i++)
				pq.push({-dd[i],i});
	
			while (pq.size()){
				int D = -pq.top().f,x = pq.top().s;
				pq.pop();
				if (dd[x] != D) continue;
	
				for (auto [to,w]: v[x]){
					if (dd[to] > D + w){
						dd[to] = D + w;
						pq.push({-dd[to],to});
					}
				}
			}
			
			for (int i = 1; i <= n; i++){
				for (int j =0;j < k; j++){
					if (((mask2>>j) & 1)) dd[i] += (dist[j + 1][i]);
				}
				DD[(mask1 | mask2)][i] = min(DD[(mask1 | mask2)][i],dd[i]);
			}
		}
		
	}

	for (int mask1=0;mask1<(1<<k); mask1++){
		for (int i =1 ; i <= n; i++){
			dd[i] = DD[mask1][i];
		}

		priority_queue <pii> pq;
		for (int i = 1; i <= n; i++)
			pq.push({-dd[i],i});

		while (pq.size()){
			int D = -pq.top().f,x = pq.top().s;
			pq.pop();
			if (dd[x] != D) continue;

			for (auto [to,w]: v[x]){
				if (dd[to] > D + w){
					dd[to] = D + w;
					pq.push({-dd[to],to});
				}
			}
		}

		for (int i = 1; i <= n; i++){
			for (int j =0;j < k; j++){
				if (((mask1>>j) & 1)) continue;
				dd[i] += (dist[j + 1][i]);
			}
			ans=min(ans,dd[i]);
		}
	}

	cout<<ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 12892 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 4 ms 39516 KB Output is correct
5 Correct 6 ms 66140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 27020 KB Output is correct
2 Correct 121 ms 27648 KB Output is correct
3 Correct 42 ms 20048 KB Output is correct
4 Correct 48 ms 23916 KB Output is correct
5 Correct 110 ms 24936 KB Output is correct
6 Correct 37 ms 21768 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 39716 KB Output is correct
2 Correct 16 ms 39784 KB Output is correct
3 Correct 12 ms 39516 KB Output is correct
4 Correct 10 ms 39772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2256 ms 62340 KB Output is correct
2 Correct 2148 ms 61004 KB Output is correct
3 Correct 1663 ms 52600 KB Output is correct
4 Correct 1207 ms 55756 KB Output is correct
5 Correct 278 ms 51328 KB Output is correct
6 Correct 75 ms 50756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6070 ms 87720 KB Time limit exceeded
2 Halted 0 ms 0 KB -