제출 #1314131

#제출 시각아이디문제언어결과실행 시간메모리
1314131dsuyCities (BOI16_cities)C++20
14 / 100
184 ms20260 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,k,m;
const int maxn = 1e5+5;
const int maxk = 6;
const int INF = LLONG_MAX/4;
const int state = 1 << maxk;
vector<pair<int,int>> adj[maxn];
int important_city[maxk];
unordered_map<int,int> pos_city;
void inp(){
    cin >> n >> k >> m;
    for(int i = 1;i<=k;i++){
        cin >> important_city[i];
        pos_city[important_city[i]] = i;
    }
    for(int i = 1;i<=m;i++){
        int x,y,w; cin >> x >> y >> w;
        adj[x].push_back({y,w});
        adj[y].push_back({x,w});
    }
}
namespace codeAC{
    bool check(){
        return n <= maxn && m <= 2 * maxn && k <= 5;
    }

    int dist[maxn][maxk];
    void dijkstra(int start,int idx){
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
        pq.push({0,start});
        dist[start][idx] = 0;
        while(!pq.empty()){
            int w = pq.top().first;
            int u = pq.top().second;
            pq.pop();
            if(dist[u][idx] < w) continue;
            for(pair<int,int> it : adj[u]){
                int v = it.first;
                int nw = it.second;
                if(dist[v][idx] > dist[u][idx] + nw){
                    dist[v][idx] = dist[u][idx] + nw;
                    pq.push({dist[v][idx],v});
                }
            }
        }
    }
    void solve(void){
        for(int i = 1;i<=n;i++){
            for(int j = 1;j<=k;j++) dist[i][j] = INF;
        }
        for(int i = 1;i<=k;i++){
            dijkstra(important_city[i],i);
        }
        int best = INF;
        for(int i = 1;i<=n;i++){
            int cost = 0;
            for(int j = 1;j<=k;j++){
                cost += dist[i][j];
            }
            best = min(best,cost);
        }
        cout << best << '\n';
    }
};
/*
4 3 5
1 3 4
1 2 4
1 3 9
2 3 2
2 4 5
3 4 8

*/
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    inp();
    if(codeAC::check()) return codeAC::solve(),0;
    return 0;
}
#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...