-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path11833.cpp
96 lines (82 loc) · 2.1 KB
/
11833.cpp
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
87
88
89
90
91
92
93
94
95
96
/*
graphs > shortest path > single-source > weighted graph
difficulty: medium
date: 22/May/2019
hint: build the graph already with the given constraints
by: @brpapa
*/
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX_V 250
#define INF (1 << 30)
struct Tadj {
int id, w;
Tadj(int id, int w) : id(id), w(w) {}
};
vector<Tadj> adjList[MAX_V];
struct Theap {
int id, c;
Theap(int id, int c) : id(id), c(c) {}
bool operator<(const Theap &p) const {
return this->c < p.c;
}
};
int c[MAX_V];
int R; // cidades da rota: 0 a R-1
void insertEdge(int a, int b, int w) {
Tadj v(a, w), u(b, w);
// a é da rota
if (a < R) {
if (b == a + 1 && b < R) // b é o seguinte imediato e da rota
adjList[a].push_back(u);
else if (b == a - 1 || b >= R) // b é o anterior imediato da rota ou não é da rota
adjList[b].push_back(v);
}
// a não é da rota e b é da rota
else if (a >= R && b < R)
adjList[a].push_back(u);
// ambos não pertencem à rota
else if (a >= R && b >= R) {
adjList[a].push_back(u);
adjList[b].push_back(v);
}
}
void dijkstra(int init_v) {
priority_queue<Theap> heap;
c[init_v] = 0;
heap.push(Theap(init_v, c[init_v]));
while (!heap.empty()) {
int v = heap.top().id; // vértice de menor custo
heap.pop(); // fecha vértice v
for (Tadj u : adjList[v]) {
int c_new = c[v] + u.w;
if (c_new < c[u.id]) {
c[u.id] = c_new;
heap.push(Theap(u.id, c[u.id]));
}
}
}
}
int main() {
int V, E; // cidades, estradas
int x; // cidade onde veiculo foi consertado
while (true) {
scanf("%d %d %d %d", &V, &E, &R, &x);
if (V == 0 && E == 0 && R == 0 && x == 0) break;
// init
for (int v = 0; v < MAX_V; v++) {
adjList[v].clear();
c[v] = INF;
}
int v1, v2, w;
while (E--) {
scanf("%d %d %d", &v1, &v2, &w);
insertEdge(v1, v2, w);
}
dijkstra(x);
printf("%d\n", c[R-1]);
}
return 0;
}