#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 |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
476 KB |
Output is correct |
3 |
Correct |
3 ms |
508 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Incorrect |
3 ms |
512 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
476 KB |
Output is correct |
3 |
Correct |
3 ms |
508 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Incorrect |
3 ms |
512 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
15216 KB |
Output is correct |
2 |
Correct |
445 ms |
23772 KB |
Output is correct |
3 |
Correct |
546 ms |
24316 KB |
Output is correct |
4 |
Correct |
93 ms |
11120 KB |
Output is correct |
5 |
Correct |
459 ms |
23892 KB |
Output is correct |
6 |
Correct |
466 ms |
24224 KB |
Output is correct |
7 |
Correct |
440 ms |
24276 KB |
Output is correct |
8 |
Correct |
474 ms |
24320 KB |
Output is correct |
9 |
Correct |
469 ms |
23792 KB |
Output is correct |
10 |
Correct |
454 ms |
23428 KB |
Output is correct |
11 |
Correct |
462 ms |
23128 KB |
Output is correct |
12 |
Correct |
458 ms |
23196 KB |
Output is correct |
13 |
Correct |
467 ms |
23188 KB |
Output is correct |
14 |
Correct |
429 ms |
23232 KB |
Output is correct |
15 |
Correct |
461 ms |
23624 KB |
Output is correct |
16 |
Correct |
442 ms |
23288 KB |
Output is correct |
17 |
Correct |
454 ms |
23296 KB |
Output is correct |
18 |
Correct |
444 ms |
23196 KB |
Output is correct |
19 |
Correct |
87 ms |
10992 KB |
Output is correct |
20 |
Correct |
439 ms |
23100 KB |
Output is correct |
21 |
Correct |
436 ms |
22980 KB |
Output is correct |
22 |
Correct |
102 ms |
12904 KB |
Output is correct |
23 |
Correct |
122 ms |
11348 KB |
Output is correct |
24 |
Correct |
131 ms |
12912 KB |
Output is correct |
25 |
Correct |
106 ms |
12912 KB |
Output is correct |
26 |
Correct |
104 ms |
11524 KB |
Output is correct |
27 |
Correct |
94 ms |
10876 KB |
Output is correct |
28 |
Correct |
100 ms |
12896 KB |
Output is correct |
29 |
Correct |
91 ms |
10992 KB |
Output is correct |
30 |
Correct |
104 ms |
12880 KB |
Output is correct |
31 |
Correct |
100 ms |
10952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
476 KB |
Output is correct |
3 |
Correct |
3 ms |
508 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Incorrect |
3 ms |
512 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |