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

[최단 경로] 2171039 이채원 #350

Open
wants to merge 3 commits into
base: 2171039-이채원
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
67 changes: 67 additions & 0 deletions 13_최단 경로/필수/BOJ_1238.cpp
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]); //최댓값 갱신
}

Comment on lines +50 to +63
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

//출력
cout << max_dist;

}
68 changes: 68 additions & 0 deletions 13_최단 경로/필수/BOJ_2458.cpp
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
Copy link
Contributor

@Dong-droid Dong-droid May 30, 2023

Choose a reason for hiding this comment

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

P3. queue를 사용해도 되지만

    for(int i = 1; i <=n; i++) 
        if(graph[node][i] == INF && graph[i][node] == INF) return false;

를 사용해서 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;
}