很明显是个最短路的模型,考虑用spfa.
由从一个点到另一个点的时间为l(u,v)+wait(u,v),wait(u,v)为等待时间;
因为从一个点到另一个点,早走总比晚走好,所以直接在spfa的不等式中加上wait(u,v)即可;
由从一个点到另一个点的时间为l(u,v)+wait(u,v),wait(u,v)为等待时间;
因为从一个点到另一个点,早走总比晚走好,所以直接在spfa的不等式中加上wait(u,v)即可;
wait的计算方法参见:http://www.cnblogs.com/Rinyo/archive/2012/11/29/2795030.html
代码如下(C++初学者...代码可能有点丑...):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 305, MAXM = 14005, INF = 1e8;
int N, M, st, en;
struct city
{
int initial, i_last; // initial color and last time;
int last[2];
}v[MAXN];
vector<int> e[MAXN], l[MAXN];
int dis[MAXN] = {0};
void calc(int time, int k, int& ck, int& nk)
{
if(time < v[k].i_last)
{
ck = v[k].initial;
nk = v[k].i_last;
}
else
{
int tmp = (time - v[k].i_last) % (v[k].last[0] + v[k].last[1]);
int base = time - tmp;
if(v[k].initial == 0)
{
if(tmp < v[k].last[1]) {ck = 1; nk = v[k].last[1] + base;}
else {ck = 0; nk = v[k].last[0] + v[k].last[1] + base;}
}
else
{
if(tmp < v[k].last[0]) {ck = 0; nk = v[k].last[0] + base;}
else {ck = 1; nk = v[k].last[0] + v[k].last[1] + base;}
}
}
}
int wait(int time, int x, int y, int f)
{
int curx, cury, nextx, nexty;
calc(time, x, curx, nextx); calc(time, y, cury, nexty);
if(curx == cury) return time;
if(nextx == nexty)
{
if(f == 0) return wait(nextx, x, y, 1);
else if(time <= v[x].i_last || time <= v[y].i_last) return wait(nextx, x, y, 1);
else return INF;
}
return min(nextx, nexty);
}
int father[MAXN] = {0};
void print(int r)
{
if(r > 0)
{
print(father[r]);
cout << r << ' ';
}
}
void SPFA()
{
int ll = -1, r = 0;
int queue[MAXM] = {0};
bool vis[MAXN] = {0};
for(int i = 1; i <= N; ++i) dis[i] = INF;
dis[st] = 0;
vis[st] = true;
queue[0] = st;
while(ll < r)
{
ll++;
int cur = queue[ll];
vis[cur] = false;
for(int i = 0; i < e[cur].size(); ++i)
{
int wait_time = wait(dis[cur], cur, e[cur][i], 0);
if(dis[e[cur][i]] > l[cur][i] + wait_time)
{
dis[e[cur][i]] = l[cur][i] + wait_time;
father[e[cur][i]] = cur;
if(!vis[e[cur][i]])
{
vis[e[cur][i]] = true;
queue[++r] = e[cur][i];
}
}
}
}
if(dis[en] == INF)
{
cout << '0';
return ;
}
cout << dis[en] << '\n';
print(en);
}
int main()
{
cin >> st >> en >> N >> M;
scanf("\n");
for(int i = 1; i <= N; ++i)
{
char c;
scanf("%c", &c);
cin >> v[i].i_last >> v[i].last[0] >> v[i].last[1];
scanf("\n");
if(c == 'B') v[i].initial = 0;
else v[i].initial = 1;
}
for(int i = 1; i <= M; ++i)
{
int x, y, ll;
cin >> x >> y >> ll;
e[x].push_back(y);
l[x].push_back(ll);
e[y].push_back(x);
l[y].push_back(ll);
}
SPFA();
return 0;
}