QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#425482#8673. 最短路径zhoukangyang#10 3613ms336996kbC++144.4kb2024-05-30 11:39:432024-05-30 11:39:44

Judging History

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

  • [2024-05-30 11:39:44]
  • 评测
  • 测评结果:10
  • 用时:3613ms
  • 内存:336996kb
  • [2024-05-30 11:39:43]
  • 提交

answer

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define sz(a) ((int) (a).size())
#define vi vector < int >
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
#define pb emplace_back
using namespace std;
const int N = 1 << 21;
struct edge {
	int u, v;
	ll w;
};
std::vector<edge> generate_edges(int n, int m, unsigned seed) {
    std::mt19937 gen(seed);
    std::vector<edge> edges(m);
    unsigned max = -1u / n * n;
    auto sample = [&]() {
        unsigned x;
        do { x = gen(); } while(x >= max);
        return x % n + 1;
    };
	for(auto&e : edges) {
		e.u = sample();
		e.v = sample();
		e.w = gen();
    } // u 到 v 存在边权为 w 的边
    return edges;
}
int n, m, q;
ll seed;
vector<pair<int,ll>>e[N], re[N];
ll dis[N];
int lst[N];
vi road[N];
ll lstw[N];
void dijkstra(int pos) {
	priority_queue<pair<ll,int>>pq;
	pq.push({0, pos});
	L(i, 1, n) dis[i] = 1e18;
	vi vis(n + 1);
	dis[pos] = 0;
	while(!pq.empty()) {
		auto u = pq.top().second;
		pq.pop();
		if(vis[u])continue;
		if(lst[u]) {
			road[u] = road[lst[u]], road[u].pb(u);
		}
		vis[u] = 1;
		for(auto&E : e[u]) {
			int to = E.first;
			if(dis[to] > dis[u] + E.second) {
				dis[to] = dis[u] + E.second;
				pq.push({-dis[to], to});
				lstw[to] = E.second;
				lst[to] = u;
			}
		}
	}
}
ll dis1[N], dis2[N];
int vis[N], que[N], all;
int use[N];
mt19937_64 rnd;
inline int rad(int l, int r) {
	return rnd() % (r - l + 1) + l;
}
int pick[N], seg1[N], seg2[N];
const int xN = 1 << 18;
void upd1(int x) {
	seg1[x + xN] = x;
	for(int u = (x + xN) >> 1; u; u >>= 1) {
		if(dis1[x] >= dis1[seg1[u]]) break;
		seg1[u] = x;
	}
}
void del1(int x) {
	seg1[x + xN] = 0;
	for(int u = (x + xN) >> 1; u; u >>= 1) 
		seg1[u] = dis1[seg1[u * 2]] < dis1[seg1[u * 2 + 1]] ? seg1[u * 2] : seg1[u * 2 + 1];
}

void upd2(int x) {
	seg2[x + xN] = x;
	for(int u = (x + xN) >> 1; u; u >>= 1) {
		if(dis2[x] >= dis2[seg2[u]]) break;
		seg2[u] = x;
	}
}
void del2(int x) {
	seg2[x + xN] = 0;
	for(int u = (x + xN) >> 1; u; u >>= 1) 
		seg2[u] = dis2[seg2[u * 2]] < dis2[seg2[u * 2 + 1]] ? seg2[u * 2] : seg2[u * 2 + 1];
}
int ban1[N], ban2[N];
const int M = 1 << 18;
int main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	// n = 2e5, m = 3e6, q = 1000, seed = 114514;
	cin >> n >> m >> q >> seed;
	auto E = generate_edges(n, m, seed);
	sort(E.begin(), E.end(), [&] (edge a, edge b) {
		return a.w < b.w;
	});
	for(auto&ed : E) if(ed.u != ed.v) e[ed.u].pb(ed.v, ed.w), re[ed.v].pb(ed.u, ed.w);
	// dijkstra(1);
	// L(i, 1, n) {
	// 	dist[i] = dis[i];
	// }
	
	// L(s, 0, 100) E.erase(E.begin());
	// L(i, 1, n) e[i].clear();
	// for(auto&ed : E) e[ed.u].pb(ed.v, ed.w);
	// dijkstra(1);
	// int cnt = 0;
	// L(i, 1, n) {
	// 	if(dist[i] != dis[i]) {
	// 		++cnt;
	// 	}
	// }
	// cout << cnt << endl;

	L(i, 0, n) dis1[i] = dis2[i] = 1e18;
	while(q--) {
		int x, y;
		x = rad(1, n);
		y = rad(1, n);
		cin >> x >> y;
		if(x == y) {
			cout << 0 << '\n';
			continue;
		} 
		all = 2, que[1] = x, que[2] = y, vis[x] = vis[y] = 1;
		dis1[x] = 0, dis2[y] = 0;
		upd1(x), upd2(y);
		ll ans = 1e18;
		while(true) {
			if(min(dis1[seg1[1]], dis2[seg2[1]]) * 2 >= ans) {
				break;
			}
			if(dis1[seg1[1]] < dis2[seg2[1]]) {
				int u = seg1[1];
				del1(u);
				// cout << "u = " << u << ' ' << dis1[u] << endl;
				for(auto&E : e[u]) {
					int to = E.first;
					if(dis1[to] > dis1[u] + E.second) {
						dis1[to] = dis1[u] + E.second;
						if(!vis[to]) vis[to] = 1, que[++all] = to;
						else ans = min(ans, dis1[to] + dis2[to]);
						if(dis1[to] * 2 < ans) upd1(to);
					}
				}
			} else {
				int u = seg2[1];
				del2(u);
				for(auto&E : re[u]) {
					int to = E.first;
					if(dis2[to] > dis2[u] + E.second) {
						dis2[to] = dis2[u] + E.second;
						if(!vis[to]) vis[to] = 1, que[++all] = to;
						else ans = min(ans, dis1[to] + dis2[to]);
						if(dis2[to] * 2 < ans) upd2(to);
					}
				}
			}
		}
		L(i, 1, all) dis1[que[i]] = dis2[que[i]] = 1e18, vis[que[i]] = 0;
		if(ans > 1e17) cout << -1 << '\n';
		else cout << ans << '\n';
	}
	// L(i, 1, n) if(i % 10 == 0) cout << 1. * dis[i] / mx << ' ' << sz(road[i]) << endl;
	
	// L(i, 1, n) cout << sz(road[i]) << ' ';
	// cout << endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 5
Accepted
time: 19ms
memory: 168624kb

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: 0
Accepted
time: 16ms
memory: 168844kb

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:

ok 2000 lines

Test #3:

score: -5
Wrong Answer
time: 57ms
memory: 168404kb

input:

1000 2000 2000 1526732796
400 914
837 927
7 271
873 60
934 156
981 329
973 512
276 54
540 278
605 230
681 555
636 706
955 618
640 214
859 696
267 595
38 839
309 12
484 919
746 49
948 337
607 638
438 163
817 869
95 518
534 376
369 331
665 64
736 970
154 41
510 425
876 907
143 803
270 403
350 286
131 ...

output:

14198403396
-1
20203456441
11552404306
16160464812
27144556597
-1
5570702410
-1
19513776618
10597134504
8945453029
20326028889
-1
12608727274
17050357023
-1
-1
15134668738
19589312812
32078322699
16255615559
-1
20150114514
15485138820
-1
5265380455
-1
19291857101
-1
-1
-1
20197008935
17619903738
-1
...

result:

wrong answer 33rd lines differ - expected: '19427108277', found: '20197008935'

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 3613ms
memory: 336996kb

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
-1
54655397
-1
66733258
40352182
59908954
39110431
69912141
47214491
71729739
78477841
56946348
-1
84851928
61929012
47785308
-1
-1
74176422
81572572
97088081
-1
-1
43789135
66104645
37205410
50006799
35033769
53332277
63805951
60228359
-1
79777545
53652058
61036023
449527...

result:

wrong answer 4th lines differ - expected: '27805775', found: '-1'

Subtask #3:

score: 10
Accepted

Test #13:

score: 10
Accepted
time: 348ms
memory: 187660kb

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:

ok 10000 lines

Test #14:

score: 0
Accepted
time: 55ms
memory: 181000kb

input:

200000 100000 10000 1394653802
99794 128174
196511 141958
176353 6707
19037 95308
12331 132159
47825 12373
47277 130874
165656 114428
81800 12371
165878 128160
33280 71225
139344 138789
126396 182051
103407 151857
20873 18698
155652 38063
150807 191146
57310 174863
114490 88197
158133 29636
137962 1...

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:

ok 10000 lines

Test #15:

score: 0
Accepted
time: 104ms
memory: 178600kb

input:

100000 100000 10000 913053279
28316 35031
36768 9164
74111 12192
71120 23394
97477 34141
50880 24433
99500 23365
99785 571
95784 50853
8313 70744
33410 27807
29073 96498
82964 79943
32999 84423
90798 98756
98245 89258
89589 49557
90152 40866
53406 41385
33889 39018
42199 52421
13784 26639
85311 5769...

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:

ok 10000 lines

Test #16:

score: 0
Accepted
time: 16ms
memory: 161244kb

input:

1 1 10000 1920830832
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
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...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 10000 lines

Subtask #4:

score: 0
Time Limit Exceeded

Test #17:

score: 0
Time Limit Exceeded

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
30333017011
19365143518
-1
-1
33507263304
23396138679
19478702596
-1
-1
-1
20149023019
23727970709
24229890807
28875639856
-1
22877254445
25605430611
29065653090
-1
40180239767
25161604327
-1
24613723208
19348763468
-1
25490848431
212347766...

result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

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:


Subtask #6:

score: 0
Time Limit Exceeded

Test #24:

score: 0
Time Limit Exceeded

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
2421541219
1956726372
1831208977
1250925907
2109206110
2124574785
2228685790
1937527554
2859407309
3873219523
-1
2829292936
-1
1971567227
2150629777
3996662822
3131069774
3642386093
4026096296
2818109409
3959164366
16263236...

result:


Subtask #7:

score: 0
Time Limit Exceeded

Test #33:

score: 0
Time Limit Exceeded

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
4420290357
4918664465
4136524360
3978712900
4211432244
4134849186
3320322266
4765588952
5922250935
8511005290
3664302146
7375189344
-1
6306639305
6924759359
8505305851
4383406189
4671581259
-1
5825132311
5905242114
4254638128
3597537847
-1
5425741929
6168182537
4526246471
-1
-1...

result: