-
algospot - firetrucksAlgorithm/알고스팟 2020. 1. 20. 05:581234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677//algospot - firetrucks#include <iostream>#include <vector>#include <queue>using namespace std;#define INF 2000000000int V, E, N, M;vector<vector<pair<int, int>>> adj;vector<int> firePlc;vector<int> mini;void init(){cin >> V >> E >> N >> M;adj = vector<vector<pair<int, int>>>(V + 1);firePlc = vector<int>(N);mini = vector<int>(V+1, INF);int x, y, r;for (int i = 0; i < E; i++){cin >> x >> y >> r;adj[x].push_back(make_pair(y, r));adj[y].push_back(make_pair(x, r));}for (int i = 0; i < N; i++)cin >> firePlc[i];for (int i = 0; i < M; i++){cin >> x;adj[0].push_back(make_pair(x, 0));}}void dijkstra(){priority_queue<pair<int, int>> pq;pq.push(make_pair(0,0));mini[0] = 0;int here, cost;while(!pq.empty()){here = pq.top().second;cost = -pq.top().first;pq.pop();if(mini[here] >= cost)for(int i = 0; i<adj[here].size(); ++i){int there = adj[here][i].first;int c = adj[here][i].second;if(c+cost < mini[there]){mini[there] = c+cost;pq.push(make_pair(-(c+cost), there));}}}int ans = 0;for(int i = 0; i<N; i++)ans += mini[firePlc[i]];cout << ans << "\n";}int main(){int C;cin >> C;for (int t_num = 0; t_num < C; ++t_num){init();dijkstra();}}
cs https://algospot.com/judge/problem/read/FIRETRUCKS
algospot.com :: FIRETRUCKS
소방차 문제 정보 문제 한겨울 날씨가 추워지면 각종 난방 기구 때문에 화재가 발생하기 쉽습니다. 어느 추운 겨울날 서울 시내 n개의 지역에 동시에 화재가 발생했습니다. 피해를 최소화하기 위해 가능한 한 빠르게 소방차를 파견해야 합니다. 서울 시내에는 m개의 소방서가 있습니다. 화재 장소에서 가장 가까운 소방서에서 소방차를 보낸다고 할 때, 각 화재 장소에 소방차가 도달하기까지 걸리는 시간의 합을 계산하는 프로그램을 작성하세요. 각 소방서에는 소방차가 충분
algospot.com
firetrucks
불난곳에선 어떤 소방서에서 오는지 상관없다는 점을 이용
가상의 정점을 하나 만들고, 가상의 정점과 소방서들을 가중치 0인 간선으로 모두 연결
가상의 정점을 시작으로 dijkstra algorithm을 한번만 사용하면 정답을 구할수 있음
'Algorithm > 알고스팟' 카테고리의 다른 글
algospot - drunken (0) 2020.01.29 algospot - nthlon (0) 2020.01.20 algospot - clocksync (0) 2020.01.20 algospot - gallery (0) 2020.01.20 algospot - hanoi4 (0) 2020.01.17