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 int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
//#define endl "\n"
#define pii pair<int,int>
using namespace std;
const int MAXN = 1e5+5;
const int mod7 = 1e9+7;
const long long inf = 1e18;
int n,m;
int a,b,c,d;
set<pii> pq;
vector<vector<int>> dist(4, vector<int>(MAXN, inf));
vector<vector<pii>> graf(MAXN);
vector<vector<pii>> graf2(MAXN);
int rez;
vector<int> distu(MAXN,inf);
vector<int> distv(MAXN,inf);
vector<bool> vis(MAXN, 0);
void diksta(int nod, int p)
{
dist[p][nod] = 0;
pq.insert({dist[p][nod], nod});
while(!pq.empty())
{
auto it = pq.begin();
int u = it->sc;
pq.erase(it);
for(auto x: graf[u])
{
if(dist[p][x.fi] > dist[p][u] + x.sc)
{
pq.erase({dist[p][x.fi], x.fi});
dist[p][x.fi] = dist[p][u] + x.sc;
pq.insert({dist[p][x.fi], x.fi});
}
}
}
}
void dfs(int nod, int p)
{
vis[nod] = 1;
for(auto x: graf2[nod])
{
if(!vis[x.fi])dfs(x.fi, nod);
distu[nod] = min(distu[nod], distu[x.fi]);
distv[nod] = min(distv[nod], distv[x.fi]);
}
rez = min(rez, distu[nod] + dist[3][nod]);
//cout << rez << endl;
rez = min(rez, distv[nod] + dist[2][nod]);
//cout << rez << endl;
distu[nod] = min(distu[nod], dist[2][nod]);
distv[nod] = min(distv[nod], dist[3][nod]);
}
struct grana{int a,b,c;};
signed main()
{
ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
int tt=1;
//cin >> tt;
while(tt--)
{
cin >> n >> m;
cin >> a >> b >> c >> d;
vector<grana> sve;
for(int i=0; i<m; i++)
{
int u,v,w;
cin >> u >> v >> w;
sve.pb({u,v,w});
graf[u].pb({v,w});
graf[v].pb({u,w});
}
diksta(a,0);
diksta(b,1);
diksta(c,2);
diksta(d,3);
rez = dist[2][d];
int distab = dist[0][b];
for(auto x: sve)
{
int u = x.a;
int v = x.b;
int w = x.c;
if(dist[0][u] + dist[1][u] == distab && dist[0][v] + dist[1][v] == distab)
{
if(dist[0][u] < dist[0][v] && dist[0][u] + w == dist[0][v])graf2[u].pb({v,0});
else if(dist[0][u] > dist[0][v] && dist[0][u] == w+ dist[0][v])graf2[v].pb({u,0});
}
}
dfs(a,a);
graf2 = vector<vector<pii>>(MAXN);
fill(all(distu), inf);
fill(all(distv), inf);
fill(all(vis), 0);
for(auto x: sve)
{
int u = x.a;
int v = x.b;
int w = x.c;
if(dist[0][u] + dist[1][u] == distab && dist[0][v] + dist[1][v] == distab)
{
if(dist[1][u] < dist[1][v] && dist[1][u] + w == dist[1][v])graf2[u].pb({v,0});
else if(dist[1][u] > dist[1][v] && dist[1][u] == w+ dist[1][v])graf2[v].pb({u,0});
}
}
dfs(b,b);
cout << rez << endl;
}
}
# | 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... |