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>
#ifndef dbg
#define dbg(...)
#endif
#define all(x) begin(x), end(x)
#define rsz(...) resize(__VA_ARGS__)
#define psh(...) push_back(__VA_ARGS__)
#define emp(...) emplace_back(__VA_ARGS__)
#define prt(...) print(cout, __VA_ARGS__)
#define dmp(...) print(cerr, #__VA_ARGS__, '=', __VA_ARGS__)
#define dprt(...) dbg(print(cerr,__VA_ARGS__))
#define ddmp(...) dbg(dmp(__VA_ARGS__))
using namespace std;using ll=long long;
template<typename t>void print(ostream& os, const t& a){os<<a<<'\n';}
template<typename t, typename... A>void print
(ostream& os, const t& a, A&&... b){os<<a<<' ';print(os, b...);}
template<typename t>using V=vector<t>;
constexpr int oo = 1e9 + 1;
struct Edge
{
int v, w;
Edge(int v, int w):
v(v),w(w){}
};
struct Node : V< Edge >
{
int val = oo;
};
struct Query
{
int v, id;
Query(int v, int id):
v(v), id(id){}
};
struct Union
{
V< Query > qs;
int lid, siz = 1;
};
struct Event
{
int v, val;
Event(int v, int val):
v(v), val(val){}
};
inline bool operator> (const Event& wc, const Event& mn)
{
return wc.val > mn.val;
}
int curr = oo;
int n, m, k, q;
V< Node > gr;
V< int > odp;
V< bool > vis;
V< Union > tb;
V< Event > evs;
priority_queue< Event, V< Event >, greater< Event > > Q;
inline void dijkstra()
{
while (not Q.empty())
{
int v = Q.top().v;
int val = Q.top().val;
Q.pop();
if (val > gr[v].val)
continue;
for (auto& ed : gr[v])
{
int s = ed.v;
int dl = ed.w;
if (gr[s].val > gr[v].val + dl)
{
gr[s].val = gr[v].val + dl;
Q.emplace(s, gr[s].val);
}
}
}
}
inline int find(int v)
{
return tb[v].lid == tb[tb[v].lid].lid?
tb[v].lid : tb[v].lid = find(tb[v].lid);
}
inline void join(int p, int q)
{
p = find(p);
q = find(q);
if (p == q)
return;
if (tb[p].siz < tb[q].siz)
swap(p, q);
if (tb[p].qs.size() < tb[q].qs.size())
swap(tb[p].qs, tb[q].qs);
tb[p].siz += tb[q].siz;
tb[q].lid = p;
for (auto& qu : tb[q].qs)
{
if (odp[qu.id] == oo and find(qu.v) != p)
tb[p].qs.psh(qu);
odp[qu.id] = min(odp[qu.id], curr);
}
tb[q].qs.clear();
tb[q].qs.shrink_to_fit();
}
inline void add(int v)
{
vis[v] = true;
for (auto& ed : gr[v])
if (vis[ed.v])
join(ed.v, v);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
gr.rsz(n + 1);
tb.rsz(n + 1);
vis.rsz(n + 1);
for (int i = 1; i <= n; ++i)
tb[i].lid = i;
for (int i = 0; i < m; ++i)
{
int p, q, w;
cin >> p >> q >> w;
gr[p].emp(q, w);
gr[q].emp(p, w);
}
cin >> k;
for (int i = 0; i < k; ++i)
{
int v; cin >> v;
gr[v].val = 0;
Q.emplace(v, 0);
}
cin >> q;
odp.rsz(q, oo);
for (int i = 0; i < q; ++i)
{
int p, q; cin >> p >> q;
tb[p].qs.emp(q, i);
tb[q].qs.emp(p, i);
}
dijkstra();
for (int i = 1; i <= n; ++i)
evs.emp(i, gr[i].val);
sort(all(evs), greater< Event >());
for (auto& ev : evs)
curr = ev.val, add(ev.v);
for (int i : odp)
prt(i);
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |