답안 #393471

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
393471 2021-04-23T14:30:22 Z Nordway Toll (BOI17_toll) C++17
100 / 100
158 ms 13248 KB
#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define mp make_pair
#define sz(v) v.size()
#define up_b upper_bound
#define low_b lower_bound
#define all(v) v.begin(),v.end()

using namespace std;

typedef long long ll;
typedef long double ld;

const int N=5e4+11;
const ll mod=1e9+7;
const int inf=1e9;

int k,n,m,o;

int lef[5][N],rig[5][N];
vector<pair<int,int>>g[N][2];
int ans[N];
int A[N],B[N];

void go(int l,int r,vector<int>orders){
	if(l==r){
		for(int i:orders){
			ans[i]=inf;
		}
		return ;
	}
	int mid=(l+r)/2;
	for(int x=0;x<k;x++){
		for(int i=l*k;i<(r+1)*k;i++){
			lef[x][i]=inf;
			rig[x][i]=inf;
		}
		lef[x][mid*k+x]=0;
		rig[x][mid*k+x]=0;
		for(int i=(mid+1)*k;i<(r+1)*k;i++){
			for(pair<int,int>p:g[i][1]){
				rig[x][i]=min(rig[x][i],rig[x][p.x]+p.y);
			}
		}
		for(int i=mid*k-1;i>=l*k;i--){
			for(pair<int,int>p:g[i][0]){
				lef[x][i]=min(lef[x][i],lef[x][p.x]+p.y);
			}
		}
	}
	vector<int>todo[2];
	for(int i:orders){
		int a=A[i],b=B[i];
		if(a/k<=mid&&mid<b/k){
			ans[i]=inf;
			for(int x=0;x<k;x++){
				ans[i]=min(ans[i],lef[x][a]+rig[x][b]);
			}
			continue;
		}
		todo[a/k>mid].pb(i);
	}
	go(l,mid,todo[0]);
	go(mid+1,r,todo[1]);
}

void solve() {
	cin>>k>>n>>m>>o;
	for(int i=1;i<=m;i++){
		int a,b,t;
		cin>>a>>b>>t;
		g[a][0].pb(mp(b,t));
		g[b][1].pb(mp(a,t));
	}
	vector<int>orders;
	for(int i=0;i<o;i++){
		cin>>A[i]>>B[i];
		orders.pb(i);
	}
	go(0,(n-1)/k,orders);
	for(int i=0;i<o;i++){
		if(ans[i]==inf)ans[i]=-1;
		cout<<ans[i]<<"\n";
	}

}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--){
		solve();
		cout<<"\n";
	}
}

# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 7396 KB Output is correct
2 Correct 3 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 3 ms 2764 KB Output is correct
6 Correct 3 ms 2776 KB Output is correct
7 Correct 3 ms 2764 KB Output is correct
8 Correct 36 ms 7320 KB Output is correct
9 Correct 35 ms 7196 KB Output is correct
10 Correct 9 ms 3460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 8448 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 6 ms 3148 KB Output is correct
8 Correct 7 ms 3148 KB Output is correct
9 Correct 39 ms 7268 KB Output is correct
10 Correct 98 ms 11304 KB Output is correct
11 Correct 73 ms 8636 KB Output is correct
12 Correct 63 ms 8340 KB Output is correct
13 Correct 158 ms 11668 KB Output is correct
14 Correct 65 ms 8184 KB Output is correct
15 Correct 56 ms 7816 KB Output is correct
16 Correct 58 ms 7816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2588 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2764 KB Output is correct
7 Correct 4 ms 2764 KB Output is correct
8 Correct 5 ms 3020 KB Output is correct
9 Correct 4 ms 2892 KB Output is correct
10 Correct 35 ms 6940 KB Output is correct
11 Correct 63 ms 8080 KB Output is correct
12 Correct 104 ms 11032 KB Output is correct
13 Correct 116 ms 11588 KB Output is correct
14 Correct 85 ms 9796 KB Output is correct
15 Correct 72 ms 7672 KB Output is correct
16 Correct 57 ms 7672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2588 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2764 KB Output is correct
7 Correct 4 ms 2764 KB Output is correct
8 Correct 5 ms 3020 KB Output is correct
9 Correct 4 ms 2892 KB Output is correct
10 Correct 35 ms 6940 KB Output is correct
11 Correct 63 ms 8080 KB Output is correct
12 Correct 104 ms 11032 KB Output is correct
13 Correct 116 ms 11588 KB Output is correct
14 Correct 85 ms 9796 KB Output is correct
15 Correct 72 ms 7672 KB Output is correct
16 Correct 57 ms 7672 KB Output is correct
17 Correct 73 ms 8196 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 4 ms 2820 KB Output is correct
24 Correct 4 ms 2892 KB Output is correct
25 Correct 5 ms 3020 KB Output is correct
26 Correct 6 ms 2892 KB Output is correct
27 Correct 41 ms 7044 KB Output is correct
28 Correct 111 ms 11048 KB Output is correct
29 Correct 114 ms 11784 KB Output is correct
30 Correct 81 ms 9924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 7396 KB Output is correct
2 Correct 3 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 3 ms 2764 KB Output is correct
6 Correct 3 ms 2776 KB Output is correct
7 Correct 3 ms 2764 KB Output is correct
8 Correct 36 ms 7320 KB Output is correct
9 Correct 35 ms 7196 KB Output is correct
10 Correct 9 ms 3460 KB Output is correct
11 Correct 71 ms 8448 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 6 ms 3148 KB Output is correct
18 Correct 7 ms 3148 KB Output is correct
19 Correct 39 ms 7268 KB Output is correct
20 Correct 98 ms 11304 KB Output is correct
21 Correct 73 ms 8636 KB Output is correct
22 Correct 63 ms 8340 KB Output is correct
23 Correct 158 ms 11668 KB Output is correct
24 Correct 65 ms 8184 KB Output is correct
25 Correct 56 ms 7816 KB Output is correct
26 Correct 58 ms 7816 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2588 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2764 KB Output is correct
33 Correct 4 ms 2764 KB Output is correct
34 Correct 5 ms 3020 KB Output is correct
35 Correct 4 ms 2892 KB Output is correct
36 Correct 35 ms 6940 KB Output is correct
37 Correct 63 ms 8080 KB Output is correct
38 Correct 104 ms 11032 KB Output is correct
39 Correct 116 ms 11588 KB Output is correct
40 Correct 85 ms 9796 KB Output is correct
41 Correct 72 ms 7672 KB Output is correct
42 Correct 57 ms 7672 KB Output is correct
43 Correct 73 ms 8196 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 2 ms 2636 KB Output is correct
49 Correct 4 ms 2820 KB Output is correct
50 Correct 4 ms 2892 KB Output is correct
51 Correct 5 ms 3020 KB Output is correct
52 Correct 6 ms 2892 KB Output is correct
53 Correct 41 ms 7044 KB Output is correct
54 Correct 111 ms 11048 KB Output is correct
55 Correct 114 ms 11784 KB Output is correct
56 Correct 81 ms 9924 KB Output is correct
57 Correct 154 ms 13248 KB Output is correct
58 Correct 36 ms 7376 KB Output is correct
59 Correct 70 ms 8592 KB Output is correct