QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#562323#8673. 最短路径lichenghan0 2334ms175776kbC++142.5kb2024-09-13 16:46:262024-09-13 16:46:28

Judging History

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

  • [2024-09-13 16:46:28]
  • 评测
  • 测评结果:0
  • 用时:2334ms
  • 内存:175776kb
  • [2024-09-13 16:46:26]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+10;
int n,m,k;
unsigned seed;
struct edge_t{
	int v; ll w;
	friend bool operator<(const edge_t& ls,const edge_t& rs){ return ls.w<rs.w; }
};
struct node{
	ll d;
	int u,i; // the i-th edge of u
	friend bool operator<(const node& ls,const node& rs){ return ls.d>rs.d; }
};
vector<edge_t> g[N],gr[N];
void init_graph(){
	std::mt19937 gen(seed);
	unsigned max = -1u / n * n;
	auto sample = [&]() {
		unsigned x;
		do { x = gen(); } while(x >= max);
		return x % n + 1;
	};
	for(int i=1;i<=m;i++) {
		int u = sample();
		int v = sample();
		ll w = gen();
		g[u].push_back({v,w});
		gr[v].push_back({u,w});
	}
	for(int i=1;i<=n;i++) {
		sort(g[i].begin(),g[i].end());
		sort(gr[i].begin(),gr[i].end());
	}
}
ll ds[N],dt[N];
bool vs[N],vt[N];
ll query(int s,int t){
	if(s==t) return 0;
	if(!g[s].size()||!gr[t].size()) return -1;
	// puts("here");
	vector<int> us{s},ut{t};
	priority_queue<node> qs,qt;
	ds[s]=0; vs[s]=1; qs.push({g[s][0].w,s,0});
	dt[t]=0; vt[t]=1; qt.push({gr[t][0].w,t,0});
	int mid=-1;
	while(!qs.empty()&&!qt.empty()){
		if(qs.size()<=qt.size()){
			auto [d,u,i]=qs.top(); qs.pop();
			int v=g[u][i].v;
			for(int j=i+1;j<g[u].size();++j) if(!vs[g[u][j].v]) {
				qs.push({ds[u]+g[u][j].w,u,j}); break;
			}
			if(vs[v]) continue;
			// printf("ds %d = %lld\n",v,d);
			vs[v]=true; ds[v]=d; us.push_back(v);
			if(vt[v]){ mid=v; break; }
			if(g[v].size()) qs.push({d+g[v][0].w,v,0});
		}else{
			auto [d,u,i]=qt.top(); qt.pop();
			int v=gr[u][i].v;
			for(int j=i+1;j<gr[u].size();++j) if(!vt[gr[u][j].v]) {
				qt.push({ds[u]+gr[u][j].w,u,j}); break;
			}
			if(vt[v]) continue;
			// printf("dt %d = %lld\n",v,d);
			vt[v]=true; dt[v]=d; ut.push_back(v);
			if(vs[v]){ mid=v; break; }
			if(gr[v].size()) qt.push({d+gr[v][0].w,v,0});
		}
	}
	if(mid==-1){
		for(int i:us) vs[i]=false;
		for(int i:ut) vt[i]=false;
		return -1;
	}
	ll ans=ds[mid]+dt[mid];
	for(int u:us){
		ll lim=(ds[mid]-ds[u])<<1;
		for(auto e:g[u]){
			if(e.w>=lim) break;
			if(vt[e.v]) ans=min(ans,ds[u]+e.w+dt[e.v]);
		}
	}
	for(int u:ut) {
		ll lim=(dt[mid]-dt[u])<<1;
		for(auto e:gr[u]){
			if(e.w>=lim) break;
			if(vs[e.v]) ans=min(ans,ds[e.v]+e.w+dt[u]);
		}
	}
	for(int u:us) vs[u]=false;
	for(int u:ut) vt[u]=false;
	return ans;
}
signed main(){
	scanf("%d%d%d%u",&n,&m,&k,&seed);
	init_graph();
	while(k--){
		int s,t;
		scanf("%d%d",&s,&t);
		printf("%lld\n",query(s,t));
	}
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 5
Accepted
time: 0ms
memory: 16612kb

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
Wrong Answer
time: 2ms
memory: 16472kb

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: '25932394336'

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 449ms
memory: 139364kb

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:

28161425
35728028
32202812
23792977
31191764
22851011
30402685
27383012
24870421
19075088
34821106
24509585
32737431
23696150
25983590
29928616
30999529
24191739
32611278
24829874
18789286
32681332
24709227
26432441
26653089
24891760
23914546
37045939
17893812
30132744
32027765
34129023
22679494
292...

result:

wrong answer 1st lines differ - expected: '36084543', found: '28161425'

Subtask #3:

score: 0
Wrong Answer

Test #13:

score: 0
Wrong Answer
time: 47ms
memory: 27492kb

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 679th lines differ - expected: '172751101166', found: '78327710551'

Subtask #4:

score: 0
Wrong Answer

Test #17:

score: 0
Wrong Answer
time: 1266ms
memory: 43216kb

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:

12266976219
-1
19720208875
9724031597
-1
12740960888
-1
-1
16092227260
12491022552
-1
-1
19162785493
16905799379
14721529276
-1
-1
-1
14648299924
14070142549
13589333959
17142218382
-1
13038987100
18070260158
15764373665
-1
17453232132
18652846889
-1
13845286962
12326565644
-1
16128104815
1315639384...

result:

wrong answer 1st lines differ - expected: '21671419385', found: '12266976219'

Subtask #5:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 1229ms
memory: 43232kb

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:

13903562283
15971897353
12091137846
13721677016
-1
12785171291
14886416997
12419913827
10902170806
16023939707
12893942694
10257207280
16934055682
15099806053
16486662347
13138546004
12415388299
-1
-1
14151421154
10348384324
14568144390
16259206882
15728756673
13464130386
11891343649
14733650936
105...

result:

wrong answer 1st lines differ - expected: '18098332289', found: '13903562283'

Subtask #6:

score: 0
Wrong Answer

Test #24:

score: 0
Wrong Answer
time: 1554ms
memory: 173388kb

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:

1151688137
1014926539
1088027206
1290996836
783329713
1001603895
957288369
1321560788
1034761722
1391397123
905498144
1269898265
976744247
848919834
891531703
1101789647
1385932730
1312553962
1104028533
974582129
717000425
1020461307
1183626162
859921794
1360889876
1185978109
938791004
810132972
853...

result:

wrong answer 1st lines differ - expected: '1547972368', found: '1151688137'

Subtask #7:

score: 0
Wrong Answer

Test #33:

score: 0
Wrong Answer
time: 2334ms
memory: 175776kb

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:

2604806539
1823235949
2272059067
3036772578
2066194436
2183578370
2904109547
2270004858
1643612815
2674444133
1758696450
2210140080
1722325365
1712562724
2614198784
2832356559
2240846225
2366030366
1978266999
1795974746
2014311095
1964012043
2370806502
2582700694
2311241416
2700700151
2832754020
235...

result:

wrong answer 1st lines differ - expected: '3371897180', found: '2604806539'