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 fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e10
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
int n,m,k;
vii ke[maxn];
vi special;
int d[maxn];
int sources[maxn];
pii dist[maxn];
int mindist;
pii node;
void dijkstra(vi inp)
{
mindist = inf;
fo(i,1,n) d[i] = inf;
priority_queue<pii,vii,greater<pii>> q;
for(int it : inp)
{
d[it] = 0;
sources[it] = it;
q.push({0,it});
}
while(ss(q))
{
auto [du,u] = q.top();
q.pop();
if(du != d[u]) continue;
for(auto [v,w] : ke[u])
{
if(minimize(d[v],du + w))
{
q.push({d[v],v});
sources[v] = sources[u];
}
}
}
fo(u,1,n) for(auto [v,w] : ke[u]) if(sources[u] != sources[v])
{
if(minimize(mindist,d[u] + w + d[v])) node = {sources[u],sources[v]};
}
}
bool ok[maxn];
pii dij(int inp)
{
pii minn;
minn.fi = inf;
fo(i,1,n) d[i] = inf;
d[inp] = 0;
priority_queue<pii,vii,greater<pii>> q;
q.push({0,inp});
while(q.size())
{
auto [du,u] = q.top();
q.pop();
if(d[u] != du) continue;
for(auto [v,w] : ke[u])
{
if(d[v] > d[u] + w)
{
d[v] = d[u] + w;
q.push({d[v],v});
}
}
}
for(int it : special) if(ok[it])
{
if(minimize(minn.fi,d[it])) minn.se = it;
}
return minn;
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
special.resize(k);
fo(i,1,m)
{
int u,v,w;
cin >> u >> v >> w;
ke[u].pb(v,w);
ke[v].pb(u,w);
}
int res = inf;
for(int &it : special) cin >> it,ok[it] = 1;
dijkstra(special);
int dd = mindist;
vi cc;
pii xx = node;
for(int it : special) if(it != node.fi and it != node.se) cc.pb(it);
dijkstra(cc);
minimize(res,mindist + dd);
node = xx;
ok[node.fi] = ok[node.se] = 0;
pii d1 = dij(node.fi);
ok[d1.se] = 0;
minimize(res,dij(node.se).fi + d1.fi);
ok[d1.se] = 1;
swap(node.fi,node.se);
d1 = dij(node.fi);
ok[d1.se] = 0;
minimize(res,dij(node.se).fi + d1.fi);
cout << res;
}
Compilation message (stderr)
RelayMarathon.cpp:125:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
125 | main()
| ^~~~
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |