This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
ll dist[maxn][4], n, m, s, t, u, v;
ll dp[maxn][2];
bool visited[maxn];
vector<pair<int,int> > g[maxn];
/*
dist[i][0] - dist to i from u
dist[i][1] - dist to i from v
dist[i][2] - dist to i from s
dist[i][3] - dist to i from t
*/
void dijkstra(int start, int x) {
memset(visited,false,sizeof(visited));
for(int i=0;i<=n;i++) {
dist[i][x] = 10000000LL * INT_MAX;
}
dist[start][x] = 0LL;
//visited[start] = true;
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
pq.push(mp(0, start));
while(!pq.empty()) {
int curr = pq.top().second;
pq.pop();
if(visited[curr]) continue;
visited[curr] = true;
for(auto i:g[curr]) {
if(dist[curr][x] + i.second < dist[i.first][x] && !visited[i.first]) {
dist[i.first][x] = dist[curr][x] + i.second;
pq.push(mp(dist[i.first][x], i.first));
}
}
}
}
void get_dp(int start, int x) {
memset(visited,false,sizeof(visited));
for(int i=0;i<=n;i++) {
dp[i][x] = dist[i][1];
}
queue<int>q;
q.push(start);
while(!q.empty()) {
int curr = q.front();
q.pop();
for(auto i:g[curr]) {
if(x == 0) {
if(dist[curr][2] + i.second + dist[i.first][3] == dist[t][2]) {
dp[i.first][0] = min(dp[i.first][0], dp[curr][0]);
}
}
else {
if(dist[curr][3] + i.second + dist[i.first][2] == dist[s][3]) {
dp[i.first][1] = min(dp[i.first][1], dp[curr][1]);
}
}
if(!visited[i.first]) {
visited[i.first] = true;
q.push(i.first);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
cin>>s>>t;
cin>>u>>v;
int x, y, z;
for(int i=0;i<m;i++) {
cin>>x>>y>>z;
g[x].pb(mp(y,z));
g[y].pb(mp(x,z));
}
dijkstra(u, 0);
dijkstra(v, 1);
dijkstra(s, 2);
dijkstra(t, 3);
get_dp(s, 0);
get_dp(t, 1);
ll result = LLONG_MAX;
for(int i=1;i<=n;i++) {
result = min(result, dist[i][0] + min(dp[i][0], dp[i][1]));
}
cout<<result;
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... |