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],avail[maxN],vit;
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 (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;
q.push({-di[v],v});
}
}
}
}
void calc(lli u)
{
avail[u] = vit;
dp[u] = min(dp[u],dc[u]);
res = min(dp[u] + dd[u],res);
for (lli i : adj1[u])
{
lli v = e[i].x + e[i].y - u;
if (avail[v] == vit) continue;
dp[v] = min(dp[v],dp[u]);
res = min(dp[v] + dd[v],res);
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);
if (da[e[i].y] + db[e[i].x] + e[i].w == da[b]) adj1[e[i].y].push_back(i);
}
res = dc[d];vit = 1;
fill(dp + 1,dp + n + 1,1e18);calc(a);swap(dd,dc);swap(d,c);++vit;
fill(dp + 1,dp + n + 1,1e18);calc(a);
cout << res;
}
//a = 1, x = 3, y = 2, b = 4
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... |