# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1098163 | vjudge1 | Evacuation plan (IZhO18_plan) | Cpython 3 | 0 ms | 0 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>
#define INF 1e18
#define fi first
#define se second
#define FOR(i, k, n) for(ll i = k; i <= n; i++)
#define FOR1(i, k, n) for(ll i = k; i >= n; i--)
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define ll long long
#define vll vector<ll>
#define pll pair<ll, ll>
#define re return 0
#define mii map<int, int>
#define input "BAI1.inp"
#define output "BAI1.out"
#define rf freopen(input, "r", stdin); freopen(output, "w", stdout)
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
bool vis[maxn];
ll dp[maxn];
vii adj[maxn], vv;
pii b[maxn];
int kq[maxn], par[maxn], sz[maxn];
int n, m;
void make_set()
{
FOR(i, 1, n)
{
sz[i] = 1;
par[i] = i;
}
}
int find(int u)
{
if(u == par[u])
return u;
return par[u] = find(par[u]);
}
void Union(int u, int v)
{
u = find(u);
v = find(v);
if(sz[u] > sz[v])
swap(u, v);
par[u] = v;
sz[v] += sz[u];
}
int main()
{
cin >> n >> m;
FOR(i, 1, m)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
FOR(i, 1, n)
dp[i] = INF;
int k;
cin >> k;
priority_queue<pll, vector<pll>, greater<pll>> Q;
FOR(i, 1, k)
{
int x;
cin >> x;
dp[x] = 0;
Q.push({0, x});
}
while(!Q.empty())
{
int u = Q.top().se;
Q.pop();
if(vis[u])
continue;
vis[u] = 1;
for(auto x : adj[u])
{
int v = x.fi;
int w = x.se;
if(dp[v] > dp[u] + w)
{
dp[v] = dp[u] + w;
Q.push({dp[v], v});
}
}
}
int q;
cin >> q;
if(q == 1)
{
make_set();
FOR(i, 1, n)
vv.pb({dp[i], i});
sort(vv.begin(), vv.end());
FOR(i, 1, q)
{
int u, v;
cin >> u >> v;
b[i] = {u, v};
}
FOR1(i, vv.size() - 1, 0)
{
for(auto x : adj[vv[i].se])
{
if(dp[x.fi] < dp[vv[i].se])
continue;
int v = x.fi;
int u = vv[i].se;
if(find(v) != find(u))
Union(u, v);
}
FOR(j, 1, q)
{
if(find(b[j].fi) == find(b[j].se))
{
kq[j] = max(kq[j], vv[i].fi);
}
}
}
FOR(i, 1, q)
cout << kq[i] << "\n";
re;
}
if(n <= 1000 && m <= 1000 && q <= 1000)
{
make_set();
FOR(i, 1, n)
vv.pb({dp[i], i});
sort(vv.begin(), vv.end());
FOR(i, 1, q)
{
int u, v;
cin >> u >> v;
b[i] = {u, v};
}
FOR1(i, vv.size() - 1, 0)
{
for(auto x : adj[vv[i].se])
{
if(dp[x.fi] < dp[vv[i].se])
continue;
int v = x.fi;
int u = vv[i].se;
if(find(v) != find(u))
Union(u, v);
}
FOR(j, 1, q)
{
if(find(b[j].fi) == find(b[j].se))
{
kq[j] = max(kq[j], vv[i].fi);
}
}
}
FOR(i, 1, q)
cout << kq[i] << "\n";
}
re;
}