답안 #831162

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
831162 2023-08-19T20:21:37 Z trs4630 Two Transportations (JOI19_transportations) C++14
84 / 100
506 ms 49528 KB
#include "Azer.h"
#include <stdlib.h>
#include <cassert>
#include <queue>
#define MAXN 2007
#define MAXM 500007
#define vi std::vector<int>
#define pii std::pair<int,int>
#define vpii std::vector<pii>
#define pqpii std::priority_queue<pii,vpii,std::greater<pii>>
#define LOOP(I,X) for(int I=0;I<X;I++)
#define pub push_back
using namespace std;
namespace{
	enum Mode{
		CL, // choose leader
		RE, // recommend edge
		DW, // determine winner
		SR, // send relax
		RX, // relax
	};
	Mode mode;
	bool leader;
	bool winner;
	int n;
	int *dis;
	bool *visited;
	vpii *e;
	pqpii pq;
	pii rec;
	int rec_other;
	int relax_w=0;
	int relax_v;
	bool *buf;
	int buf_size=0;
	int buf_now=0;
	int t=0;
	int bits_next;
int cnt=0;
	int Read(int bits){
		int ans=0;
		LOOP(i,bits){
			ans<<=1;
			ans|=((int)(buf[buf_now+i]));
		}
		buf_now+=bits;
		return ans;
	}
	void Send(int bits,int x){
		LOOP(i,bits){
cnt++;
// assert(buf_size+cnt<=58000);
			SendA((bool)((x>>(bits-1-i))&1));
		}
	}
	void Do(){
		while(t<n-1){
			if(mode==CL){
				mode=RE;
				if(leader){
					bool new_leader=(bool)(rand()%2);
					Send(1,(int)new_leader);
					if(new_leader){
						leader=!leader;
					}
				}else{
					bool new_leader=(bool)Read(1);
					if(new_leader){
						leader=!leader;
					}
				}
				if(leader){
					bits_next=9;
					return;
				}
			}
			if(mode==RE){
				mode=DW;
				while(!pq.empty()){
					pii x=pq.top();
					if(!visited[x.second]){
						break;
					}
					pq.pop();
				}
				if(pq.empty()){
					rec={511,-1};
				}else{
					rec=pq.top();
					rec.first-=relax_w;
				}
				if(leader){
					rec_other=Read(9);
				}else{
					Send(9,(int)rec.first);
					bits_next=1;
					return;
				}
			}
			if(mode==DW){
				mode=SR;
				if(leader){
					if(rec.first<=rec_other){
						Send(1,1);
						winner=true;
					}else{
						Send(1,0);
						winner=false;
					}
				}else{
					winner=!((bool)Read(1));
				}
				if(!winner){
					if(leader){
						bits_next=11;
					}else{
						bits_next=20;
					}
					return;
				}
			}
			if(mode==SR){
				mode=RX;
				if(leader){
					if(winner){
						Send(11,(int)rec.second);
						relax_v=rec.second;
						Send(9,(int)rec.first);
						relax_w+=rec.first;
					}else{
						relax_v=Read(11);
						relax_w+=rec_other;
					}
				}else{
					if(winner){
						Send(11,(int)rec.second);
						relax_v=rec.second;
						relax_w+=rec.first;
					}else{
						relax_v=Read(11);
						relax_w+=Read(9);
					}
				}
			}
			if(mode==RX){
				mode=CL;
				dis[relax_v]=relax_w;
				visited[relax_v]=true;
				for(pii x:e[relax_v]){
					if(!visited[x.first]){
						pq.push({relax_w+x.second,x.first});
					}
				}
				t++;
				if(!leader){
					bits_next=1;
					return;
				}
			}
		}
	}
}
void InitA(int N,int A,vi U,vi V,vi C){
	dis=(int *)malloc(sizeof(int)*MAXN);
	visited=(bool *)malloc(sizeof(bool)*MAXN);
	LOOP(i,MAXN){
		visited[i]=false;
	}
	e=(vpii *)malloc(sizeof(vpii)*MAXN);
	buf=(bool *)malloc(sizeof(bool)*58007);
	srand(49);
	n=N;
	LOOP(i,A){
		e[U[i]].pub({V[i],C[i]});
		e[V[i]].pub({U[i],C[i]});
	}
	dis[0]=0;
	visited[0]=true;
	for(pii x:e[0]){
		pq.push({x.second,x.first});
	}
	mode=CL;
	leader=true;
	Do();
}
void ReceiveA(bool x){
	buf[buf_size]=x;
	buf_size++;
	if(buf_size-buf_now==bits_next){
		Do();
	}
}
vi Answer(){
	vi ans;
	LOOP(i,n){
		ans.pub(dis[i]);
	}
	return ans;
}
#include "Baijan.h"
#include <stdlib.h>
#include <cassert>
#include <queue>
#define MAXN 2007
#define MAXM 500007
#define vi std::vector<int>
#define pii std::pair<int,int>
#define vpii std::vector<pii>
#define pqpii std::priority_queue<pii,vpii,std::greater<pii>>
#define LOOP(I,X) for(int I=0;I<X;I++)
#define pub push_back
using namespace std;
namespace{
	enum Mode{
		CL, // choose leader
		RE, // recommend edge
		DW, // determine winner
		SR, // send relax
		RX, // relax
	};
	Mode mode;
	bool leader;
	bool winner;
	int n;
	int *dis;
	bool *visited;
	vpii *e;
	pqpii pq;
	pii rec;
	int rec_other;
	int relax_w=0;
	int relax_v;
	bool *buf;
	int buf_size=0;
	int buf_now=0;
	int t=0;
	int bits_next;
int cnt=0;
	int Read(int bits){
		int ans=0;
		LOOP(i,bits){
			ans<<=1;
			ans|=((int)buf[buf_now+i]);
		}
		buf_now+=bits;
		return ans;
	}
	void Send(int bits,int x){
		LOOP(i,bits){
cnt++;
// assert(buf_size+cnt<=58000);
			SendB((bool)((x>>(bits-1-i))&1));
		}
	}
	void Do(){
		while(t<n-1){
			if(mode==CL){
				mode=RE;
				if(leader){
					bool new_leader=(bool)(rand()%2);
					Send(1,(int)new_leader);
					if(new_leader){
						leader=!leader;
					}
				}else{
					bool new_leader=(bool)Read(1);
					if(new_leader){
						leader=!leader;
					}
				}
				if(leader){
					bits_next=9;
					return;
				}
			}
			if(mode==RE){
				mode=DW;
				while(!pq.empty()){
					pii x=pq.top();
					if(!visited[x.second]){
						break;
					}
					pq.pop();
				}
				if(pq.empty()){
					rec={511,-1};
				}else{
					rec=pq.top();
					rec.first-=relax_w;
				}
				if(leader){
					rec_other=Read(9);
				}else{
					Send(9,(int)rec.first);
					bits_next=1;
					return;
				}
			}
			if(mode==DW){
				mode=SR;
				if(leader){
					if(rec.first<=rec_other){
						Send(1,1);
						winner=true;
					}else{
						Send(1,0);
						winner=false;
					}
				}else{
					winner=!((bool)Read(1));
				}
				if(!winner){
					if(leader){
						bits_next=11;
					}else{
						bits_next=20;
					}
					return;
				}
			}
			if(mode==SR){
				mode=RX;
				if(leader){
					if(winner){
						Send(11,(int)rec.second);
						relax_v=rec.second;
						Send(9,(int)rec.first);
						relax_w+=rec.first;
					}else{
						relax_v=Read(11);
						relax_w+=rec_other;
					}
				}else{
					if(winner){
						Send(11,(int)rec.second);
						relax_v=rec.second;
						relax_w+=rec.first;
					}else{
						relax_v=Read(11);
						relax_w+=Read(9);
					}
				}
			}
			if(mode==RX){
				mode=CL;
				dis[relax_v]=relax_w;
				visited[relax_v]=true;
				for(pii x:e[relax_v]){
					if(!visited[x.first]){
						pq.push({relax_w+x.second,x.first});
					}
				}
				t++;
				if(!leader){
					bits_next=1;
					return;
				}
			}
		}
	}
}
void InitB(int N,int A,vi U,vi V,vi C){
	dis=(int *)malloc(sizeof(int)*MAXN);
	visited=(bool *)malloc(sizeof(bool)*MAXN);
	LOOP(i,MAXN){
		visited[i]=false;
	}
	e=(vpii *)malloc(sizeof(vpii)*MAXN);
	buf=(bool *)malloc(sizeof(bool)*58007);
	srand(49);
	n=N;
	LOOP(i,A){
		e[U[i]].pub({V[i],C[i]});
		e[V[i]].pub({U[i],C[i]});
	}
	dis[0]=0;
	visited[0]=true;
	for(pii x:e[0]){
		pq.push({x.second,x.first});
	}
	mode=CL;
	leader=false;
	bits_next=1;
}
void ReceiveB(bool x){
	buf[buf_size]=x;
	buf_size++;
	if(buf_size-buf_now==bits_next){
		Do();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 272 ms 768 KB Output is correct
2 Correct 0 ms 400 KB Output is correct
3 Correct 305 ms 888 KB Output is correct
4 Correct 422 ms 10156 KB Output is correct
5 Correct 17 ms 752 KB Output is correct
6 Correct 277 ms 2604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 400 KB Output is correct
2 Correct 294 ms 992 KB Output is correct
3 Correct 318 ms 872 KB Output is correct
4 Correct 462 ms 27664 KB Output is correct
5 Correct 412 ms 24492 KB Output is correct
6 Correct 70 ms 652 KB Output is correct
7 Correct 449 ms 24764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 319 ms 956 KB Output is correct
2 Correct 0 ms 400 KB Output is correct
3 Correct 295 ms 876 KB Output is correct
4 Correct 333 ms 844 KB Output is correct
5 Correct 315 ms 980 KB Output is correct
6 Correct 277 ms 844 KB Output is correct
7 Correct 267 ms 896 KB Output is correct
8 Correct 302 ms 928 KB Output is correct
9 Correct 297 ms 1032 KB Output is correct
10 Correct 299 ms 812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 156 ms 668 KB Output is correct
2 Correct 120 ms 672 KB Output is correct
3 Correct 234 ms 13316 KB Output is correct
4 Correct 106 ms 728 KB Output is correct
5 Correct 231 ms 9964 KB Output is correct
6 Correct 147 ms 692 KB Output is correct
7 Correct 139 ms 872 KB Output is correct
8 Correct 151 ms 752 KB Output is correct
9 Correct 238 ms 19628 KB Output is correct
10 Correct 227 ms 19436 KB Output is correct
11 Correct 391 ms 38220 KB Output is correct
12 Correct 318 ms 36020 KB Output is correct
13 Correct 118 ms 860 KB Output is correct
14 Correct 1 ms 400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 156 ms 668 KB Output is correct
2 Correct 120 ms 672 KB Output is correct
3 Correct 234 ms 13316 KB Output is correct
4 Correct 106 ms 728 KB Output is correct
5 Correct 231 ms 9964 KB Output is correct
6 Correct 147 ms 692 KB Output is correct
7 Correct 139 ms 872 KB Output is correct
8 Correct 151 ms 752 KB Output is correct
9 Correct 238 ms 19628 KB Output is correct
10 Correct 227 ms 19436 KB Output is correct
11 Correct 391 ms 38220 KB Output is correct
12 Correct 318 ms 36020 KB Output is correct
13 Correct 118 ms 860 KB Output is correct
14 Correct 1 ms 400 KB Output is correct
15 Correct 175 ms 708 KB Output is correct
16 Correct 182 ms 828 KB Output is correct
17 Correct 197 ms 652 KB Output is correct
18 Correct 255 ms 10136 KB Output is correct
19 Correct 185 ms 996 KB Output is correct
20 Correct 282 ms 10192 KB Output is correct
21 Correct 111 ms 820 KB Output is correct
22 Correct 198 ms 796 KB Output is correct
23 Correct 330 ms 22332 KB Output is correct
24 Correct 297 ms 22308 KB Output is correct
25 Correct 465 ms 44092 KB Output is correct
26 Correct 386 ms 40504 KB Output is correct
27 Correct 158 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 156 ms 668 KB Output is correct
2 Correct 120 ms 672 KB Output is correct
3 Correct 234 ms 13316 KB Output is correct
4 Correct 106 ms 728 KB Output is correct
5 Correct 231 ms 9964 KB Output is correct
6 Correct 147 ms 692 KB Output is correct
7 Correct 139 ms 872 KB Output is correct
8 Correct 151 ms 752 KB Output is correct
9 Correct 238 ms 19628 KB Output is correct
10 Correct 227 ms 19436 KB Output is correct
11 Correct 391 ms 38220 KB Output is correct
12 Correct 318 ms 36020 KB Output is correct
13 Correct 118 ms 860 KB Output is correct
14 Correct 1 ms 400 KB Output is correct
15 Correct 175 ms 708 KB Output is correct
16 Correct 182 ms 828 KB Output is correct
17 Correct 197 ms 652 KB Output is correct
18 Correct 255 ms 10136 KB Output is correct
19 Correct 185 ms 996 KB Output is correct
20 Correct 282 ms 10192 KB Output is correct
21 Correct 111 ms 820 KB Output is correct
22 Correct 198 ms 796 KB Output is correct
23 Correct 330 ms 22332 KB Output is correct
24 Correct 297 ms 22308 KB Output is correct
25 Correct 465 ms 44092 KB Output is correct
26 Correct 386 ms 40504 KB Output is correct
27 Correct 158 ms 980 KB Output is correct
28 Correct 200 ms 760 KB Output is correct
29 Correct 215 ms 728 KB Output is correct
30 Correct 400 ms 24000 KB Output is correct
31 Correct 142 ms 680 KB Output is correct
32 Correct 389 ms 20996 KB Output is correct
33 Correct 191 ms 812 KB Output is correct
34 Correct 220 ms 764 KB Output is correct
35 Correct 200 ms 880 KB Output is correct
36 Correct 327 ms 25072 KB Output is correct
37 Correct 329 ms 25128 KB Output is correct
38 Correct 506 ms 49528 KB Output is correct
39 Correct 469 ms 46996 KB Output is correct
40 Correct 246 ms 936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 272 ms 768 KB Output is correct
2 Correct 0 ms 400 KB Output is correct
3 Correct 305 ms 888 KB Output is correct
4 Correct 422 ms 10156 KB Output is correct
5 Correct 17 ms 752 KB Output is correct
6 Correct 277 ms 2604 KB Output is correct
7 Correct 0 ms 400 KB Output is correct
8 Correct 294 ms 992 KB Output is correct
9 Correct 318 ms 872 KB Output is correct
10 Correct 462 ms 27664 KB Output is correct
11 Correct 412 ms 24492 KB Output is correct
12 Correct 70 ms 652 KB Output is correct
13 Correct 449 ms 24764 KB Output is correct
14 Correct 319 ms 956 KB Output is correct
15 Correct 0 ms 400 KB Output is correct
16 Correct 295 ms 876 KB Output is correct
17 Correct 333 ms 844 KB Output is correct
18 Correct 315 ms 980 KB Output is correct
19 Correct 277 ms 844 KB Output is correct
20 Correct 267 ms 896 KB Output is correct
21 Correct 302 ms 928 KB Output is correct
22 Correct 297 ms 1032 KB Output is correct
23 Correct 299 ms 812 KB Output is correct
24 Correct 156 ms 668 KB Output is correct
25 Correct 120 ms 672 KB Output is correct
26 Correct 234 ms 13316 KB Output is correct
27 Correct 106 ms 728 KB Output is correct
28 Correct 231 ms 9964 KB Output is correct
29 Correct 147 ms 692 KB Output is correct
30 Correct 139 ms 872 KB Output is correct
31 Correct 151 ms 752 KB Output is correct
32 Correct 238 ms 19628 KB Output is correct
33 Correct 227 ms 19436 KB Output is correct
34 Correct 391 ms 38220 KB Output is correct
35 Correct 318 ms 36020 KB Output is correct
36 Correct 118 ms 860 KB Output is correct
37 Correct 1 ms 400 KB Output is correct
38 Correct 175 ms 708 KB Output is correct
39 Correct 182 ms 828 KB Output is correct
40 Correct 197 ms 652 KB Output is correct
41 Correct 255 ms 10136 KB Output is correct
42 Correct 185 ms 996 KB Output is correct
43 Correct 282 ms 10192 KB Output is correct
44 Correct 111 ms 820 KB Output is correct
45 Correct 198 ms 796 KB Output is correct
46 Correct 330 ms 22332 KB Output is correct
47 Correct 297 ms 22308 KB Output is correct
48 Correct 465 ms 44092 KB Output is correct
49 Correct 386 ms 40504 KB Output is correct
50 Correct 158 ms 980 KB Output is correct
51 Correct 200 ms 760 KB Output is correct
52 Correct 215 ms 728 KB Output is correct
53 Correct 400 ms 24000 KB Output is correct
54 Correct 142 ms 680 KB Output is correct
55 Correct 389 ms 20996 KB Output is correct
56 Correct 191 ms 812 KB Output is correct
57 Correct 220 ms 764 KB Output is correct
58 Correct 200 ms 880 KB Output is correct
59 Correct 327 ms 25072 KB Output is correct
60 Correct 329 ms 25128 KB Output is correct
61 Correct 506 ms 49528 KB Output is correct
62 Correct 469 ms 46996 KB Output is correct
63 Correct 246 ms 936 KB Output is correct
64 Correct 331 ms 1136 KB Output is correct
65 Runtime error 284 ms 13312 KB Execution killed with signal 13
66 Halted 0 ms 0 KB -