-
Notifications
You must be signed in to change notification settings - Fork 37
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
[최단경로] 2071031 유서현 #346
Open
ruruisryu
wants to merge
3
commits into
Altu-Bitu-Official:2071031-유서현2
Choose a base branch
from
ruruisryu:13_assignment
base: 2071031-유서현2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[최단경로] 2071031 유서현 #346
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "iostream" | ||
#include "vector" | ||
#include "queue" | ||
#include "algorithm" | ||
|
||
using namespace std; | ||
|
||
const int INF = 1000000; | ||
|
||
typedef pair<int, int> ci; | ||
|
||
int dijkstra(vector<vector<ci>> &nodes, int start, int dest, int n){ | ||
vector<int> dist(n+1, INF); | ||
|
||
priority_queue<ci, vector<ci>, greater<>> pq; | ||
|
||
// 시작정점 초기화 | ||
dist[start] = 0; | ||
pq.push({0, start}); | ||
while (!pq.empty()) { | ||
int weight = pq.top().first; // 현재 정점까지의 경로값 | ||
int node = pq.top().second; // 현재 탐색하려는 정점 | ||
pq.pop(); | ||
|
||
if (weight > dist[node]) { // 이미 더 작은 값으로 기록된 정점 | ||
continue; | ||
} | ||
for (int i = 0; i < nodes[node].size(); i++) { | ||
int next_node = nodes[node][i].first; // 연결된 정점 | ||
// 시작점으로부터 현재 node를 거쳐 다음 정점까지 가는 경로값 | ||
int next_weight = weight + nodes[node][i].second; | ||
if (next_weight < dist[next_node]) { // 최단 경로 값이 갱신된다면 | ||
dist[next_node] = next_weight; | ||
pq.push({next_weight, next_node}); | ||
} | ||
} | ||
} | ||
return dist[dest]; | ||
} | ||
|
||
int result(vector<vector<ci>> &nodes, int dest, int n){ | ||
int tmp, result = 0; | ||
// 모든 학생의 파티까지의 왕복 거리에 대해서 dijkstra알고리즘 실행 | ||
for(int i=1; i<=n; i++){ | ||
// tmp는 시작점에서 파티장소까지 갈 떄의 거리와 파티장소에서 시작점으로 돌아올 떄의 거리 합 | ||
tmp = dijkstra(nodes, i, dest, n) + dijkstra(nodes, dest, i, n); | ||
// result와 tmp중 값이 더 큰 것으로 result를 갱신 | ||
result = max(result, tmp); | ||
} | ||
return result; | ||
} | ||
|
||
int main(){ | ||
int n, m, x; | ||
int start, end, time; | ||
cin >> n >> m >> x; | ||
|
||
// 연결리스트 | ||
vector<vector<ci>> nodes(n+1, vector<ci>(0)); | ||
|
||
// 입력 | ||
for(int i=0; i<m; i++){ | ||
cin >> start >> end >> time; | ||
nodes[start].push_back({end, time}); | ||
} | ||
// 연산과 출력 | ||
cout << result(nodes, x, n); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3. 파티장소는 dest로 고정이기 때문에, 파티 장소에서 다른 정점들가지 걸리는 시간은 1번만 구해줘도 됩니다. 그래서,
dijkstra(nodes,dest,i,n)
은 한 번만 실행하는 게 더 효율적일 것 입니다!