# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1013431 | daffuwu | Commuter Pass (JOI18_commuter_pass) | C++14 | 265 ms | 35600 KiB |
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>
using namespace std;
#define fr first
#define sc second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long n, m, dst[100069][4], src[4], a, b, c, ans, dp[100069][2];
vector<pair<long long, long long> > al[100069];
vector<long long> al1[100069], par[100069];
void dij(long long src, long long idx)
{
long long i;
priority_queue<pair<long long, long long> > pq;
for (i=1; i<=n; i++) dst[i][idx] = 1e15;
dst[src][idx] = 0;
pq.push({0, src});
for (; !pq.empty(); )
{
auto [w, u] = pq.top();
pq.pop();
w = -w;
if (dst[u][idx]<w) continue;
for (auto [v, w]:al[u])
{
if (dst[v][idx]>dst[u][idx]+w)
{
dst[v][idx] = dst[u][idx]+w;
par[v].clear();
pq.push({-dst[v][idx], v});
}
if (dst[v][idx] == dst[u][idx]+w) par[v].push_back(u);
}
}
if (idx == 0)
{
for (i=1; i<=n; i++)
{
for (auto u:par[i]) al1[u].push_back(i);
}
}
for (i=1; i<=n; i++) par[i].clear();
}
long long slv(long long u, long long idx)
{
if (dp[u][idx] != -1) return dp[u][idx];
if (u == src[1]) return dp[u][idx] = dst[u][idx+2];
dp[u][idx] = 1e15;
for (auto v:al1[u]) dp[u][idx] = min(dp[u][idx], slv(v, idx));
if (dp[u][idx] != 1e15) dp[u][idx] = min(dp[u][idx], dst[u][idx+2]);
return dp[u][idx];
}
int main()
{
long long i, j, ii;
scanf("%lld%lld%lld%lld%lld%lld", &n, &m, src, src+1, src+2, src+3);
for (i=1; i<=m; i++)
{
scanf("%lld%lld%lld", &a, &b, &c);
al[a].push_back({b, c});
al[b].push_back({a, c});
}
for (i=0; i<=3; i++) dij(src[i], i);
memset(dp, -1, sizeof(dp));
ans = dst[src[3]][2];
for (i=1; i<=n; i++)
{
for (j=0; j<=1; j++)
{
ans = min(ans, dst[i][j+2]+slv(i, 1-j));
}
}
printf("%lld\n", ans);
}
Compilation message (stderr)
# | 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... |