다익스트라 문제집 (1)

 
1238 파티
N개의 숫자로 구분된 각각의 마을에 한 명의 학생이 살고 있다.
어느 날 이 N명의 학생이 X (1 ≤ X ≤ N)번 마을에 모여서 파티를 벌이기로 했다. 이 마을 사이에는 총 M개의 단방향 도로들이 있고 i번째 길을 지나는데 Ti(1 ≤ Ti ≤ 100)의 시간을 소비한다.
각각의 학생들은 파티에 참석하기 위해 걸어가서 다시 그들의 마을로 돌아와야 한다. 하지만 이 학생들은 워낙 게을러서 최단 시간에 오고 가기를 원한다.
이 도로들은 단방향이기 때문에 아마 그들이 오고 가는 길이 다를지도 모른다. N명의 학생들 중 오고 가는데 가장 많은 시간을 소비하는 학생은 누구일지 구하여라.

입력

첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 10,000), X가 공백으로 구분되어 입력된다. 두 번째 줄부터 M+1번째 줄까지 i번째 도로의 시작점, 끝점, 그리고 이 도로를 지나는데 필요한 소요시간 Ti가 들어온다. 시작점과 끝점이 같은 도로는 없으며, 시작점과 한 도시 A에서 다른 도시 B로 가는 도로의 개수는 최대 1개이다.
모든 학생들은 집에서 X에 갈수 있고, X에서 집으로 돌아올 수 있는 데이터만 입력으로 주어진다.

출력

첫 번째 줄에 N명의 학생들 중 오고 가는데 가장 오래 걸리는 학생의 소요시간을 출력한다.

예제 입력 1

4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3

예제 출력 1

10

#include<iostream> #include<algorithm> #include<vector> #include<queue> #include <list> using namespace std; vector<vector<int>> adajancent; vector<int> slowest; //노드수, 간선수, 목적지 int n, m, x; int Dijikstra(int here,int there) { struct vertexCost { int vertex; int cost; bool operator<(const vertexCost& val) const { return this->cost < val.cost; } bool operator>(const vertexCost& val) const { return this->cost > val.cost; } bool operator==(const vertexCost& val) const { return this->cost == val.cost; } }; priority_queue<vertexCost> discorvered; vector<int> best = vector<int>(n+1,INT32_MAX); best[here] = 0; discorvered.push(vertexCost{ here,0 }); while (discorvered.empty() == false) { int cost = discorvered.top().cost; here = discorvered.top().vertex; discorvered.pop(); if (best[here] < cost) continue; for (int i = 0; i < adajancent[here].size(); i++) { if (adajancent[here][i] == -1) continue; int nextCost = best[here] + adajancent[here][i]; if (nextCost >= best[i]) continue; best[i] = nextCost; discorvered.push(vertexCost{ i,nextCost }); } } return best[there]; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> x; adajancent = vector<vector<int>>(m+1, vector<int>(m+1, -1)); slowest = vector<int>(n+1); for (int i = 1; i < m+1; i++) { int startIdx, destIdx, Ti; cin >> startIdx >> destIdx >> Ti; adajancent[startIdx][destIdx] = Ti; } for (int i = 1; i < slowest.size(); i++) { slowest[i] = Dijikstra(i, x) + Dijikstra(x, i); } sort(slowest.begin(), slowest.end(),greater<int>()); cout << slowest[0] << '\n'; }
메모리 초과 / 시간 초과 됨

#include<iostream> #include<algorithm> #include<vector> #include<queue> #include <list> using namespace std; #define MAX 1001 //노드수, 간선수, 목적지 int n, m, x, answer; //BestCost int Dist[MAX], Res[MAX]; struct vertex { int dest; int cost; bool operator<(const vertex& val) const { return this->cost < val.cost; } bool operator>(const vertex& val) const { return this->cost > val.cost; } bool operator==(const vertex& val) const { return this->cost == val.cost; } }; //간선수 vector<vertex> adajancent[MAX]; void Input() { cin >> n >> m >> x; for (int i = 0; i < m; i++) { int startIdx, destIdx, Ti; cin >> startIdx >> destIdx >> Ti; adajancent[startIdx].push_back(vertex{ destIdx,Ti }); } } void Dijikstra(int start) { priority_queue<vertex> discorvered; Dist[start] = 0; discorvered.push(vertex{ start,0 }); while (!discorvered.empty()) { int cost = discorvered.top().cost; int dest = discorvered.top().dest; discorvered.pop(); for (int i = 0; i < adajancent[dest].size(); i++) { int Next = adajancent[dest][i].dest; int nCost = adajancent[dest][i].cost; if (Dist[Next] > cost + nCost) { Dist[Next] = cost + nCost; discorvered.push(vertex{ Next,Dist[Next] }); } } } } void ResetDist() { for (int j = 0; j <= n; j++) { Dist[j] = INT32_MAX; } } void Solution() { for (int i = 1; i <= n; i++) { ResetDist(); Dijikstra(i); Res[i] = Dist[x]; } ResetDist(); Dijikstra(x); for (int i = 1; i <= n; i++) { Res[i] = Res[i] + Dist[i]; } sort(Res, Res + n + 1, greater<int>()); answer = Res[0]; } void Solve() { Input(); Solution(); cout << answer << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); Solve(); }
시간초과 .. 이유 알 수 없음 ㅠ