이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
using i64 = long long;
const i64 oo = 1e18;
#define eb emplace_back
using pii = pair<int, int>;
using pli = pair<i64, int>;
int n, m;
vector<pii> adj[N];
vector<i64> dist[4];
vector<i64> dp[2];
priority_queue<pli, vector<pli>, greater<pli>> Q;
int a[4];
i64 ans = oo;
#define S 0
#define T 1
#define U 2
#define V 3
void dijkstra(int u, vector<i64> &dist) {
dist.assign(n + 1, oo);
Q.emplace(0, u);
dist[u] = 0;
while(!Q.empty()) {
pli top = Q.top();
Q.pop();
int u = top.second;
if(dist[u] != top.first)
continue;
for(auto [v, w] : adj[u]) {
if(dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
Q.emplace(dist[v], v);
}
}
}
}
void solve(int type) {
vector<pli> res;
for(int i = 1; i <= n; i++) {
if(dist[S][a[T]] == dist[S][i] + dist[T][i])
res.eb(dist[type][i], i);
}
sort(res.begin(), res.end());
for(pli x : res) {
int u = x.second;
dp[0][u] = dist[U][u];
dp[1][u] = dist[V][u];
for(auto [v, w] : adj[u]) {
if(dist[type][v] + w == dist[type][u]) {
dp[0][u] = min(dp[0][u], dp[0][v]);
dp[1][u] = min(dp[1][u], dp[1][v]);
}
}
ans = min(ans, min(dp[0][u] + dist[V][u], dp[1][u] + dist[U][u]));
}
}
int main() {
if(fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 0; i < 4; i++)
cin >> a[i];
for(int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].eb(v, w);
adj[v].eb(u, w);
}
for(int i = 0; i < 4; i++)
dijkstra(a[i], dist[i]);
// for(int j = 0; j < 4; j++) {
// cout << j << " : ";
//
// for(int i = 1; i <= n; i++)
// cout << dist[j][i] << " ";
//
// cout << "\n";
// }
dp[0].assign(n + 1, oo);
dp[1].assign(n + 1, oo);
solve(0);
solve(1);
cout << ans;
}
컴파일 시 표준 에러 (stderr) 메시지
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |