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

[최단경로] 2071031 유서현 #346

Open
wants to merge 3 commits into
base: 2071031-유서현2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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.

68 changes: 68 additions & 0 deletions 11_투 포인터/필수/2458.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를 갱신
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. 파티장소는 dest로 고정이기 때문에, 파티 장소에서 다른 정점들가지 걸리는 시간은 1번만 구해줘도 됩니다. 그래서, dijkstra(nodes,dest,i,n)은 한 번만 실행하는 게 더 효율적일 것 입니다!

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