QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#420063#8673. 最短路径cmk6660 5368ms386812kbC++239.6kb2024-05-24 14:26:102024-05-24 14:26:10

Judging History

你现在查看的是最新测评结果

  • [2024-05-24 14:26:10]
  • 评测
  • 测评结果:0
  • 用时:5368ms
  • 内存:386812kb
  • [2024-05-24 14:26:10]
  • 提交

answer

/*  _              _     _                                             _       __      __      __   
   / \     _   _  | |_  | |__     ___    _ __   _    ___   _ __ ___   | | __  / /_    / /_    / /_  
  / _ \   | | | | | __| | '_ \   / _ \  | '__| (_)  / __| | '_ ` _ \  | |/ / | '_ \  | '_ \  | '_ \ 
 / ___ \  | |_| | | |_  | | | | | (_) | | |     _  | (__  | | | | | | |   <  | (_) | | (_) | | (_) |
/_/   \_\  \__,_|  \__| |_| |_|  \___/  |_|    (_)  \___| |_| |_| |_| |_|\_\  \___/   \___/   \___/ 
[Created Time:       2024-05-24 13:58:24]
[Last Modified Time: 2024-05-24 14:25:57] */
#pragma GCC optimize("Ofast", "unroll-loops")
#include<bits/stdc++.h>
#ifdef LOCAL
#include"debug.h"
#else
#define D(...) ((void)0)
#endif
using namespace std; using ll = long long;
#define For(i, j, k) for ( int i = (j) ; i <= (k) ; i++ )
#define Fol(i, j, k) for ( int i = (j) ; i >= (k) ; i-- )
namespace FastIO
{
// ------------------------------
// #define DISABLE_MMAP
// ------------------------------
#if ( defined(LOCAL) || defined(_WIN32) ) && !defined(DISABLE_MMAP)
#define DISABLE_MMAP
#endif
#ifdef LOCAL
	inline char gc() { return getchar(); }
	inline void pc(char c) { putchar(c); }
#else
#ifdef DISABLE_MMAP
	inline constexpr int _READ_SIZE = 1 << 18;
	inline static char _read_buffer[_READ_SIZE], *_read_ptr = nullptr, *_read_ptr_end = nullptr;
	inline char gc()
	{
		if ( __builtin_expect(_read_ptr == _read_ptr_end, false) )
		{
			_read_ptr = _read_buffer, _read_ptr_end = _read_buffer + fread(_read_buffer, 1, _READ_SIZE, stdin);
			if ( __builtin_expect(_read_ptr == _read_ptr_end, false) ) return EOF;
		}
		return *_read_ptr++;
	}
#else
#include<sys/mman.h>
	inline static const char *_read_ptr = (const char *)mmap(nullptr, 0x7fffffff, 1, 2, 0, 0);
	inline char gc() { return *_read_ptr++; }
#endif
	inline constexpr int _WRITE_SIZE = 1 << 18;
	inline static char _write_buffer[_WRITE_SIZE], *_write_ptr = _write_buffer;
	inline void pc(char c)
	{
		*_write_ptr++ = c;
		if ( __builtin_expect(_write_buffer + _WRITE_SIZE == _write_ptr, false) )
			fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout), _write_ptr = _write_buffer;
	}
	inline struct _auto_flush
	{
		inline ~_auto_flush() { fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout); }
	}	_auto_flush;
#endif
	template < class T > inline constexpr bool _is_signed = numeric_limits < T >::is_signed;
	template < class T > inline constexpr bool _is_unsigned = numeric_limits < T >::is_integer && !_is_signed < T >;
#if __SIZEOF_LONG__ == 64
	template <> inline constexpr bool _is_signed < __int128 > = true;
	template <> inline constexpr bool _is_unsigned < __uint128_t > = true;
#endif
	inline void read(char &c) { do c = gc(); while ( !isgraph(c) ); }
	inline void read_cstr(char *s)
	{
		char c = gc(); while ( !isgraph(c) ) c = gc();
		while ( isgraph(c) ) *s++ = c, c = gc(); *s = 0;
	}
	inline void read(string &s)
	{
		char c = gc(); s.clear(); while ( !isgraph(c) ) c = gc();
		while ( isgraph(c) ) s.push_back(c), c = gc();
	}
	template < class T, enable_if_t < _is_signed < T >, int > = 0 >
	inline void read(T &x)
	{
		char c = gc(); bool f = true; x = 0;
		while ( !isdigit(c) ) { if ( c == 45 ) f = false; c = gc(); }
		if ( f ) while ( isdigit(c) ) x = x * 10 + ( c & 15 ), c = gc();
		else     while ( isdigit(c) ) x = x * 10 - ( c & 15 ), c = gc();
	}
	template < class T, enable_if_t < _is_unsigned < T >, int > = 0 >
	inline void read(T &x)
	{
		char c = gc(); while ( !isdigit(c) ) c = gc();
		x = 0; while ( isdigit(c) ) x = x * 10 + ( c & 15 ), c = gc();
	}
	inline void write(char c) { pc(c); }
	inline void write_cstr(const char *s) { while ( *s ) pc(*s++); }
	inline void write(const string &s) { for ( char c : s ) pc(c); }
	template < class T, enable_if_t < _is_signed < T >, int > = 0 >
	inline void write(T x)
	{
		char buffer[numeric_limits < T >::digits10 + 1]; int digits = 0;
		if ( x >= 0 )  do buffer[digits++] =  ( x % 10 ) | 48, x /= 10; while ( x );
		else { pc(45); do buffer[digits++] = -( x % 10 ) | 48, x /= 10; while ( x ); }
		while ( digits ) pc(buffer[--digits]);
	}
	template < class T, enable_if_t < _is_unsigned < T >, int > = 0 >
	inline void write(T x)
	{
		char buffer[numeric_limits < T >::digits10]; int digits = 0;
		do buffer[digits++] = ( x % 10 ) | 48, x /= 10; while ( x );
		while ( digits ) pc(buffer[--digits]);
	}
	template < int N > struct _tuple_io_helper
	{
		template < class ...T > static inline void _read(tuple < T... > &x) { _tuple_io_helper < N - 1 >::_read(x), read(get<N - 1>(x)); }
		template < class ...T > static inline void _write(const tuple < T... > &x) { _tuple_io_helper < N - 1 >::_write(x), pc(32), write(get<N - 1>(x)); }
	};
	template <> struct _tuple_io_helper < 1 >
	{
		template < class ...T > static inline void _read(tuple < T... > &x) { read(get<0>(x)); }
		template < class ...T > static inline void _write(const tuple < T... > &x) { write(get<0>(x)); }
	};
	template < class ...T > inline void read(tuple < T... > &x) { _tuple_io_helper < sizeof...(T) >::_read(x); }
	template < class ...T > inline void write(const tuple < T... > &x) { _tuple_io_helper < sizeof...(T) >::_write(x); }
	template < class T1, class T2 > inline void read(pair < T1, T2 > &x) { read(x.first), read(x.second); }
	template < class T1, class T2 > inline void write(const pair < T1, T2 > &x) { write(x.first), pc(32), write(x.second); }
	template < class T1, class ...T2 > inline void read(T1 &x, T2 &...y) { read(x), read(y...); }
	template < class ...T > inline void read_cstr(char *x, T *...y) { read_cstr(x), read_cstr(y...); }
	template < class T1, class ...T2 > inline void write(const T1 &x, const T2 &...y) { write(x), write(y...); }
	template < class ...T > inline void write_cstr(const char *x, const T *...y) { write_cstr(x), write_cstr(y...); }
	template < class T > inline void print(const T &x) { write(x); }
	inline void print_cstr(const char *x) { write_cstr(x); }
	template < class T1, class ...T2 > inline void print(const T1 &x, const T2 &...y) { write(x), pc(32), print(y...); }
	template < class ...T > inline void print_cstr(const char *x, const T *...y) { write_cstr(x), pc(32), print_cstr(y...); }
	inline void println() { pc(10); } inline void println_cstr() { pc(10); }
	template < class ...T > inline void println(const T &...x) { print(x...), pc(10); }
	template < class ...T > inline void println_cstr(const T *...x) { print_cstr(x...), pc(10); }
}	using FastIO::read, FastIO::read_cstr, FastIO::write, FastIO::write_cstr, FastIO::println, FastIO::println_cstr;
template <class T, int N, int M> struct darry_with_radix_sort {
	struct E {int idx; T val;} e[M], f[M];
	T a[M];
	int b[N], c[2 << (std::__lg(N) / 2)], n, m, o;
	struct V {
		T *b, *e;
		V(T *b_, T *e_) : b(b_), e(e_) {}
		T* begin() const { return b; }
		T* end() const { return e; }
		T operator[](std::size_t i) const { return *(b + i); }
		std::size_t size() const { return e - b; }
	};
	void init(int n_) { n = n_, m = 0, o = std::__lg(n) / 2 + 1, std::fill(b + 1, b + n + 1, 0); }
	template <class ...U>
	void emplace(int idx, U&&... val) {
		b[idx]++, e[++m].idx = idx, std::construct_at(&e[m].val, val...);
	}
	void build() {
		std::fill(c, c + (1 << o), 0);
		for (int i = 1; i <= m; i++) c[e[i].idx & ((1 << o) - 1)]++;
		for (int i = 1; i < (1 << o); i++) c[i] += c[i - 1];
		for (int i = 1; i <= m; i++) f[c[e[i].idx & ((1 << o) - 1)]--] = std::move(e[i]);
		std::fill(c, c + (1 << o), 0);
		for (int i = 1; i <= m; i++) c[f[i].idx >> o]++;
		for (int i = 1; i < (1 << o); i++) c[i] += c[i - 1];
		for (int i = m; i; i--) a[c[f[i].idx >> o]--] = std::move(f[i].val);
		b[0] = 1;
		for (int i = 1; i <= n; i++) b[i] += b[i - 1];
	}
	V operator[](int idx) { return V(a + b[idx - 1], a + b[idx]); }
};
constexpr ll inf = numeric_limits < ll >::max() >> 1;
int n, m, q, o, u, v, s[2], id[2][200009]; ll w, d[2][200009], lim, ans; bool used[2][200009];
darry_with_radix_sort < pair < int, ll >, 200009, 3000009 > g[2]; vector < int > nd[2];
#include<ext/pb_ds/priority_queue.hpp>
__gnu_pbds::priority_queue < pair < ll, int >, greater < pair < ll, int > >, __gnu_pbds::pairing_heap_tag > pq[2];
inline void work()
{
	For(o, 0, 1)
	{
		for ( int i : nd[o] ) used[o][i] = false, d[o][i] = inf, id[o][i] = 0;
		read(u), nd[o].clear(), pq[o].clear(), pq[o].push(pair(d[o][u] = 0, u));
	}
	for ( int o = 0 ; ; o = !o )
	{
		if ( pq[o].empty() ) { println(-1); return; }
		u = pq[o].top().second, pq[o].pop();
		if ( used[o][u] ) continue; nd[o].push_back(u);
		if ( used[!o][u] ) break; used[o][u] = true;
		while ( id[o][u] < (int)g[o][u].size() )
		{
			auto [v, w] = g[o][u][id[o][u]++];
			if ( d[o][v] > d[o][u] + w ) pq[o].push(pair(d[o][v] = d[o][u] + w, v));
		}
	}
	ans = d[0][u] + d[1][u];
	For(o, 0, 1) for ( int i : nd[o] )
	{
		lim = ( d[o][u] - d[o][i] ) << 1;
		for ( auto [v, w] : g[o][i] ) 
		{
			if ( w > lim ) break;
			if ( used[!o][v] ) ans = min(ans, d[o][i] + d[!o][v] + w);
		}
	}
	println(ans);
}
int main()
{
	read(n, m, q);
	For(o, 0, 1) g[o].init(n);
	{
		unsigned mx = -1u / n * n, s; read(s); mt19937 rnd(s);
		const auto gen = [&] { unsigned x; do x = rnd(); while ( x >= mx ); return x % n + 1; };
		For(i, 1, m) u = gen(), v = gen(), w = rnd(), g[0].emplace(u, v, w), g[1].emplace(v, u, w);
	}
	For(o, 0, 1)
	{
		g[o].build(), nd[o].reserve(n), fill(d[o] + 1, d[o] + n + 1, inf);
		For(i, 1, n) sort(g[o][i].begin(), g[o][i].end(), [&](auto x, auto y) { return x.second < y.second; });
	}
	while ( q-- ) work();
	return 0;
}
// 想上GM捏 想上GM捏 想上GM捏 想上GM捏 想上GM捏
// 伊娜可爱捏 伊娜贴贴捏

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 5
Accepted
time: 57ms
memory: 379516kb

input:

4 8 5 1112792816
2 3
4 3
4 3
3 2
1 4

output:

3419197189
1798364963
1798364963
3986398077
2337967903

result:

ok 5 lines

Test #2:

score: -5
Wrong Answer
time: 35ms
memory: 379596kb

input:

2000 2000 2000 3336994405
659 1650
1678 341
818 235
1380 1865
1927 1366
1233 1673
267 1698
775 1022
1255 1110
1533 1928
1854 169
1579 729
449 1335
943 583
360 50
795 926
1584 911
1924 604
280 309
1429 420
1107 1858
1466 76
265 1109
1077 622
245 1941
957 1434
1560 1128
122 51
229 925
826 1006
851 323...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

wrong answer 409th lines differ - expected: '43257212723', found: '-1'

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 2633ms
memory: 380672kb

input:

3000 3000000 10000 37461678
2873 1368
1757 2000
1262 1822
2484 1778
2055 2096
2545 366
2923 2028
1469 1874
691 631
1173 2967
894 2020
1207 881
373 236
1913 1923
1351 16
1066 2032
471 1561
1047 2043
457 145
2728 1752
2521 1199
1568 904
2515 543
1472 2161
748 2744
748 1908
912 172
2340 2494
977 267
10...

output:

36084543
49860181
45803385
27805775
41392651
43506617
39517515
39687913
37675345
23367579
37276839
32364058
50703016
26615083
25983590
69912018
42921191
31222991
39092809
25257238
36267824
77866403
34199475
45804995
36929459
34257048
43789135
55135658
31005342
41408425
35033769
37667712
42873640
378...

result:

wrong answer 16th lines differ - expected: '51209814', found: '69912018'

Subtask #3:

score: 0
Wrong Answer

Test #13:

score: 0
Wrong Answer
time: 60ms
memory: 385448kb

input:

200000 200000 10000 1824322211
104482 112162
130667 13436
36792 142259
51832 97549
15358 180076
128251 92635
45296 195115
62109 38014
22014 86754
79735 103777
94797 96086
196760 5955
45622 59618
12995 62585
55686 156402
23085 68138
170749 148553
97603 160274
112975 22651
116322 190720
84774 57075
23...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

wrong answer 1040th lines differ - expected: '212192011649', found: '-1'

Subtask #4:

score: 0
Wrong Answer

Test #17:

score: 0
Wrong Answer
time: 2039ms
memory: 385612kb

input:

200000 500000 10000 3113327438
68816 31422
174349 125983
18111 188786
84806 87249
142007 180723
95611 116398
104758 196349
77547 89859
120350 77199
110906 10209
177461 194861
115505 105566
27493 166237
15676 158290
86204 116010
159979 125659
132461 61989
194289 157721
18830 82910
166696 98162
125208...

output:

21671419385
-1
31996366393
19295613250
-1
25762674206
-1
-1
30938504016
19365143518
-1
-1
33507263304
23396138679
19478702596
-1
-1
-1
20149023019
23727970709
24229890807
28875639856
-1
22877254445
25605430611
27724721382
-1
26550979061
25161604327
-1
22676819628
19348763468
-1
24220635647
161988758...

result:

wrong answer 9th lines differ - expected: '30333017011', found: '30938504016'

Subtask #5:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 1982ms
memory: 385624kb

input:

200000 500000 10000 1843053063
3409 108359
168924 184622
13119 119837
109492 38050
97152 51201
49047 12472
183998 191613
193074 177289
194248 104409
15509 88499
61967 143398
4532 56790
196650 158711
63655 70744
140178 107299
63530 87330
127334 159237
7134 184418
125289 28604
176966 179527
181695 128...

output:

18098332289
22666064981
23549058925
26339412859
-1
23116762056
22209493371
21117534178
22029252897
33952599088
17793204212
13278636159
25843769632
18134229421
29623865096
23847021502
20878297870
-1
-1
21042457357
23208160613
19615484227
26566774108
15726744387
23457868594
23352911380
16578768343
242...

result:

wrong answer 32nd lines differ - expected: '25595931739', found: '25653379485'

Subtask #6:

score: 0
Wrong Answer

Test #24:

score: 0
Wrong Answer
time: 4228ms
memory: 385828kb

input:

100000 3000000 10000 3892765041
14843 34156
43390 49542
38564 95501
26194 87126
18638 53346
69414 47011
95472 58303
44370 77172
75652 90555
94386 31888
47911 9905
70599 97061
52764 24896
31445 15589
82314 43852
97155 93412
11834 45082
75614 42459
67802 32024
82389 4968
32860 62514
97630 28012
14839 ...

output:

1547972368
1533240012
1192488694
1802115335
1491444021
1888896300
1720188008
1762089620
1815841406
1831208977
1250925907
1756812381
2027344758
1385409721
2044038817
1877583272
1632784703
2090242303
1694524102
1818975564
1429598050
1599437722
2286394605
1416358110
1929044811
2022891575
1487757623
156...

result:

wrong answer 15th lines differ - expected: '1937527554', found: '2044038817'

Subtask #7:

score: 0
Wrong Answer

Test #33:

score: 0
Wrong Answer
time: 5368ms
memory: 386812kb

input:

200000 3000000 10000 3910662331
161257 40967
50546 86049
199665 199302
177403 36274
158790 143151
193304 78511
28032 149723
96394 37099
2157 76024
195400 34830
41933 147591
191613 96468
194837 67293
57992 63117
24749 6694
117818 87323
46130 53470
174812 24950
149173 124886
119910 54123
2297 124533
5...

output:

3371897180
3059012504
3899803743
4918664465
3834117056
3101758258
4211432244
3700678845
3320322266
4020388668
3314213999
4156507838
3045843635
3379778417
3201003504
4511026914
4847106102
3502897631
3579081638
3470448652
4322207527
2160161436
4372954162
3841655899
3367608876
3864513044
4225021719
377...

result:

wrong answer 34th lines differ - expected: '4605416839', found: '4680435838'