답안 #714587

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
714587 2023-03-25T06:09:17 Z amirhoseinfar1385 Toll (BOI17_toll) C++17
100 / 100
264 ms 8100 KB
#include<bits/stdc++.h>
using namespace std;
int k,n,m,o;
const int maxn=50000+10,sq=240;
vector<pair<int,int>>adj[maxn],revadj[maxn];
int dp[sq][5][5],inf=1e8*3;

void pre(){
	for(int i=0;i+sq<maxn;i+=sq){
		for(int j=0;j<k;j++){
			vector<int>allv(sq,inf);
			allv[j]=0;
			for(int h=i;h<i+sq;h++){
				for(auto x:revadj[h]){
					if(x.first>=i){
						allv[h-i]=min(allv[x.first-i]+x.second,allv[h-i]);
					}
				}
			}
			for(int h=0;h<k;h++){
				dp[i/sq][j][h]=allv[sq-(k-h)];
			}
		}
	}
}


void solve(int a,int b){
	vector<int>allv(sq+2,inf);
	if(b-a<=sq){
		allv[0]=0;
		for(int i=1;i<=b-a;i++){
			for(auto x:revadj[i+a]){
				if(x.first>=a){
					allv[i]=min(allv[i],allv[x.first-a]+x.second);
				}
			}
		}
		if(allv[b-a]==inf){
			cout<<-1<<"\n";
			return ;
		}
		cout<<allv[b-a]<<"\n";
		return ;
	}
	vector<int>revallv(sq+2,inf);
	int l=a-(a%sq);
	allv[a-l]=0;
	for(int i=a-l+1;i<sq;i++){
		for(auto x:revadj[l+i]){
			if(x.first>=l){
				allv[i]=min(allv[i],allv[x.first-l]+x.second);
			}
		}
	}
	l=b-(b%sq);
	revallv[b-l]=0;
	for(int i=b-l-1;i>=0;i--){
		for(auto x:adj[l+i]){
			if(x.first<=b){
				revallv[i]=min(revallv[i],revallv[x.first-l]+x.second);
			}
		}
	}
	vector<int>now(k),last(k);
	for(int i=0;i<k;i++){
		now[i]=allv[sq-(k-i)];
		last[i]=revallv[i];
	}
	a-=(a%sq);
	a+=sq;
	while(a+sq<=b){
		//cout<<a<<endl;
		vector<int>fake(k,inf);
		for(int i=0;i<k;i++){
			for(auto x:adj[a-(k-i)]){
				if(x.first>=a){
					for(int j=0;j<k;j++){
						fake[j]=min(fake[j],dp[a/sq][x.first-a][j]+x.second+now[i]);
					}
				}
			}
		}
		swap(fake,now);
		fake.clear();
		a+=sq;
	//	cout<<a<<endl;
	}
	int res=inf;
	for(int i=0;i<k;i++){
		//cout<<i<<" "<<now[i]<<" "<<last[i]<<"\n";
		for(auto x:adj[a-(k-i)]){
			res=min(res,x.second+now[i]+last[x.first-a]);
		}
	}
	if(res==inf){
		cout<<-1<<"\n";
		return;
	}
	cout<<res<<"\n";
	return ;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);	
	cin>>k>>n>>m>>o;
	for(int i=0;i<m;i++){
		int a,b,w;
		cin>>a>>b>>w;
		adj[a].push_back(make_pair(b,w));
		revadj[b].push_back(make_pair(a,w));
	}
	pre();
	for(int i=0;i<o;i++){
		int a,b;
		cin>>a>>b;
		solve(a,b);
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 5884 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 3 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 58 ms 5812 KB Output is correct
9 Correct 59 ms 5680 KB Output is correct
10 Correct 22 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 5852 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 17 ms 2772 KB Output is correct
8 Correct 20 ms 2840 KB Output is correct
9 Correct 64 ms 5832 KB Output is correct
10 Correct 175 ms 7628 KB Output is correct
11 Correct 117 ms 5868 KB Output is correct
12 Correct 119 ms 5744 KB Output is correct
13 Correct 123 ms 8100 KB Output is correct
14 Correct 72 ms 5916 KB Output is correct
15 Correct 75 ms 5308 KB Output is correct
16 Correct 71 ms 5188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 2 ms 2656 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 2824 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 20 ms 5716 KB Output is correct
11 Correct 38 ms 5696 KB Output is correct
12 Correct 54 ms 7484 KB Output is correct
13 Correct 64 ms 8028 KB Output is correct
14 Correct 47 ms 6748 KB Output is correct
15 Correct 34 ms 5228 KB Output is correct
16 Correct 29 ms 5248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 2 ms 2656 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 2824 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 20 ms 5716 KB Output is correct
11 Correct 38 ms 5696 KB Output is correct
12 Correct 54 ms 7484 KB Output is correct
13 Correct 64 ms 8028 KB Output is correct
14 Correct 47 ms 6748 KB Output is correct
15 Correct 34 ms 5228 KB Output is correct
16 Correct 29 ms 5248 KB Output is correct
17 Correct 55 ms 5740 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 5 ms 2772 KB Output is correct
24 Correct 6 ms 2772 KB Output is correct
25 Correct 11 ms 2772 KB Output is correct
26 Correct 10 ms 2772 KB Output is correct
27 Correct 29 ms 5824 KB Output is correct
28 Correct 89 ms 7632 KB Output is correct
29 Correct 105 ms 8012 KB Output is correct
30 Correct 80 ms 6880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 5884 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 3 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 58 ms 5812 KB Output is correct
9 Correct 59 ms 5680 KB Output is correct
10 Correct 22 ms 2644 KB Output is correct
11 Correct 111 ms 5852 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 17 ms 2772 KB Output is correct
18 Correct 20 ms 2840 KB Output is correct
19 Correct 64 ms 5832 KB Output is correct
20 Correct 175 ms 7628 KB Output is correct
21 Correct 117 ms 5868 KB Output is correct
22 Correct 119 ms 5744 KB Output is correct
23 Correct 123 ms 8100 KB Output is correct
24 Correct 72 ms 5916 KB Output is correct
25 Correct 75 ms 5308 KB Output is correct
26 Correct 71 ms 5188 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 3 ms 2644 KB Output is correct
29 Correct 2 ms 2656 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 3 ms 2644 KB Output is correct
34 Correct 4 ms 2824 KB Output is correct
35 Correct 4 ms 2772 KB Output is correct
36 Correct 20 ms 5716 KB Output is correct
37 Correct 38 ms 5696 KB Output is correct
38 Correct 54 ms 7484 KB Output is correct
39 Correct 64 ms 8028 KB Output is correct
40 Correct 47 ms 6748 KB Output is correct
41 Correct 34 ms 5228 KB Output is correct
42 Correct 29 ms 5248 KB Output is correct
43 Correct 55 ms 5740 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 5 ms 2772 KB Output is correct
50 Correct 6 ms 2772 KB Output is correct
51 Correct 11 ms 2772 KB Output is correct
52 Correct 10 ms 2772 KB Output is correct
53 Correct 29 ms 5824 KB Output is correct
54 Correct 89 ms 7632 KB Output is correct
55 Correct 105 ms 8012 KB Output is correct
56 Correct 80 ms 6880 KB Output is correct
57 Correct 264 ms 8100 KB Output is correct
58 Correct 50 ms 5728 KB Output is correct
59 Correct 94 ms 5780 KB Output is correct