Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[최단 경로] 2176063 김서현 #353

Open
wants to merge 22 commits into
base: 2176063-김서현2
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5c5694a
[5월 19일] 트리 라이브 코딩 코드 업로드
jk0527 May 19, 2023
cf31227
Merge branch 'main' of https://github.com/Altu-Bitu-Official/Altu-Bitu-4
jk0527 May 19, 2023
a8cc122
[5월 19일] 트리 리드미 수정
jk0527 May 19, 2023
5ab3590
[5월 23일] 트리 과제 코드 업로드
jk0527 May 23, 2023
20be664
Merge branch 'main' of https://github.com/Altu-Bitu-Official/Altu-Bitu-4
jk0527 May 23, 2023
dad8d2c
[5월 23일] 트리 리드미 수정
jk0527 May 23, 2023
1373cde
[5월 23일] 최단 경로 강의 자료 업로드
jk0527 May 23, 2023
6d1de48
Merge branch 'main' of https://github.com/Altu-Bitu-Official/Altu-Bitu-4
jk0527 May 23, 2023
b57dc56
[5월 23일] 최단 경로 리드미 업로드
jk0527 May 23, 2023
c908c33
[5월 23일] 리드미 수정
jk0527 May 23, 2023
dfb69e0
[5월 26일] 최단 경로 강의 자료 수정
jk0527 May 26, 2023
65bd5df
[5월 26일] 최단 경로 강의 자료 수정
jk0527 May 26, 2023
7b46298
[5월 26일] 최단 경로 라이브 코딩 코드 업로드
jk0527 May 26, 2023
74a7044
Merge branch 'main' of https://github.com/Altu-Bitu-Official/Altu-Bitu-4
jk0527 May 26, 2023
fce223d
[5월 26일] 최단 경로 리드미 수정
jk0527 May 26, 2023
91e590f
[최단 경로] 0530
sforseohn May 30, 2023
c36add7
Merge branch '2176063-김서현2' into 13-assignment
sforseohn May 30, 2023
0efc874
[최단 경로] 0530
sforseohn May 30, 2023
020306f
Merge branch '13-assignment' of https://github.com/sforseohn/Altu-Bit…
sforseohn May 30, 2023
042e028
[최단 경로] 0602
sforseohn Jun 2, 2023
5cedeff
[최단 경로] 추가제출
sforseohn Jun 2, 2023
0e0ea97
Merge branch '2176063-김서현2' into 13-assignment
sforseohn Jun 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions 13_최단 경로/필수/1238.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int, int> ci;
const int INF = 1e5; // n * 가중치의 최대값

int dijkstra(int start, int end, int n, vector<vector<ci>> &graph) {
vector<int> dist(n+1, INF);
dist[start] = 0; // 시작점에서의 시간
priority_queue<ci, vector<ci>, greater<>> pq;
pq.push({0, start}); // 시간, 시작점

while(!pq.empty()) {
int time = pq.top().first; // 지금 뽑힌 정점의 time
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3. time은 다른 언어에서 키워드인 것 같아요. 혹시 모르니까 조심하세요~ ⚠️

int node = pq.top().second;
pq.pop(); // 안하면 시간초과

if(time > dist[node]) { // 이미 저장된 시간보다 길다 -> 업데이트할 필요 X
continue;
}

for(int i = 0; i < graph[node].size(); i++) {
int next_node = graph[node][i].first;
int next_time = graph[node][i].second + time;

if(dist[next_node] > next_time) {
dist[next_node] = next_time;
pq.push({next_time, next_node});
}
}
}
return dist[end];
}

int findLongest(int x, int n, vector<vector<ci>> graph) {
int ans = 0;

for(int i = 1; i <= n; i++) {
int time = dijkstra(i, x, n, graph) + dijkstra(x, i, n, graph); // 가는 시간 + 오는 시간
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3. dijkstra(x,i,n,graph)는 한 번만 실행되도 되기 때문에 for문 전에 실행한 결과를 저장해 두고 사용하면 시간을 더 줄일 수 있을 거에요.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dijkstra를 한 번만 실행하니까 시간이 두 배로 줄었어요..!! 감사합니다!

ans = max(ans, time); // 최대값 구하기
}
return ans;
}

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

int n, m, x, a, b, t;

// 입력
cin >> n >> m >> x; // x: 파티 목적지
vector<vector<ci>> graph(n+1, vector<ci>(0));

while(m--) {
cin >> a >> b >> t;
graph[a].push_back({b, t}); // 단방향 도로
}

// 연산
int ans = findLongest(x, n, graph);

// 출력
cout << ans << '\n';

return 0;
}