반응형

문제 출처 :


https://www.acmicpc.net/problem/1238



알고리즘 분석 :


문제 해결에 필요한 사항

1. 다익스트라 

개념 :: http://www.crocus.co.kr/532

소스 코드 :: http://www.crocus.co.kr/533

2. 우선순위 큐를 이용한 다익스트라 :: http://www.crocus.co.kr/546


다익스트라 알고리즘을 이용하면 해결 할 수 있는 문제이다.


시작점 처음부터 끝점으로까지 기준으로 하여 계속 돌리고 끝점으로 시작점으로 해서 돌려서


시작 -> 끝까지 최단 거리 + 끝 -> 시작까지 최단 거리가 가장 큰 사람의 값을 도출하면 된다.



소스 코드 : 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
#include <queue>
#include <limits.h>
#include <cstdio>
 
#define max(a,b)(a > b ? a : b)
 
using namespace std;
 
typedef pair<intint> pii;
 
vector<pii> adj[1001]; 
 
vector<int> dijkstra(int src, int V, int E)
{
    // V만큼 배열을 INT_MAX로 초기화
    vector<int> dist(V, INT_MAX);
    dist[src] = 0;
 
    priority_queue<pii> pq;
 
    pq.push(pii(0, src));
 
    while (!pq.empty())
    {
        int cost = -pq.top().first;
        int here = pq.top().second;
 
        pq.pop();
 
        if (dist[here] < cost)
            continue;
 
        // 인접한 정점들을 모두 검사.
        for (int i = 0; i < adj[here].size(); i++)
        {
            int there = adj[here][i].first;
            int nextDist = cost + adj[here][i].second;
 
            // 더 짧은 경로를 발견하면, dist[]를 갱신하고 우선순위 큐에 넣는다.
            if (dist[there] > nextDist)
            {
                dist[there] = nextDist;
                pq.push(pii(-nextDist, there));
            }
        }
    }
 
 
    return dist;
}
 
int main()
{
    // 정점, 간선의 개수 및 도착점
    int V, E, end;
 
    scanf("%d %d %d"&V, &E, &end);
 
    V++// 정점의 개수가 1부터 시작하면 V++해준다.
 
    for (int i = 0; i < E; i++)
    {
        int from, to, val;
        scanf("%d %d %d"&from, &to, &val);
 
        adj[from].push_back(pii(to,val));
    }
 
    int ans = 0;
    for (int start = 1; start < V; start++)
    {
        vector<int> go = dijkstra(start, V, E);
        vector<int> come = dijkstra(end, V, E);
 
        ans = max(ans, go[end] + come[start]);
    }
 
    printf("%d", ans);
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[1865번] 웜홀  (0) 2017.03.25
[1389번] 케빈 베이컨의 6단계 법칙  (0) 2017.03.24
[2565번] 전깃줄  (12) 2017.03.23
[10775번] 공항  (0) 2017.03.23
[2133번] 타일 채우기  (4) 2017.03.23