Submission #197156

# Submission time Handle Problem Language Result Execution time Memory
197156 2020-01-19T11:08:45 Z dndhk Toll (BOI17_toll) C++14
7 / 100
102 ms 32184 KB
#include <bits/stdc++.h>

#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1000000000;
const int MAX_N = 50000;
const int MAX_K = 5;

int N, Q, M, K;
vector<pii> gp[MAX_N+1];
ll dp[MAX_K+1][MAX_K+1], ndp[MAX_K+1][MAX_K+1];

struct SEG{
	struct NODE{
		int l, r;
		ll d[MAX_K+1][MAX_K+1];
	};
	NODE seg[MAX_N*2+1];
	int cnt = 0;
	int SZ;
	void add(){
		seg[cnt].l = seg[cnt].r = -1;
		for(int i=0 ;i<K; i++){
			for(int j=0; j<K; j++){
				seg[cnt].d[i][j] = INF;
			}
		}
		cnt++;
	}
	void Init(int x){
		SZ = x;
		add();
		init(0, 1, SZ);
	}
	void init(int idx, int s, int e){
		if(s==e){
			for(int j=0; j<K; j++){
				int i = (s-1)*K + j;
				for(pii p : gp[i]){
					seg[idx].d[j][p.first%K] = p.second;
				}
			}
			// cout<<s<<" "<<e<<endl;
			// for(int j=0; j<K; j++){
			// 	for(int k=0; k<K; k++){
			// 		cout<<seg[idx].d[j][k]<<" ";
			// 	}cout<<endl;
			// }
			return;
		}
		seg[idx].l = cnt; add();
		seg[idx].r = cnt; add();
		init(seg[idx].l, s, (s+e)/2); init(seg[idx].r, (s+e)/2+1, e);
		for(int i=0; i<K; i++){
			for(int j=0; j<K; j++){
				for(int k=0; k<K; k++){
					seg[idx].d[i][k] = min(seg[idx].d[i][k], seg[seg[idx].l].d[i][j] + seg[seg[idx].r].d[j][k]);
				}
			}
		}
		// cout<<s<<" "<<e<<endl;
		// for(int j=0; j<K; j++){
		// 	for(int k=0; k<K; k++){
		// 		cout<<seg[idx].d[j][k]<<" ";
		// 	}cout<<endl;
		// }
	}
	void Calc(int x, int y){
		calc(0, 1, SZ, x, y);
	}
	void calc(int idx, int s, int e, int x, int y){
		if(x<=s && e<=y){
			for(int i=0; i<K; i++){
				for(int j=0; j<K; j++){
					ndp[i][j] = INF;
				}
			}
			for(int i=0; i<K; i++){
				for(int j=0; j<K; j++){
					for(int k=0; k<K; k++){
						ndp[i][k] = min(ndp[i][k], dp[i][j]+seg[idx].d[j][k]);
					}
				}
			}
			for(int i=0; i<K; i++){
				for(int j=0; j<K; j++){
					dp[i][j] = ndp[i][j];
				}
			}
			return;
		}
		if(x>e || y<s)	return;
		calc(seg[idx].l, s, (s+e)/2, x, y);
		calc(seg[idx].r, (s+e)/2+1, e, x, y);
	}
}Seg;

int main(){
	scanf("%d%d%d%d", &K, &N, &M, &Q);
	for(int i=1; i<=M; i++){
		int a, b, t;
		scanf("%d%d%d", &a, &b, &t);
		gp[a].pb({b, t});
	}
	Seg.Init(N/K);
	for(int i=1; i<=Q; i++){
		int a, b; scanf("%d%d", &a, &b);
		if(a==b){
			printf("0\n");
			continue;
		}
		if(a/K>=b/K){
			printf("-1\n");
			continue;
		}
		int ga = a/K, gb = b/K;
		if(gb==ga+1){
			bool tf = false;
			for(pii p : gp[a]){
				if(p.first==b){
					printf("%d\n", p.second);
					tf = true;
					break;
				}
			}
			if(!tf){
				printf("-1\n");
			}
		}else{
			for(int i=0; i<K; i++){
				for(int j=0; j<K; j++)	dp[i][j] = 0LL;
			}
			Seg.Calc(ga+1, gb);
			if(dp[a%K][b%K]==INF){
				printf("-1\n");
			}else{
				printf("%lld\n", dp[a%K][b%K]);
			}
		}
	}
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:105:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &K, &N, &M, &Q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:108:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &a, &b, &t);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:113:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b; scanf("%d%d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 74 ms 32120 KB Output is correct
2 Correct 3 ms 1528 KB Output is correct
3 Correct 3 ms 1528 KB Output is correct
4 Correct 3 ms 1528 KB Output is correct
5 Correct 4 ms 1912 KB Output is correct
6 Correct 4 ms 2168 KB Output is correct
7 Correct 5 ms 2172 KB Output is correct
8 Correct 72 ms 32184 KB Output is correct
9 Correct 71 ms 31992 KB Output is correct
10 Correct 42 ms 30456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 17656 KB Output is correct
2 Correct 4 ms 1528 KB Output is correct
3 Correct 4 ms 1528 KB Output is correct
4 Incorrect 4 ms 1528 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1500 KB Output is correct
2 Incorrect 0 ms 1528 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1500 KB Output is correct
2 Incorrect 0 ms 1528 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 74 ms 32120 KB Output is correct
2 Correct 3 ms 1528 KB Output is correct
3 Correct 3 ms 1528 KB Output is correct
4 Correct 3 ms 1528 KB Output is correct
5 Correct 4 ms 1912 KB Output is correct
6 Correct 4 ms 2168 KB Output is correct
7 Correct 5 ms 2172 KB Output is correct
8 Correct 72 ms 32184 KB Output is correct
9 Correct 71 ms 31992 KB Output is correct
10 Correct 42 ms 30456 KB Output is correct
11 Correct 102 ms 17656 KB Output is correct
12 Correct 4 ms 1528 KB Output is correct
13 Correct 4 ms 1528 KB Output is correct
14 Incorrect 4 ms 1528 KB Output isn't correct
15 Halted 0 ms 0 KB -