-
Notifications
You must be signed in to change notification settings - Fork 37
[최단경로] 2071031 유서현 #346
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
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 2 commits
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 contains hidden or 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); | ||
| } |
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 hidden or 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,56 @@ | ||
| #include "iostream" | ||
| #include "vector" | ||
| #include "algorithm" | ||
|
|
||
| using namespace std; | ||
|
|
||
| const int INF = 9999999; // 최대 n-1개의 간선을 지나므로 n * (가중치 최대값) | ||
|
|
||
| int floydWarshall(int n, vector<vector<int>> &graph) { | ||
| for (int k = 1; k <= n; k++) { // 중간 정점 | ||
| for (int i = 1; i <= n; i++) { // 출발 정점 | ||
| for (int j = 1; j <= n; j++) { // 도착 정점 | ||
| // 중간에 k를 거쳐서 i에서 j로 갈 때의 비용 | ||
| int cost = graph[i][k] + graph[k][j]; | ||
| // 더 짧은 경로 선택 | ||
| graph[i][j] = min(graph[i][j], cost); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int count =0, result =0; | ||
| for(int i=1; i<=n; i++){ | ||
| for(int j=1; j<=n; j++){ | ||
| // 조건을 만족한다면 i와 j 사이의 대소 관계는 서로 알 수 있다는 뜻 | ||
| if(graph[i][j] != INF || graph[j][i] != INF){ | ||
| count++; | ||
| } | ||
| } | ||
| // 서로의 키 대소관계를 아는 사람의 수가 n-1 + 자기 자신은 항상 알수 있으므로 1 | ||
| // 즉 count가 n이라면 자신의 키 순위를 알 수 있을 것이다. | ||
| if(count == n){ | ||
| result++; | ||
| } | ||
| count =0; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| int main() { | ||
| int n, m; | ||
| int taller, smaller; | ||
|
|
||
| // 입력 | ||
| cin >> n >> m; | ||
| vector<vector<int>> graph(n + 1, vector<int>(n + 1, INF)); | ||
| for (int i = 1; i <= n; i++) { // 자기 자신과의 거리 | ||
| graph[i][i] = 0; | ||
| } | ||
| while (m--) { | ||
| cin >> taller >> smaller; | ||
| graph[taller][smaller] = 1; | ||
| } | ||
|
|
||
| // 연산 및 출력 | ||
| cout << floydWarshall(n, graph); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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. 설명좋습니다 👍 근데 이부분은 floydwarshall과 크게 상관없기 때문에 다른 함수로 만들어주면 더 좋아요. 예를 들면,
countStudent()어떤가요?? 😄