이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
int dist[4][100001];
bool cmp(int a, int b) {
return dist[0][a] < dist[0][b];
}
void solve() {
int N, M; cin >> N >> M;
int S[4]; cin >> S[0] >> S[1] >> S[2] >> S[3];
vector<pair<int, int> > adj[N+1];
for(int i=0; i<M; i++) {
int A, B, C; cin >> A >> B >> C;
adj[A].push_back({B, C});
adj[B].push_back({A, C});
}
int vis[4][N+1];
for(int i=1; i<=N; i++) {
dist[0][i] = dist[1][i] = dist[2][i] = dist[3][i] = 1e18;
vis[0][i] = vis[1][i] = vis[2][i] = vis[3][i] = 0;
}
for(int i=0; i<2; i++) {
dist[i][S[i]] = 0;
priority_queue<pair<int, int>, vector<pair<int,int> >, greater<pair<int, int> > > pq;
for(int j=1; j<=N; j++) {
pq.push({dist[i][j], j});
}
while(pq.size()) {
pair<int, int> t= pq.top(); pq.pop();
if(!vis[i][t.second]) {
vis[i][t.second]=1;
for(auto x : adj[t.second]) {
if(!vis[i][x.first] && dist[i][x.first] > dist[i][t.second] + x.second) {
dist[i][x.first] = dist[i][t.second] + x.second;
pq.push({dist[i][x.first], x.first});
}
}
}
}
}
int ans = 1e18;
int allowed[N+1];
for(int i=1; i<=N; i++) {
allowed[i] = (dist[0][i] + dist[1][i] == dist[0][S[1]] ? 1 : 0);
}
vector<int> dag1[N+1], dag2[N+1]; // small -> big, big -> small
for(int i=1; i<=N; i++) {
for(int j=0; j<adj[i].size(); j++) {
if(allowed[i] && allowed[adj[i][j].first] && dist[0][i] < dist[0][adj[i][j].first]) {
dag1[i].push_back(adj[i][j].first);
dag2[adj[i][j].first].push_back(i);
}
}
}
//Reset
for(int i=2; i<4; i++) {
dist[i][S[i]] = 0;
priority_queue<pair<int, int>, vector<pair<int,int> >, greater<pair<int, int> > > pq;
for(int j=1; j<=N; j++) {
pq.push({dist[i][j], j});
}
while(pq.size()) {
pair<int, int> t= pq.top(); pq.pop();
if(!vis[i][t.second]) {
vis[i][t.second]=1;
for(auto x : adj[t.second]) {
if(!vis[i][x.first] && dist[i][x.first] > dist[i][t.second] + x.second) {
dist[i][x.first] = dist[i][t.second] + x.second;
pq.push({dist[i][x.first], x.first});
}
}
}
}
}
vector<int> all;
all.push_back(0);
for(int i=1; i<=N; i++) {
if(allowed[i]) all.push_back(i);
}
sort(all.begin() + 1, all.end(), cmp);
int A = all.size() - 1;
int dp[A+1];
int pos[N+1];
for(int i=1; i<=A; i++) {
pos[all[i]] = i;
}
for(int i=1; i<=A; i++) {
dp[i] = dist[2][all[i]];
for(int x : dag2[all[i]]) {
dp[i] = min(dp[i], dp[pos[x]]);
}
ans = min(ans, dp[i] + dist[3][all[i]]);
}
reverse(all.begin() + 1, all.end());
for(int i=1; i<=A; i++) {
pos[all[i]] = i;
}
for(int i=1; i<=A; i++) {
dp[i] = dist[2][all[i]];
for(int x : dag1[all[i]]) {
dp[i] = min(dp[i], dp[pos[x]]);
}
ans = min(ans, dp[i] + dist[3][all[i]]);
}
ans = min(ans, dist[2][S[3]]);
cout << ans << '\n';
}
signed main() {
int t = 1;// cin >> t;
while(t--) solve();
}
//6 -> 3 -> 1 -> 8 -> 9
// 12 1 1 1
컴파일 시 표준 에러 (stderr) 메시지
commuter_pass.cpp: In function 'void solve()':
commuter_pass.cpp:48:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int j=0; j<adj[i].size(); j++) {
| ~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |