题解:P17462 [GESP202609 八级] 末班车
RiverBird208 · · 题解
鲜花:看了一圈讨论区,没懂大家咋做的,口胡了一个思路,5min 写完了。感觉比较优秀,应该还可以改进。
注意到不能每次询问都跑 Dijkstra,考虑预处理。
因为每次出发时间不同,不好正着处理。考虑倒推。
设
至于询问,判断是否有
时间复杂度:
参考代码:
#include<bits/stdc++.h>
//#define int long long
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(x)
#define debugv(x)
#else
#define debug(x) cout<<#x<<':'<<x<<'\n';
#define debugv(x) {\
cout<<#x<<':'<<x.size();\
for(int i:x)cout<<' '<<i;\
cout<<'\n';\
}
#endif
const int N=505;
int n,m,t;
vector<array<int,3>>g[N];
int d[N][N];
bool v[N];
priority_queue<pair<int,int>>q;
void dij(int s,int*d){
for(int i=1;i<=n;i++)d[i]=-1e9;
memset(v,0,sizeof v);
d[s]=1e9,q.push({d[s],s});
while(!q.empty()){
int x=q.top().second;q.pop();
for(auto y:g[x]){
int v=min(d[x]-y[2],y[1]);
if(v<=d[y[0]])continue;
d[y[0]]=v;
q.push({d[y[0]],y[0]});
}
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>m>>t;
while(m--){
int x,y,l,t;cin>>x>>y>>l>>t;
g[y].push_back({x,l,t});
}
for(int i=1;i<=n;i++)dij(i,d[i]);
while(t--){
int x,y,s;cin>>x>>y>>s;
cout<<(d[y][x]>=s?"Yes\n":"No\n");
}
return 0;
}