이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pl;
typedef tuple<int,int,int> tt;
#define all(v) v.begin(), v.end()
const int mn = 1e5 + 10;
ll dist[4][mn], dp[mn];
vector<pl> adj[mn], subg[mn];
bool vis[mn];
int n, node;
void dijkstra (int save, int source) {
for (int i = 1; i <= n; i++) vis[i] = 0, dist[save][i] = LLONG_MAX;
dist[save][source] = 0;
priority_queue<pl> pq;
pq.push({0, source});
while (pq.size()) {
int a = pq.top().second; pq.pop();
if (vis[a]) continue;
vis[a] = 1;
for (pl it : adj[a]) {
ll w; int b; tie(w, b) = it;
if (dist[save][a] + w < dist[save][b]) {
dist[save][b] = dist[save][a] + w;
pq.push({-dist[save][b], b});
}
}
}
return;
}
void addEdge (int a, int b, ll c, bool way = 1) {
subg[a].push_back({c, b});
if (way) subg[b].push_back({c, a});
}
ll finalD() {
for (int i = 0; i <= node; i++) vis[i] = 0, dp[i] = LLONG_MAX;
dp[0] = 0;
priority_queue<pl> pq;
pq.push({0, 0});
while (pq.size()) {
int a = pq.top().second; pq.pop();
if (vis[a]) continue;
vis[a] = 1;
for (pl it : subg[a]) {
ll w; int b; tie(w, b) = it;
if (dp[a] + w < dp[b]) {
dp[b] = dp[a] + w;
pq.push({-dp[b], b});
}
}
}
return dp[node];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int m; cin >> n >> m;
int s, t, u, v; cin >> s >> t >> u >> v;
node = n + 1;
vector<tt> edge(m);
for (int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c;
adj[a].push_back({c, b});
adj[b].push_back({c, a});
edge[i] = make_tuple(a, b, c);
}
dijkstra(0, s);
dijkstra(1, t);
dijkstra(2, u);
dijkstra(3, v);
for (int i = 1; i <= n; i++) {
if (dist[0][i] + dist[1][i] > dist[0][t]) continue;
addEdge(0, i, dist[2][i]);
addEdge(i, node, dist[3][i]);
}
for (int i = 0; i < m; i++) {
int a, b; ll c; tie(a, b, c) = edge[i];
if (dist[0][a] + c + dist[1][b] == dist[0][t] && dist[0][a] + c == dist[0][b]) addEdge(a, b, 0, 0);
if (dist[0][b] + c + dist[1][a] == dist[0][t] && dist[0][b] + c == dist[0][a]) addEdge(b, a, 0, 0);
}
cout << finalD();
return 0;
}
# | 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... |