Skip to content
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
68 changes: 68 additions & 0 deletions 11_투 포인터/필수/1238.cpp
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);
}
81 changes: 0 additions & 81 deletions 11_투 포인터/필수/14503.cpp

This file was deleted.

70 changes: 0 additions & 70 deletions 11_투 포인터/필수/20437.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions 11_투 포인터/필수/20922.cpp

This file was deleted.

56 changes: 56 additions & 0 deletions 11_투 포인터/필수/2458.cpp
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이라면 자신의 키 순위를 알 수 있을 것이다.
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. 설명좋습니다 👍 근데 이부분은 floydwarshall과 크게 상관없기 때문에 다른 함수로 만들어주면 더 좋아요. 예를 들면, countStudent() 어떤가요?? 😄

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);
}