답안 #714773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
714773 2023-03-25T09:11:19 Z ajab_01 Toll (BOI17_toll) C++17
17 / 100
79 ms 78616 KB
#include<bits/stdc++.h>
using namespace std;

const int INF = 1e9;
const int N = 5e4 + 1;
const int O = 3e3 + 1;
vector<pair<int , int> > g[N] , query;
priority_queue<pair<int , int> > pq;
vector<int> vec;
int dp[O][O] , ind[N] , val[N] , pre[N] , pr[N] , k , n , m , o;
bool mark[N] , vis[N] , ch = 1;

void dijkstra(){
	val[0] = 0;
	pq.push({0 , 0});
	while(!pq.empty()){
		int v = pq.top().second;
		pq.pop();
		if(vis[v]) continue;
		vis[v] = 1;
		for(auto i : g[v]){
			int u = i.first , w = i.second;
			if(val[u] > val[v] + w){
				val[u] = val[v] + w;
				pq.push({-val[u] , u});
			}
		}
	}
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	memset(pr , -1 , sizeof(pr));
	fill(val , val + N , INF);
	cin >> k >> n >> m >> o;
	for(int i = 0 ; i < m ; i++){
		int u , v , w;
		cin >> u >> v >> w;
		g[u].push_back({v , w});
	}

	for(int i = 0 ; i < o ; i++){
		int a , b;
		cin >> a >> b;
		query.push_back({a , b});
		if(a) ch = 0;
		if(mark[b]) continue;
		vec.push_back(b);
		ind[b] = (int)vec.size() - 1;
		mark[b] = 1;
	}

	if(k == 1){
		int bef = 0;
		for(int i = 0 ; i < n ; i++){
			if(i == bef)
				pre[i] = 0;
			else
				pre[i] = pre[i - 1] + g[i - 1][0].second;
			pr[i] = bef;
			if(!g[i].size()){
				while(!g[i].size())i++;
				bef = i;
				i--;
			}
		}
		for(auto i : query){
			if(pr[i.first] == -1 || pr[i.second] == -1 || pr[i.first] != pr[i.second] || i.second < i.first)
				cout << -1 << '\n';
			else
				cout << pre[i.second] - pre[i.first] << '\n';
		}
		return 0;
	}

	if(ch){
		dijkstra();
		for(auto i : query){
			if(val[i.second] == INF)
				cout << -1 << '\n';
			else
				cout << val[i.second] << '\n';
		}
		return 0;
	}

	if(o <= 3000){
		memset(dp , -1 , sizeof(dp));
		int sz = (int)vec.size();
		for(int i = 0 ; i < sz ; i++)
			dp[vec[i]][i] = 0;

		for(int v = n - 1 ; v >= 0 ; v--){
			for(int j = 0 ; j < sz ; j++){
				for(auto u : g[v]){
					if(dp[u.first][j] == -1) continue;
					if(dp[v][j] == -1)
						dp[v][j] = dp[u.first][j] + u.second;
					else 
						dp[v][j] = min(dp[v][j] , dp[u.first][j] + u.second);
				}
			}
		}

		for(auto i : query){
			int a = i.first , b = i.second;
			if(dp[a][ind[b]] == -1)
				cout << -1 << '\n';
			else
				cout << dp[a][ind[b]] << '\n';
		}
		return 0;
	}
	cout << "jalebe" << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 4180 KB Output is correct
2 Correct 1 ms 1876 KB Output is correct
3 Correct 1 ms 1876 KB Output is correct
4 Correct 1 ms 1876 KB Output is correct
5 Correct 2 ms 1876 KB Output is correct
6 Correct 2 ms 1876 KB Output is correct
7 Correct 2 ms 1876 KB Output is correct
8 Correct 21 ms 4028 KB Output is correct
9 Correct 18 ms 4052 KB Output is correct
10 Correct 3 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 3928 KB Output is correct
2 Correct 1 ms 1876 KB Output is correct
3 Correct 1 ms 1876 KB Output is correct
4 Correct 1 ms 1876 KB Output is correct
5 Correct 1 ms 1876 KB Output is correct
6 Correct 1 ms 1876 KB Output is correct
7 Correct 3 ms 2132 KB Output is correct
8 Correct 4 ms 2132 KB Output is correct
9 Correct 21 ms 4136 KB Output is correct
10 Correct 65 ms 4864 KB Output is correct
11 Correct 37 ms 3964 KB Output is correct
12 Correct 41 ms 3784 KB Output is correct
13 Correct 55 ms 4856 KB Output is correct
14 Correct 30 ms 3776 KB Output is correct
15 Correct 27 ms 3404 KB Output is correct
16 Correct 27 ms 3404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1876 KB Output is correct
2 Correct 16 ms 37076 KB Output is correct
3 Correct 16 ms 37052 KB Output is correct
4 Correct 20 ms 37040 KB Output is correct
5 Correct 16 ms 37076 KB Output is correct
6 Correct 1 ms 1876 KB Output is correct
7 Correct 17 ms 37076 KB Output is correct
8 Correct 18 ms 37204 KB Output is correct
9 Correct 18 ms 37204 KB Output is correct
10 Correct 15 ms 3852 KB Output is correct
11 Runtime error 79 ms 78616 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1876 KB Output is correct
2 Correct 16 ms 37076 KB Output is correct
3 Correct 16 ms 37052 KB Output is correct
4 Correct 20 ms 37040 KB Output is correct
5 Correct 16 ms 37076 KB Output is correct
6 Correct 1 ms 1876 KB Output is correct
7 Correct 17 ms 37076 KB Output is correct
8 Correct 18 ms 37204 KB Output is correct
9 Correct 18 ms 37204 KB Output is correct
10 Correct 15 ms 3852 KB Output is correct
11 Runtime error 79 ms 78616 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 4180 KB Output is correct
2 Correct 1 ms 1876 KB Output is correct
3 Correct 1 ms 1876 KB Output is correct
4 Correct 1 ms 1876 KB Output is correct
5 Correct 2 ms 1876 KB Output is correct
6 Correct 2 ms 1876 KB Output is correct
7 Correct 2 ms 1876 KB Output is correct
8 Correct 21 ms 4028 KB Output is correct
9 Correct 18 ms 4052 KB Output is correct
10 Correct 3 ms 2388 KB Output is correct
11 Correct 47 ms 3928 KB Output is correct
12 Correct 1 ms 1876 KB Output is correct
13 Correct 1 ms 1876 KB Output is correct
14 Correct 1 ms 1876 KB Output is correct
15 Correct 1 ms 1876 KB Output is correct
16 Correct 1 ms 1876 KB Output is correct
17 Correct 3 ms 2132 KB Output is correct
18 Correct 4 ms 2132 KB Output is correct
19 Correct 21 ms 4136 KB Output is correct
20 Correct 65 ms 4864 KB Output is correct
21 Correct 37 ms 3964 KB Output is correct
22 Correct 41 ms 3784 KB Output is correct
23 Correct 55 ms 4856 KB Output is correct
24 Correct 30 ms 3776 KB Output is correct
25 Correct 27 ms 3404 KB Output is correct
26 Correct 27 ms 3404 KB Output is correct
27 Correct 2 ms 1876 KB Output is correct
28 Correct 16 ms 37076 KB Output is correct
29 Correct 16 ms 37052 KB Output is correct
30 Correct 20 ms 37040 KB Output is correct
31 Correct 16 ms 37076 KB Output is correct
32 Correct 1 ms 1876 KB Output is correct
33 Correct 17 ms 37076 KB Output is correct
34 Correct 18 ms 37204 KB Output is correct
35 Correct 18 ms 37204 KB Output is correct
36 Correct 15 ms 3852 KB Output is correct
37 Runtime error 79 ms 78616 KB Execution killed with signal 11
38 Halted 0 ms 0 KB -