이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define F first
#define S second
const int N = 1e5+5;
const int inf = 1e16;
vector<pair<int, int>> adj[N];
int dist[N][4];
bool mark[N];
int dp[N][2];
int sp;
int ans;
void dijkstra(int s, int t){
for(int i=0; i< N; i++){
mark[i]=0;
dist[i][t]=inf;
}
dist[s][t]=0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while(pq.size()){
auto p = pq.top();
pq.pop();
if(mark[p.S] || dist[p.S][t]<p.F)continue;
mark[p.S]=1;
for(auto to : adj[p.S]){
if(!mark[to.F] && to.S+dist[p.S][t]<dist[to.F][t]){
dist[to.F][t]=to.S+dist[p.S][t];
pq.push({dist[to.F][t], to.F});
}
}
}
}
void dfs(int v, int p, int t){
dp[v][0]=min(dp[p][0], dist[v][2]);
dp[v][1]=min(dp[p][1], dist[v][3]);
ans=min(ans, dp[v][0]+dp[v][1]);
for(auto to : adj[v]){
if(to.F!=p){
if(dist[v][t]+dist[to.F][t^1]+to.S==sp){
dfs(to.F, v, t);
}
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, s, t, u, v;
cin >> n >> m >> s >> t >> u >> v;
for(int i=0; i< m; i++){
int a, b, c;
cin >> a >> b >> c;
adj[a].pb({b, c});
adj[b].pb({a, c});
}
dijkstra(s, 0);
dijkstra(t, 1);
dijkstra(u, 2);
dijkstra(v, 3);
sp=dist[s][1];
ans=dist[v][2];
dp[0][0]=inf;
dp[0][1]=inf;
dfs(s, 0, 0);
dfs(t, 0, 1);
cout << ans << '\n';
}
# | 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... |