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<iostream>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
using namespace std;
using lli = long long;
const lli maxN = 2e5 + 5;
struct Tedge
{
lli x,y,w;
}
e[maxN];
struct Tadj
{
lli y,w;
};
lli n,m,a,b,c,d;
vector<lli> adj[maxN],adj1[maxN],adj2[maxN];
vector<Tadj> adj3[maxN];
priority_queue<pair<lli,lli>> q;
lli da[maxN],db[maxN],dc[maxN],dd[maxN],di[maxN],res;
lli dp[maxN];
void input()
{
res = 1e18;
cin >> n >> m >> a >> b >> c >> d;
for (lli i = 1;i <= m;++i)
{
cin >> e[i].x >> e[i].y >> e[i].w;
adj[e[i].x].push_back(i);
adj[e[i].y].push_back(i);
}
}
void dijkstra(lli a)
{
fill(di + 1,di + n + 1,1e18);
di[a] = 0;
while (!q.empty()) q.pop();
q.push({-di[a],a});
while (!q.empty())
{
lli u = q.top().second,p = q.top().first;
q.pop();
//if (a == 1) cout << u << " " << p << "\n";
if (p != -di[u]) continue;
for (lli i : adj[u])
{
lli v = e[i].x ^ e[i].y ^ u;
if (di[v] > di[u] + e[i].w)
{
di[v] = di[u] + e[i].w;
//if (a == 1) cout << v << " " << -di[v] << "\n";
q.push({-di[v],v});
//if (a == 1) cout << q.top().first << " " << q.top().second << "\n";
}
}
//if (a == 1) cout << "\n";
}
}
void calc(lli u)
{
//cout << dp[u] << "\n";
dp[u] = min(dp[u],dc[u]);
//cout << dp[u] << "\n";
res = min(dp[u] + dd[u],res);
//cout << u << "\n";
for (lli i : adj1[u])
{
lli v = e[i].x ^ e[i].y ^ u;
dp[v] = min(dp[v],dp[u]);
calc(v);
}
}
void solve()
{
dijkstra(a);swap(di,da);
dijkstra(b);swap(di,db);
dijkstra(c);swap(di,dc);
dijkstra(d);swap(di,dd);
for (lli i = 1;i <= m;++i)
if (da[e[i].x] + db[e[i].y] + e[i].w == da[b]) adj1[e[i].x].push_back(i);
res = dc[d];
fill(dp + 1,dp + n + 1,1e18);calc(a);swap(dd,dc);swap(d,c);
fill(dp + 1,dp + n + 1,1e18);calc(a);swap(dd,dc);swap(d,c);
cout << res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("C.inp","r",stdin);
// freopen("C.out","w",stdout);
input();
solve();
}
# | 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... |