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 100010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#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]};
}
}
pii min1,min2;
void dij(int inp)
{
min1.fi = inf;
min2.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(it != inf)
{
if(min1.fi > d[it])
{
min2 = min1;
min1 = {d[it],it};
}
}
}
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;
dijkstra(special);
int d1 = mindist;
vi cc;
for(int it : special) if(it != node.fi and it != node.se) cc.pb(it);
dijkstra(cc);
minimize(res,mindist + d1);
dij(node.fi);
pii x = min1;
pii y = min2;
dij(node.se);
if(x.se == node.se)
{
int cc = inf;
for(int it : special) if(it != node.fi and it != node.se and it != y.se) minimize(cc,d[it]);
minimize(res,y.fi + cc);
}
else
{
int cc = inf;
for(int it : special) if(it != node.fi and it != node.se and it != x.se) minimize(cc,d[it]);
minimize(res,x.fi + cc);
}
dij(node.se);
x = min1;
y = min2;
dij(node.fi);
if(x.se == node.fi)
{
int cc = inf;
for(int it : special) if(it != node.fi and it != node.se and it != y.se) minimize(cc,d[it]);
minimize(res,y.fi + cc);
}
else
{
int cc = inf;
for(int it : special) if(it != node.fi and it != node.se and it != x.se) minimize(cc,d[it]);
minimize(res,x.fi + cc);
}
cout << res;
}
Compilation message (stderr)
RelayMarathon.cpp:128:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
128 | main()
| ^~~~
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | 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... |