답안 #472565

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
472565 2021-09-13T18:06:00 Z Hamed5001 Toll (BOI17_toll) C++14
0 / 100
4 ms 1484 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll mxN = 5e4, OO = 2e16;
ll preprocess[mxN];
bool vis[mxN];
ll COST[mxN];
vector<pair<ll, ll>> adj[mxN];
ll K, N, M, O;

void pre(ll st) {
	priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
	pq.push({0, st});
	preprocess[st] = 0;

	while(!pq.empty()) {
		ll node = pq.top().second;
		ll cost = pq.top().first;
		pq.pop();

		for (auto c : adj[node]) {
			if (cost + c.first < preprocess[c.second]) {
				preprocess[c.second] = cost + c.first;
				pq.push({cost + c.first, c.second});	
			}
		}
	}
}

ll dijkstra(ll st, ll en) {
	for (int i = 0; i < N; i++)
		COST[i] = OO;
	priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
	pq.push({0, st});
	COST[st] = 0;

	while(!pq.empty()) {
		ll node = pq.top().second;
		ll cost = pq.top().first;
		if (node == en) return cost;

		pq.pop();
		for (auto c : adj[node]) {
			if (cost + c.first < COST[c.second]) {
				COST[c.second] = cost + c.first;
				pq.push({cost + c.first, c.second});	
			}
		}
	}
	return -1;
}

void solve() {
	cin >> K >> N >> M >> O;

	for (int i = 0; i < N; i++)
		preprocess[i] = OO;
	
	for (ll i = 0; i < M; i++) {
		ll u, v, t;
		cin >> u >> v >> t;
		adj[u].push_back({t, v});
	}
	pre(0);

	while(O--) {
		ll a, b;
		cin >> a >> b;
		if (a == 0) {
			if (preprocess[b] == OO) preprocess[b] = -1;
			cout << preprocess[b] << endl;
		}
		else {
			cout << dijkstra(a, b) << endl;
		}
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	solve();
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:83:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |  freopen("input.txt", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:84:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |  freopen("output.txt", "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 1484 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1484 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1484 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1484 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 1484 KB Output isn't correct
2 Halted 0 ms 0 KB -