简略题意:
给出S和T,问从S到T的最短路。
但是两个节点能通行当且仅当两个节点的颜色相同。
每个节点有一个初始颜色,当前颜色剩余的时间,以及每种颜色的持续时长。
需要注意的点:
1. 双向边
2. 无解(两种颜色无限交替)
稍微修改一下最短路,每次从一个路口走到另一个路口时,需要附加上额外的时间代价,这个代价可以通过模拟得到。
然后就是一个普通的最短路。
#define others
#ifdef poj
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#endif // poj
#ifdef others
#include <bits/stdc++.h>
#endif // others
//#define file
#define all(x) x.begin(), x.end()
using namespace std;
const double eps = 1e-8;
int dcmp(double x) { if(fabs(x)<=eps) return 0; return (x>0)?1:-1;};
typedef long long LL;
namespace solver {
struct A {
LL col;
LL r;
LL t[2];
void in() {
char c;
scanf(" %c", &c);
if(c == 'B') col = 0;
else col = 1;
scanf("%lld %lld %lld", &r, &t[0], &t[1]);
}
};
int color(A x, int t) {
int sum = x.t[0] + x.t[1];
t = (t - x.r + sum) % sum;
return t < x.t[x.col^1]?x.col^1:x.col;
}
LL wait_time(A x, A y, LL t) {
for(int i = 0; i < 300; i++) {
if(color(x, t+i) == color(y, t+i)) {
return i;
}
}
return 1e18;
}
const LL maxn = 333;
vector<pair<LL, LL> > G[maxn];
LL dis[maxn], vis[maxn];
LL pre[maxn];
LL S, T;
LL n, m;
void solve() {
for(int i = 0; i < maxn; i++) dis[i] = 1e18;
scanf("%lld%lld", &S, &T);
scanf("%lld%lld", &n, &m);
vector<A> vec;
vec.resize(n + 1);
for(LL i = 1; i <= n; i++)
vec[i].in();
for(LL i = 1; i <= m; i++) {
LL u, v, w;
scanf("%lld%lld%lld", &u, &v, &w);
G[u].push_back({v, w});
G[v].push_back({u, w});
}
queue<LL> q;
q.push(S);
vis[S] = 1;
dis[S] = 0;
while(!q.empty()) {
LL u = q.front(); q.pop();
vis[u] = 0;
for(LL i = 0; i < G[u].size(); i++) {
LL v = G[u][i].first, w = G[u][i].second;
LL wait = wait_time(vec[u], vec[v], dis[u]);
if(dis[v] > dis[u] + w + wait) {
dis[v] = dis[u] + w + wait;
pre[v] = u;
if(!vis[v]) {
q.push(v);
vis[v] = 1;
}
}
}
}
if(dis[T] == 1e18) {
puts("0");
} else {
cout<<dis[T]<<endl;
vector<LL> G;
while(T) {
G.push_back(T);
T = pre[T];
}
reverse(all(G));
for(LL i = 0; i < G.size(); i++) {
printf("%lld%c", G[i], i == G.size()-1?'\n':' ');
}
}
}
}
int main() {
#ifdef file
freopen("gangsters.in", "r", stdin);
freopen("gangsters.out", "w", stdout);
#endif // file
// int t;
// scanf("%lld", &t);
// while(t--)
solver::solve();
return 0;
}