-
Notifications
You must be signed in to change notification settings - Fork 0
/
20128.cpp
76 lines (76 loc) · 2.44 KB
/
20128.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//AC
//BOJ 20218 Parity Constraint Shortest Path
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <limits.h>
using namespace std;
#define INF 9223372036854775807
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<pll, int> ppli;
priority_queue<ppli, vector<ppli>, greater<ppli>> pq;
vector<pll> v[101010];
ll odd[101010];
ll even[101010];
bool ck[101010];
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int N, M, x, y, c;
cin >> N >> M;
for(int i=0; i<M; i++) {
cin >> x >> y >> c;
v[x].push_back({y, c});
v[y].push_back({x, c});
}
for(int i=1; i<=N; i++) {
odd[i] = INF;
even[i] = INF;
}
even[1] = 0; // 1->1 짝수 최소는 무조건 0
pq.push({{odd[1], even[1]}, 1});
while(!pq.empty()) {
int xx = pq.top().second;
pq.pop();
if(!ck[xx]) {
//ck[xx] = true;
for(auto tmp : v[xx]) {
ll yy = tmp.first;
if(tmp.second % 2) {
int check = 0;
if(odd[yy] > even[xx] + tmp.second && even[xx] != INF) { // 짝수 + 홀수 = 홀수
odd[yy] = even[xx] + tmp.second;
check = 1;
}
if(even[yy] > odd[xx] + tmp.second && odd[xx] != INF) { // 홀수 + 홀수 = 짝수
even[yy] = odd[xx] + tmp.second;
check = 1;
}
if(check) pq.push({{odd[yy],even[yy]}, yy});
}
else {
int check = 0;
if(even[yy] > even[xx] + tmp.second && even[xx] != INF) { // 짝수 + 짝수 = 짝수
even[yy] = even[xx] + tmp.second;
check = 1;
}
if(odd[yy] > odd[xx] + tmp.second && odd[xx] != INF) { // 짝수 + 홀수 = 홀수
odd[yy] = odd[xx] + tmp.second;
check = 1;
}
if(check) pq.push({{odd[yy], even[yy]}, yy});
}
}
}
}
for(int i=1; i<=N; i++) {
if(odd[i] != INF) cout << odd[i] << " ";
else cout << -1 << " ";
if(even[i] != INF) cout << even[i] << " ";
else cout << -1 << " ";
cout << "\n";
}
return 0;
}