首先求一遍最短路,若我们最后在某个节点能抓到czx,那么我们从起点到这个节点一定是走的最短路,没有必要走更长的路,那样只会更加浪费时间。
而我们到这个点的时间dis[x]是否一定是czx在这个点停留的时间区间【l, r】内?
不一定吧,我在l之前到也是可以的,因为我可以停下等着,但在r之后到就不行了
题意的“抓”其实有一种误导的感觉,不过样例很好,提示了另一种情形:czx自己找到我们
所以要跳出题意,不被字面误导,这里其实不是抓,是两个人以不同的移动方式,最后能否同时在一个点上
在那个公共点上,有两种可能:1.我先到 2.czx先到,我等他或他等我
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define debug(x) cerr << #x << "=" << x << endl;
const int MAXN = 100000 + 10;
const int MAXM = 1000000 + 10;
const int INF = 0x7fffffff;
int n,m,ee,b,t,dis[MAXN],vis[MAXN],ans,last[MAXN],tot;
struct node{
int id, dis;
bool operator < (const node &a) const {
return dis > a.dis;
}
};
priority_queue<node> q;
struct Edge{
int u,v,w,to;
Edge(){}
Edge(int u, int v, int w, int to) : u(u), v(v), w(w), to(to) {}
}e[MAXM];
inline void add(int u, int v, int w) {
e[++tot] = Edge(u,v,w,last[u]);
last[u] = tot;
}
struct ttim{
int t,x;
}tim[MAXN];
bool cmp(ttim a, ttim b) {
return a.t < b.t;
}
void dijkstra(int s) {
for(int i=1; i<=n; i++)
dis[i] = INF;
dis[s] = 0;
q.push((node){s, 0});
while(!q.empty()) {
node st = q.top();
q.pop();
int x = st.id, d = st.dis;
if(vis[x]) continue;
vis[x] = 1;
for(int i=last[x]; i; i=e[i].to) {
int v = e[i].v, w = e[i].w;
if(dis[v] > d + w) {
dis[v] = d + w;
q.push((node){v, dis[v]});
}
}
}
}
int main() {
scanf("%d %d %d %d", &n, &m, &b, &ee);
for(int i=1; i<=m; i++) {
int x,y,z;
scanf("%d %d %d", &x, &y, &z);
add(x, y, z);
add(y, x ,z);
}
dijkstra(b);
scanf("%d", &t);
tim[1].t = 0, tim[1].x = ee;
for(int i=1; i<=t; i++) {
int ai,x;
scanf("%d %d", &ai, &x);
tim[i+1].x = x, tim[i+1].t = ai;
}
sort(tim+1, tim+t+2, cmp);
for(int i=1; i<=t+1; i++) {
int l = tim[i].t, r = tim[i+1].t;
if(i == t+1) r = INF;
int x = tim[i].x;
if(dis[x] <= l) {
ans = l;
break;
} else if(dis[x] < r) {
ans = dis[x];
break;
}
}
printf("%d", ans);
return 0;
}