This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp3.cpp
123 lines (109 loc) · 2.24 KB
/
p3.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
template<typename T>
class heap {
public:
heap(): count(0), v() { }
bool is_empty() {
return count == 0;
}
void push(const T& t) {
v.push_back(t);
int index = count;
count++;
while(index) {
if(v[index] < v[(index - 1)/2]) {
swap(v[index], v[(index - 1)/2]);
index = (index - 1)/2;
} else {
break;
}
}
}
void pop() {
swap(v[0], v[count - 1]);
v.pop_back();
count--;
int i = 0;
int minindex = 0;
while(2 * i < count - 1) {
if(2 * i + 2 < count && v[2 * i + 1] > v[2 * i + 2]) {
minindex = 2 * i + 2;
} else {
minindex = 2 * i + 1;
}
if(v[i] > v[minindex]) {
swap(v[i], v[minindex]);
i = minindex;
} else {
break;
}
}
}
T min() {
return v[0];
}
private:
int count;
vector<T> v;
};
int n, m, T;
vector< pair<pair<long long, int>, int> > ed[205];
int t[105][105];
long long r[205][105];
ifstream fin("p3.in");
ofstream fout("p3.out");
void init(int n, int T) {
for(int i = 0; i < (n + 1) * (T + 1); i++) {
r[i/(T + 1)][i % (T + 1)] = (1LL << 60);
}
}
void readM(int T) {
for(int i = 0; i < T * T; i++) {
fin >> t[i/T + 1][(i % T) + 1];
}
}
void readE(int m) {
for(int i = 0; i < m; i++) {
int x, y, z;
long long c;
fin >> x >> y >> c >> z;
ed[x].push_back({{c, y}, z});
ed[y].push_back({{c, x}, z});
}
}
int main() {
heap<pair<pair<long long, int>, int>> h;
fin >> n >> m >> T;
int x, y, z;
long long c;
readE(m);
readM(T);
init(n, T);
r[1][0] = 0;
h.push({{0, 1}, 0});
long long r1 = (1LL << 60);
while(!h.is_empty()) {
pair<pair<long long, int>, int> p = h.min();
h.pop();
for(pair<pair<long long, int>, int>& x : ed[p.first.second]) {
if(r[x.first.second][x.second] >
p.first.first + x.first.first +
t[p.second][x.second]) {
r[x.first.second][x.second] =
p.first.first + x.first.first +
t[p.second][x.second];
h.push({{r[x.first.second][x.second], x.first.second}, x.second});
}
if(x.first.second == n) {
r1 = min(r1, r[n][x.second]);
}
}
}
fout << (r1 == (1LL << 60) ? -1 : r1) << endl;
fin.close();
fout.close();
return 0;
}