제출 #335024

#제출 시각아이디문제언어결과실행 시간메모리
335024limabeansCities (BOI16_cities)C++17
0 / 100
508 ms31356 KiB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;

const ll inf = 1e18;
const int maxn = 1e6 + 5;

void spa(vector<vector<pair<ll,int>>> g, vector<ll>& dist, int src) {
    int n = g.size();
    dist.resize(n);
    for (int i=0; i<n; i++) {
	dist[i] = inf;
    }
    dist[src] = 0;
    priority_queue<pair<ll,int>> pq;
    pq.emplace(0,src);
    while (!pq.empty()) {
	auto cur = pq.top();
	pq.pop();
	ll wei = -cur.first;
	int at = cur.second;
	if (wei > dist[at]) continue;
	for (auto ed: g[at]) {
	    ll d = ed.first;
	    int to = ed.second;
	    if (wei+d < dist[to]) {
		dist[to] = wei+d;
		pq.emplace(-dist[to], to);
	    }
	}
    }
}

int n,m,k;
vector<vector<pair<ll,int>>> g;




bool imp[maxn];
vector<pair<ll,pair<int,int>>> edges;
vector<int> a;
vector<vector<ll>> dists;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    cin>>n>>k>>m;
    g.resize(n);
    for (int i=0; i<k; i++) {
	int x;
	cin>>x; --x;
	imp[x]=true;
	a.push_back(x);
    }
    dists.resize(k);
    
    for (int i=0; i<m; i++) {
	int u,v,w; cin>>u>>v>>w;
	--u; --v;
	edges.push_back({w,{u,v}});
	g[u].push_back({w,v});
	g[v].push_back({w,u});
    }

    for (int i=0; i<k; i++) {
	spa(g,dists[i],a[i]);
    }

    ll res = inf;

    int ptr=0;
    for (int c1: a) {
	vector<ll> dist(k, inf);
	for (int c2=c1; c2<n; c2++) {
	    for (int i=0; i<k; i++) {
		dist[i] = min(dists[i][c1], dists[i][c2]);
	    }
	    ll cur = accumulate(dist.begin(),dist.end(),0ll) + dists[ptr][c2];
	    res = min(res, cur);
	}
	++ptr;
    }


    cout<<res<<endl;  
    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...