This repository was 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
Expand file tree
/
Copy pathp4.cpp
More file actions
142 lines (124 loc) · 2.81 KB
/
p4.cpp
File metadata and controls
142 lines (124 loc) · 2.81 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#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[10];
long long r[205][200];
ifstream fin("p4.in");
ofstream fout("p4.out");
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});
}
}
void readT() {
for(int i = 1; i <= T; i++) {
int x;
fin >> x;
t[i] = x;
}
}
void init(int n, int TT) {
for(int i = 0; i < (n + 1) * (TT + 1); i++) {
r[i/(TT + 1)][i % (TT + 1)] = (1LL << 60);
}
}
int main() {
heap<pair<pair<long long, int>, int>> h;
fin >> n >> m >> T;
int TT = (1 << T);
int x, y, z;
long long c;
readE(m);
readT();
init(n, TT);
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();
int bitmask = p.second;
for(pair<pair<long long, int>, int> x : ed[p.first.second]) {
int bitmask1 = x.second;
bitmask1 = (1 << (x.second - 1));
int y = bitmask | bitmask1;
if(((bitmask1 & bitmask) == 0) &&
r[x.first.second][y] >
p.first.first + x.first.first +
t[x.second]) {
r[x.first.second][y] =
p.first.first + x.first.first +
t[x.second];
h.push({{r[x.first.second][y], x.first.second}, y});
}
if(((bitmask1 & bitmask) != 0) &&
r[x.first.second][y] >
p.first.first + x.first.first) {
r[x.first.second][y] =
p.first.first + x.first.first;
h.push({{r[x.first.second][y], x.first.second}, y});
}
if(x.first.second == n) {
r1 = min(r[x.first.second][y], r1);
}
}
}
fout << (r1 == (1LL << 60) ? -1 : r1) << endl;
fin.close();
fout.close();
return 0;
}