forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103.cpp
More file actions
151 lines (141 loc) · 3.06 KB
/
103.cpp
File metadata and controls
151 lines (141 loc) · 3.06 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
143
144
145
146
147
148
149
150
151
#pragma warning(disable:4786)
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define for if(0); else for
#define inf 1000000000
int small(int a, int b) {
return a < b ? a : b;
}
class junction {
public:
char color;
int rem;
int tb;
int tp;
};
class state {
public:
int t;
int pos;
state(int tt, int pp) : t(tt), pos(pp) {}
bool operator<(const state& rhs) const {
return t > rhs.t;
}
};
int src, des;
int n, m;
vector<junction> jc;
// get the current color of a specific junction
char curc(int t, int idx) {
if (t < jc[idx].rem) return jc[idx].color;
t -= jc[idx].rem;
t %= (jc[idx].tb + jc[idx].tp);
if (jc[idx].color == 'B') {
if (t < jc[idx].tp) return 'P';
else return 'B';
} else {
if (t < jc[idx].tb) return 'B';
else return 'P';
}
}
int tcha(int t, int idx) {
if (t < jc[idx].rem) return jc[idx].rem;
t -= jc[idx].rem;
int bk = t / (jc[idx].tb + jc[idx].tp) * (jc[idx].tb + jc[idx].tp) + jc[idx].rem;
t %= (jc[idx].tb + jc[idx].tp);
if (jc[idx].color == 'B') {
if (t < jc[idx].tp) return bk + jc[idx].tp;
else return bk + jc[idx].tp + jc[idx].tb;
} else {
if (t < jc[idx].tb) return bk + jc[idx].tb;
else return bk + jc[idx].tb + jc[idx].tp;
}
}
int main() {
scanf("%d %d", &src, &des);
--src, --des;
scanf("%d %d", &n, &m);
jc.resize(n);
for (int i = 0; i < n; ++i) {
char str[10];
scanf("%s %d %d %d", str, &jc[i].rem, &jc[i].tb, &jc[i].tp);
jc[i].color = str[0];
}
vector<vector<int> > len(n, vector<int>(n, -1));
vector<int> pa(n, -1);
for (int i = 0; i < m; ++i) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
--a, --b;
len[a][b] = len[b][a] = c;
}
int ret = inf;
priority_queue<state> que;
vector<int> out;
que.push(state(0, src));
vector<int> flag(n, inf);
flag[src] = 0;
while (!que.empty()) {
state cur = que.top(); que.pop();
if (cur.pos == des) {
ret = cur.t;
int dd = cur.pos;
while (1) {
out.push_back(dd);
dd = pa[dd];
if (dd == -1) break;
}
while (!que.empty()) {
que.pop();
}
break;
}
int from = cur.pos;
for (int i = 0; i < n; ++i) {
if (len[from][i] == -1) continue;
int to = i;
char cf, ct;
cf = curc(cur.t, from); ct = curc(cur.t, to);
if (cf == ct) {
int sum = cur.t + len[from][to];
if (sum >= flag[to]) continue;
flag[to] = sum;
pa[to] = from;
que.push(state(sum, to));
} else {
int nf, nt;
nf = tcha(cur.t, from); nt = tcha(cur.t, to);
if (nf != nt) {
int sum = len[from][to] + small(nf, nt);
if (sum >= flag[to]) continue;
flag[to] = sum;
pa[to] = from;
que.push(state(sum, to));
} else {
nf = tcha(nf, from); nt = tcha(nt, to);
if (nf != nt) {
int sum = len[from][to] + small(nf, nt);
if (sum >= flag[to]) continue;
flag[to] = sum;
pa[to] = from;
que.push(state(sum, to));
}
}
}
}
}
if (ret == inf) {
printf("0\n");
return 0;
}
printf("%d\n", ret);
for (int i = out.size() - 1; i >= 0; --i) {
printf("%d ", out[i] + 1);
}
printf("\n");
return 0;
}