QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#76798#5506. HyperloopthemoonAC ✓4680ms24576kbC++143.9kb2023-02-12 10:44:112023-02-12 10:44:13

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-12 10:44:13]
  • 评测
  • 测评结果:AC
  • 用时:4680ms
  • 内存:24576kb
  • [2023-02-12 10:44:11]
  • 提交

answer

#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int,int> pr;
const int N=1e5+5;
const int M=3e5+5;
const int INF=50005;
int n,m;
int Head[N],Nxt[M<<1],ver[M<<1],val[M<<1],cnt_edge;
int ind[N];
int uu[M<<1],vv[M<<1],vall[M<<1],cnt;
int dist[N];
inline int Read(){
	char ch;
	int f=1;
	while((ch=getchar())<'0'||ch>'9')
		if(ch=='-') f=-1;
	int x=ch^48;
	while((ch=getchar())>='0'&&ch<='9')
		x=(x<<3)+(x<<1)+(ch^48);
	return x*f;
}
inline void print(int x){
	if(x>=10) print(x/10);
	putchar(x%10+48);
	return ;
}
inline void Print(int x,char ch='\n'){
	if(x<0){
		putchar('-');
		print(-x);
	}
	else print(x);
	putchar(ch);
	return ;
}
inline void Add(int u,int v,int value){
	++cnt_edge;
	ver[cnt_edge]=v;
	val[cnt_edge]=value;
	Nxt[cnt_edge]=Head[u];
	Head[u]=cnt_edge;
	return ;
}
inline void Add_Edge(int u,int v,int value){
	Add(u,v,value);
	Add(v,u,value);
	ind[v]++;
	return ;
}
inline void Init(){
	cnt=0;
	n=Read();
	cnt_edge=-1;
	for(int i=1;i<=n;i++){
		Head[i]=-1;
		dist[i]=INF;
	}
	for(m=Read();m;m--){
		int u=Read(),v=Read();
		int value=Read();
		Add_Edge(u,v,value);
		++cnt;
		uu[cnt]=u;
		vv[cnt]=v;
		vall[cnt]=value;
		++cnt;
		uu[cnt]=v;
		vv[cnt]=u;
		vall[cnt]=value;
	}
	return ;
}
priority_queue<pr,vector<pr>,greater<pr> >q;
bool vis[N];
inline void Dij(){
	for(int i=1;i<=n;i++)
		vis[i]=0;
	dist[1]=0;
	q.push(make_pair(0,1));
	while(q.size()){
		int u=q.top().second;
		q.pop();
		if(vis[u]) continue ;
		vis[u]=1;
		//printf("u=%d\n",u);
		for(int i=Head[u];i!=-1;i=Nxt[i]){
			int v=ver[i];
			//printf("v=%d\n",v);
			if(dist[v]>dist[u]+val[i]){
				dist[v]=dist[u]+val[i];
				q.push(make_pair(dist[v],v));
			}
		}
	}
	for(int i=1;i<=n;i++)
		vis[i]=0;
	for(int i=1;i<=n;i++){
		Head[i]=-1;
		ind[i]=0;
	}
	cnt_edge=-1;
	for(int i=1;i<=cnt;i++){
		int u=uu[i],v=vv[i];
		if(dist[v]==dist[u]+vall[i]) Add_Edge(u,v,vall[i]),vis[vall[i]]=1;
	}
	return ;
}
queue<int>que;
int to[N];
inline void Dfs(int u,int x){
	if(to[u]!=-1) return ;
	to[u]=0;
	for(int i=Head[u];i!=-1;i=Nxt[i]){
		if(i%2==1) continue ;
		int v=ver[i];
		if(dist[v]!=dist[u]+(val[i]==x)) continue ;
		Dfs(v,x);
		to[u]=to[u]||to[v];
	}
	return ;
}
inline void Bfs(int x){
	//printf("x=%d\n",x);
	cnt=0;
	for(int i=0;i<=cnt_edge;i+=2){
		int v=ver[i],u=ver[i^1];
		vis[val[i]]=0;
		++cnt;
		uu[cnt]=u;
		vv[cnt]=v;
		vall[cnt]=val[i];
	}
	for(int i=1;i<=n;i++)
		dist[i]=0;
	dist[1]=0;
	que.push(1);
	while(que.size()){
		int u=que.front();
		que.pop();
		for(int i=Head[u];i!=-1;i=Nxt[i]){
			if(i%2==1) continue ;
			int v=ver[i];
			int dis=dist[u]+(val[i]==x);
			if(dis>dist[v]) dist[v]=dis;
			--ind[v];
			if(ind[v]==0) que.push(v);
		}
	}
	for(int i=1;i<=n;i++)
		to[i]=-1;
	to[n]=1;
	for(int i=1;i<n;i++)
		Dfs(i,x);
	for(int i=1;i<=n;i++){
		Head[i]=-1;
		ind[i]=0;
	}
	cnt_edge=-1;
	for(int i=1;i<=cnt;i++){
		int u=uu[i],v=vv[i];
		if(to[u]==1&&to[v]==1&&dist[v]==dist[u]+(vall[i]==x)) Add_Edge(u,v,vall[i]),vis[vall[i]]=1;
	}
	return ;
}
int pre[N];
inline void Dfs(int u){
	vis[u]=1;
	for(int i=Head[u];i!=-1;i=Nxt[i]){
		if(i%2==1) continue ;
		int v=ver[i];
		if(vis[v]) continue ;
		pre[v]=u;
		Dfs(v);
	}
	return ;
}
int ans;
inline void Find(int u){
	ans++;
	if(u==1){
		Print(ans);
		Print(u,' ');
		return ;
	}
	Find(pre[u]);
	Print(u,' ');
	return ;
}
inline void Solve(){
	Dij();
	//Print(dist[n]);
	for(int i=50000;i;i--)
		if(vis[i]) Bfs(i);
	for(int i=1;i<=cnt;i++)
		vis[vall[i]]=0;
	Dfs(1);
	ans=0;
	Find(n);
	for(int i=1;i<=n;i++)
		vis[i]=0;
	putchar('\n');
	return ;
}
int T;
#include<ctime>
int main(){
	//#define LOCAL
	#ifdef LOCAL
	int st=clock();
	#endif
	for(T=Read();T;T--){
		Init();
		Solve();
	}
	#ifdef LOCAL
	int en=clock();
	printf("cost %d ms\n",en-st);
	#endif
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 2852kb

input:

2
4 6
1 2 1
1 3 2
2 3 1
2 4 2
3 4 1
1 4 4
6 11
1 2 9
2 3 12
3 4 3
4 5 5
5 6 10
6 1 22
2 4 9
3 6 1
4 6 5
2 5 2
3 5 8

output:

3
1 3 4 
5
1 2 5 3 6 

result:

ok correct (2 test cases)

Test #2:

score: 0
Accepted
time: 341ms
memory: 3108kb

input:

600
320 1547
204 81 13768
232 97 9939
97 249 3719
201 109 14322
183 132 40881
142 143 1
275 186 24548
18 236 7907
30 317 11845
131 130 1
311 300 11704
141 92 41925
174 191 32128
119 120 1
184 183 1
310 309 1
283 270 25477
233 141 36076
212 92 13770
307 110 40656
218 137 14033
180 85 41892
200 199 44...

output:

184
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10...

result:

ok correct (600 test cases)

Test #3:

score: 0
Accepted
time: 615ms
memory: 24576kb

input:

4
100000 220000
48940 43355 42347
77914 77913 1
45236 82683 42904
22563 16038 34866
81537 81538 43088
49803 51485 25497
63071 25523 14336
44102 39850 43782
13607 92386 16724
98711 73651 46840
17775 16801 28765
5757 98829 13508
85095 48444 1
9198 43003 32678
14461 14462 1
20245 48742 18138
89120 8911...

output:

35000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 10...

result:

ok correct (4 test cases)

Test #4:

score: 0
Accepted
time: 4680ms
memory: 19812kb

input:

4
100000 160000
5533 94547 28459
14992 20984 20548
70133 92512 27510
9013 9012 304
13621 40571 47787
305 306 262
6987 6988 135
16234 16992 40656
26246 49196 27701
19103 60272 44055
91532 91531 38290
70778 68341 35147
32524 32523 13
85786 50300 40970
49277 29735 13942
43446 34519 42455
77623 17031 34...

output:

316
1 2 3 4 5 6 97410 97409 26434 26435 26436 26437 98883 1370 1369 1368 92157 92158 4815 4816 4817 4818 50181 16985 89607 89608 24674 16979 16980 38428 13232 13233 13234 13664 13663 95009 7166 7171 97126 7163 24798 24799 11787 31031 53551 7309 7310 35482 7933 25067 32714 32715 44194 2068 72216 7959...

result:

ok correct (4 test cases)

Test #5:

score: 0
Accepted
time: 364ms
memory: 22644kb

input:

4
100000 160000
89517 25671 43802
21059 21058 1
35299 91834 43615
53383 53382 1
27213 39161 17202
10715 4050 30342
44100 44099 1
24162 28648 7378
19022 23084 37734
66056 97934 14651
31566 89391 23215
91038 91037 1
47695 47696 6099
99142 99143 1
83908 73654 15060
15551 22001 8896
55190 55189 1
26536 ...

output:

632
1 94660 94659 31256 1697 99628 31243 99467 31285 87899 31189 31212 91832 31210 2471 91847 2505 98560 2509 2510 2585 2516 2523 99749 71980 74130 74129 99693 84670 88173 88172 88171 88170 88169 88168 88167 88166 88165 88164 88163 88162 88161 88160 88159 88158 88157 88156 91942 91941 91940 91939 99...

result:

ok correct (4 test cases)