-
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
[최단 경로] 2171039 이채원 #350
Open
chae-jpg
wants to merge
3
commits into
Altu-Bitu-Official:2171039-이채원
Choose a base branch
from
chae-jpg:13-assignment
base: 2171039-이채원
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
[최단 경로] 2171039 이채원 #350
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 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,67 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <queue> | ||
|
||
using namespace std; | ||
typedef pair<int, int> pi; | ||
const int INF = 1e5; //최대 거리 : 1000 * 100 = 1e5 | ||
|
||
//시작 정점과 전체 정점의 개수, 그래프를 받아 시작 정점에 따른 모든 정점의 최단 경로를 리턴하는 함수 | ||
vector<int> dijkstra(int start, int v, vector<vector<pi>>& graph) { | ||
vector<int> dist(v+1, INF); | ||
priority_queue<pi, vector<pi>, greater<pi>> 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. | ||
continue; | ||
} | ||
for (int i = 0; i < graph[node].size(); i++) { | ||
int next_node = graph[node][i].first; | ||
int next_weight = weight + graph[node][i].second; | ||
if (next_weight < dist[next_node]) { | ||
dist[next_node] = next_weight; | ||
pq.push({next_weight, next_node}); | ||
} | ||
} | ||
} | ||
return dist; | ||
} | ||
|
||
int main() { | ||
ios_base :: sync_with_stdio(false); | ||
cin.tie(NULL); cout.tie(NULL); | ||
|
||
int n, m, x, a, b, w, max_dist = -1; | ||
|
||
cin >> n >> m >> x; | ||
vector<vector<pi>> graph(n+1, vector<pi>(0)); | ||
vector<vector<int>> dist (n+1, vector<int>(n+1, 0)); | ||
|
||
while (m--) { | ||
cin >> a >> b >> w; | ||
graph[a].push_back({b, w}); | ||
} | ||
|
||
//정점 별로 모든 정점에 대한 최단경로를 구함. | ||
for (int i = 1; i <= n; i++) { | ||
dist[i] = dijkstra(i, n, graph); | ||
} | ||
|
||
vector<int> dist_sum = dijkstra(x, n, graph); //파티장에서 모든 정점으로 돌아오는 최단 경로 | ||
|
||
//파티장에서 돌아오는 정점에 정점에서 파티장으로 가는 최단경로의 합을 구함. | ||
for (int i = 1; i <= n; i++) { | ||
dist_sum[i] += dist[i][x]; | ||
max_dist = max(max_dist, dist_sum[i]); //최댓값 갱신 | ||
} | ||
|
||
//출력 | ||
cout << max_dist; | ||
|
||
} |
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> | ||
|
||
using namespace std; | ||
|
||
const int INF = 500; //정점 간 거리를 1이라고 했을 때 모든 정점을 거친 거리 : 499. | ||
|
||
//정점의 개수와 그래프를 받고 정점 간 최단거리(연결되는지 여부)를 계산하는 함수 | ||
void 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++) { | ||
int cost = graph[i][k] + graph[k][j]; | ||
graph[i][j] = min(graph[i][j], cost); | ||
} | ||
} | ||
} | ||
} | ||
|
||
//특정 정점과 정점의 개수, 그래프를 받고 그 정점의 학생이 키를 알 수 있는지 여부를 리턴하는 함수 | ||
bool checkKnowHeight(int node, int n, vector<vector<int>>& graph) { | ||
queue<int> q; | ||
|
||
for (int i = 1; i <= n; i++) { | ||
if (graph[node][i] == INF) { //연결되어 있지 않는 노드일 때 | ||
q.push(i); | ||
} | ||
} | ||
|
||
while (!q.empty()) { | ||
if (graph[q.front()][node] == INF) { //반대로도 연결되어 있지 않을 때 -> 키 순서 파악 불가. | ||
return false; | ||
} | ||
q.pop(); | ||
} | ||
Comment on lines
+24
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3. queue를 사용해도 되지만
를 사용해서 for문으로도 구현할 수 있습니다~ |
||
return true; | ||
} | ||
|
||
int main() { | ||
ios_base :: sync_with_stdio(false); | ||
cin.tie(NULL); cout.tie(NULL); | ||
|
||
int n, m, a, b, cnt = 0; | ||
vector<vector<int>> graph; | ||
|
||
cin >> n >> m; | ||
graph.assign(n+1, vector<int> (n+1, INF)); | ||
for (int i = 1; i <= n; i++) { | ||
graph[i][i] = 0; | ||
} | ||
|
||
while (m--) { | ||
cin >> a >> b; | ||
graph[a][b] = 1; | ||
} | ||
|
||
floydWarshall (n, graph); //그래프 간 연결 여부를 플로이드-워셜로 계산 | ||
|
||
//모든 정점에 대해 자신의 키가 몇 번째인지 알 수 있는지 계산 | ||
for (int i = 1; i <= n; i++) { | ||
if (checkKnowHeight(i, n, graph)) { | ||
cnt++; | ||
} | ||
} | ||
//출력 | ||
cout << cnt; | ||
} |
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.
💯