페이지

5551번: 쇼핑몰

https://www.acmicpc.net/problem/5551


#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX_N = 3e3;
int n, m, k, dis[MAX_N + 1], r;
vector<pair<intint> > adj[MAX_N + 1];
int main() {
    scanf("%d %d %d", &n, &m, &k);
    for (int i = 0, x, y, z; i<m; i++) {
        scanf("%d %d %d", &x, &y, &z);
        adj[x].push_back({ y,z });
        adj[y].push_back({ x,z });
    }
    priority_queue<pair<intint> > pq;
    fill(dis + 1, dis + 1 + n, 1e9);
    for (int i = 0, x; i<k; i++) {
        scanf("%d", &x);
        pq.push({ 0,x });
        dis[x] = 0;
    }
    while (!pq.empty()) {
        int h = pq.top().second, tdis = -pq.top().first;
        pq.pop();
        if (tdis != dis[h]) continue;
        for (auto it : adj[h]) {
            if (tdis + it.second<dis[it.first]) {
                dis[it.first] = tdis + it.second;
                pq.push({ -dis[it.first],it.first });
            }
        }
    }
    for (int i = 1; i <= n; i++)
        for (auto it : adj[i]) r = max(r, (dis[i] + dis[it.first] + it.second + 1) / 2);
    printf("%d", r);
    return 0;
}

댓글 없음 :

댓글 쓰기