Submission #526704

# Submission time Handle Problem Language Result Execution time Memory
526704 2022-02-16T02:22:45 Z siewjh Autobus (COCI22_autobus) C++17
0 / 70
2 ms 3276 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int inf = 1'000'000'007;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int nodes, edges; cin >> nodes >> edges;
	vector<vector<int>> adjmat(nodes, vector<int>(nodes, inf));
	vector<vector<pair<int, int>>> adjlist(edges);
	for (int i = 0; i < edges; i++) {
		int a, b, t; cin >> a >> b >> t;
		adjmat[a][b] = min(adjmat[a][b], t);
	}
	for (int i = 1; i <= nodes; i++)
		for (int j = 1; j <= nodes; j++)
			if (adjmat[i][j] != inf)
				adjlist[i].push_back({ j,adjmat[i][j] });
	int maxbus, queries; cin >> maxbus >> queries;
	maxbus = min(maxbus, nodes - 1);
	int dist[71][71][71];
	for (int i = 0; i < 71; i++)
		for (int j = 0; j < 71; j++)
			for (int k = 0; k < 71; k++)
				dist[i][j][k] = inf;
	for (int i = 1; i <= nodes; i++) {
		dist[i][i][0] = 0;
		priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>, greater<pair<pair<int, int>, int>>> pq;
		pq.push({ {0,i}, 0 });
		while (!pq.empty()) {
			int d = pq.top().first.first, x = pq.top().first.second, bus = pq.top().second;
			pq.pop();
			if (d != dist[i][x][bus] || bus == maxbus) continue;
			for (auto nxt : adjlist[x]) {
				int nn = nxt.first, nd = nxt.second;
				if (d + nd < dist[i][nn][bus + 1]) {
					dist[i][nn][bus + 1] = d + nd;
					pq.push({ {dist[i][nn][bus + 1], nn}, bus + 1 });
				}
			}
		}
	}
	for (int i = 0; i < queries; i++) {
		int a, b; cin >> a >> b;
		int ans = inf;
		for (int j = 0; j <= maxbus; j++) ans = min(ans, dist[a][b][j]);
		cout << ((ans == inf) ? -1 : ans) << '\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 3276 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 3276 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 3276 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 3276 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -