백준 12930번, 두 가중치 풀이
2020. 9. 27. 20:20ㆍProblem Solving/백준
문제
풀이
조건이 복잡한 다익스트라입니다.
priority queue에 cost, 현재 정점, 가중치 1의 합, 가중치 2의 합을 저장해두고 매번 갱신해주면 됩니다.
소스코드
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<pair<ll, ll>, pair<ll, ll>> ppll;
ll dist[21], graph[21][21][2];
vector<ll> _node[21];
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
for (int t = 0; t <= 1; t++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
char c;
cin >> c;
if (c != '.') {
graph[i][j][t] = c - '0';
if (t) continue;
_node[i].push_back(j);
_node[j].push_back(i);
}
}
for (int i = 0; i < n; i++) dist[i] = 1e9;
priority_queue<ppll> pq;
pq.push({{0, 0},
{0, 0}});
dist[0] = 0;
while (!pq.empty()) {
ppll top = pq.top();
pq.pop();
ll cost = -top.first.first;
ll curr = top.first.second;
ll a = top.second.first, b = top.second.second;
if (cost > dist[curr]) continue;
for (const auto &i : _node[curr]) {
ll na = graph[i][curr][0], nb = graph[i][curr][1];
ll t = (a + na) * (b + nb);
if (t < dist[i]) {
dist[i] = t;
pq.push({{-t, i},
{a + na, b + nb}});
}
}
}
cout << (dist[1] == 1e9 ? -1 : dist[1]);
}
'Problem Solving > 백준' 카테고리의 다른 글
백준 14462, 소가 길을 건너간 이유 8 (0) | 2020.09.29 |
---|---|
백준 1806번, 부분합 풀이 (0) | 2020.09.28 |
백준 1197번, 최소 스패닝 트리(MST)를 구하는 크루스칼 알고리즘 (0) | 2020.09.27 |
백준 8983번, 한국정보올림피아드(KOI) 2013 고등부 1번, 사냥꾼 풀이 (0) | 2020.09.27 |
백준 5550번 (JOI 2011년 2번), 헌책방 풀이 (0) | 2020.09.27 |