fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 12285 (C++) Spaceship Defence (Large)
최초 업로드: 2025-03-10 19:59:00
최근 수정 시간: 2025-07-25 10:18:04
게시자: rlatjwls3333
카테고리: 백준
조회수: 33
# [Gold II] Spaceship Defence (Large) [문제링크](https://www.acmicpc.net/problem/12285) ## 문제 설명 <p>The enemy has invaded your spaceship, and only superior tactics will allow you to defend it! To travel around your spaceship, your soldiers will use two devices: <em>teleporters</em> and <em>turbolifts</em>.</p> <p>Teleporters allow your soldiers to move instantly between rooms. Every room contains a teleporter, and rooms are color-coded: if a soldier is in a room with some color, she can use the teleporter in that room to immediately move to any other room with the same color.</p> <p>Turbolifts allow your soldiers to move between rooms more slowly. A turbolift is like an elevator that moves in many directions. Each turbolift moves from one room to one other room, and it takes a certain amount of time to travel. Notes about turbolifts:</p> <ul> <li>Turbolifts are not two-way: if a turbolift moves soldiers from room a to room b, the same turbolift cannot move soldiers from room b to room a, although there might be another turbolift that does that.</li> <li>More than one soldier can use the same turbolift, and they do not interfere with each other in any way.</li> </ul> <p><span style="line-height:1.6em">You will be given the locations and destinations of several soldiers. For each soldier, output the minimum amount of time it could take that soldier to travel from his location to his destination.</span></p> ## 입력 <p>The first line of the input gives the number of test cases, <strong>T</strong>. <strong>T</strong> test cases follow.</p> <p>For every test case:</p> <p>The first line of every test case contains an integer <strong>N</strong>, which is the number of rooms in your spaceship. The rooms are numbered from 1 to <strong>N</strong>. The following <strong>N</strong> lines each contain a string telling the color of the rooms, from room 1 to room <strong>N</strong>. The strings only contain characters a-z (the lower-case English letters) and 0-9 (the number 0 to 9), and the length of each string will be less than or equal to 2.</p> <p>The next line in the test case is an integer <strong>M</strong>, which indicates the number of turbolifts in your spaceship. The following <strong>M</strong> lines each contain 3 space-separated integers <strong>a</strong><strong><sub>i</sub></strong>, <strong>b</strong><strong><sub>i</sub></strong>, <strong>t</strong><strong><sub>i</sub></strong>, telling us that there is a turbolift that can transport soldiers from room <strong>a</strong><strong><sub>i</sub></strong> to room <strong>b</strong><strong><sub>i</sub></strong> in <strong>t</strong><strong><sub>i</sub></strong> seconds.</p> <p>The next line in the test case contains an integer <strong>S</strong>, which is the number of soldiers at your command. The following <strong>S</strong> lines each contain two integers: the location and destination of one soldier, <strong>p</strong><strong><sub>j</sub></strong> and <strong>q</strong><strong><sub>j</sub></strong>.</p> <p>Limits</p> <ul> <li>1 ≤ <strong>S</strong> ≤ 100.</li> <li>1 ≤ <strong>a</strong><strong><sub>i</sub></strong><strong>, b</strong><strong><sub>i</sub></strong> ≤ <strong>N</strong>.</li> <li>0 ≤ <strong>t</strong><strong><sub>i</sub></strong> ≤ 1000.</li> <li>1 ≤ <strong>p</strong><strong><sub>j</sub></strong><strong>, q</strong><strong><sub>j</sub></strong> ≤ <strong>N</strong>.</li> <li><strong style="line-height:1.6em">T</strong><span style="line-height:1.6em"> = 1.</span></li> <li>1 ≤ <strong>N</strong> ≤ 80000.</li> <li>0 ≤ <strong>M</strong> ≤ 3000.</li> </ul> ## 출력 <p>For each test case, output one line containing only the string "Case #x:", where x is the number of the test case (starting from 1). On the next <strong>S</strong> lines, output a single integer: on line <strong>j</strong>, the smallest number of seconds it could take for a soldier to travel from <strong>p</strong><strong><sub>j</sub></strong> to <strong>q</strong><strong><sub>j</sub></strong>. If there is no path from <strong>p</strong><strong><sub>j</sub></strong> to <strong>q</strong><strong><sub>j</sub></strong>, the integer you output should be -1.</p> ## 풀이 #### 방의 수가 최대 80000까지이고, 최단거리를 찾아야하는 횟수가 최대 100번이니 다익스트라로 접근하였습니다. #### 같은 색의 방끼리는 이동하는데 0초가 걸리기에 같은 색의 방을 해시맵으로 구분해주었습니다. ``` c++ #include<bits/stdc++.h> using namespace std; int n; const int MAX = 80000; const int INF = 0x3f3f3f3f; int parent[MAX], visited[MAX]; vector<vector<pair<int, int>>> conn(MAX); struct soldier { int a, t; bool operator<(const soldier s) const { return t > s.t; } }; int dijkstra(int p, int q) { fill(visited, visited+n, INF); priority_queue<soldier> pq; pq.push({p, 0}); while(!pq.empty()) { soldier cur = pq.top(); pq.pop(); if(visited[cur.a]<=cur.t) continue; if(cur.a==q) return cur.t; visited[cur.a]=true; for(auto next:conn[cur.a]) { if(visited[next.first]>cur.t+next.second) { pq.push({next.first, cur.t+next.second}); } } } return -1; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; for(int tc=1;tc<=t;tc++) { cin >> n; // 초기화 for(int i=0;i<n;i++) { conn[i].clear(); parent[i]=i; } // 방 색칠 map<string, int> exist; for(int i=0;i<n;i++) { string s; cin >> s; if(exist[s]) { parent[i]=exist[s]; } else { exist[s]=i; } } // 방 색칠한대로 이동 수정 int m; cin >> m; while(m--) { int a, b, t; cin >> a >> b >> t; int pa = parent[a-1], pb = parent[b-1]; if(pa!=pb) { conn[pa].push_back({pb, t}); } } // 다익스트라 수행 cout << "Case #" << tc << ":\n"; int s; cin >> s; while(s--) { int p, q; cin >> p >> q; cout << dijkstra(parent[p-1], parent[q-1]) << '\n'; } } } ```