본문 바로가기

백준

C++[백준]10217번 KCM Travel

https://www.acmicpc.net/problem/10217

 

10217번: KCM Travel

각고의 노력 끝에 찬민이는 2014 Google Code Jam World Finals에 진출하게 되었다. 구글에서 온 초대장을 받고 기뻐했던 것도 잠시, 찬찬히 읽어보던 찬민이는 중요한 사실을 알아차렸다. 최근의 대세

www.acmicpc.net

 

솔루션


이전에 도로포장을 풀고 이번 문제를 풀어서 그런가, 접근 법은 역시나 똑같다.

 

2차원 배열을 선언하는데 [걸린 비용][공항] = 시간 으로 값을 채워주면 풀린다.

 

(풀이법은 도로 포장을 풀어보자.)

 

 

 

코드


#define INF 2100000000
#include <iostream>
#include <vector>
#include<queue>
using namespace std;

struct edge {
	int dest, cost, t;
};
struct pa {
	int cost, time, dest;
	
	bool operator<(const struct pa& t) const {
		if (cost == t.cost) {
			if (time == t.time) {
				return dest > t.dest;
			}
			return time > t.time;
		}
		return cost > t.cost;
	}
};

vector<struct edge> V[102];

int value[10002][102];
int N, M, K;


void dijkstra(void) {
	for (int i = 2; i <= N; i++) {
		for (int j = 0; j <= K; j++) {
			value[j][i] = INF;
		}
	}

	priority_queue<struct pa> pq;
	pq.push({ 0,0,1 });

	while (!pq.empty()) {
		int c = pq.top().cost;
		int t = pq.top().time;
		int str = pq.top().dest;
		pq.pop();

		if (value[c][str] < t) continue;

		for (int i = 0; i < V[str].size(); i++) {
			int next = V[str][i].dest;
			int time = V[str][i].t + t;
			int cost = V[str][i].cost + c;

			if (cost > K || value[cost][next] <= time) continue;
			pq.push({ cost,time, next });
			value[cost][next] = time;
		}
	}
	int minValue = INF;
	for (int i = 0; i <= K; i++) {
		minValue = min(minValue, value[i][N]);
	}

	if (minValue == INF) {
		cout << "Poor KCM\n";
	}
	else cout << minValue << '\n';

	for (int i = 0; i <= N; i++) {
		V[i].clear();
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int tc;
	cin >> tc;
	while (tc--) {
		cin >> N >> K >> M;

		for (int i = 0; i < M; i++) {
			int str, dest, c, d;
			cin >> str >> dest >> c >> d;
			V[str].push_back({ dest,c,d });
		}

		dijkstra();
		
	}
}

'백준' 카테고리의 다른 글

C++[백준]17143번 낚시왕  (2) 2023.01.04
C++[백준]10757번 큰 수 A + B  (0) 2023.01.03
C++[백준]1162번 도로포장  (0) 2023.01.03
C++[백준] 5719 거의 최단 경로  (0) 2022.12.31
C++[백준]2211번 네트워크 복구  (0) 2022.12.17