题意
有一些不同铁路公司运营的铁路,你要按顺序使用你的车票,每张票只能乘坐对应公司的车一点距离,问你可达范围。
分析
这题可以想到的是 Dijkstra 算法,我们对其稍稍改变即可。
整体上,我们不需要专门考虑是否需要换乘,只用统计过程中可达的点。
我们给每个公司开一个优先队列。 每次选最近的更新,当距离超过长度限制就结束。如果没有超过,我们就弹出队首,更新当前点(只用更新当前公司的路径),同时记录一下每张票转移的点,方便后面处理换乘。
处理完当前的车票,对可能作为换乘的起点的点进行扩展(枚举所有通向不可达点的边),在边对应的优先队列中压入新的距离和点。
当然,可以直接忽略已经可达的点。
实现
代码奇短。
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
typedef long long LL;
typedef pair<LL,int> PLI;
struct node
{
int a,b;
LL c;
};
vector<node> e[N];
vector<int> a;
bool st[N];
priority_queue<PLI,vector<PLI>,greater<PLI>> q[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t --)
{
int n,m,k;
cin>>n>>m>>k;
for(int i = 1; i <= m; i ++) while(q[i].size()) q[i].pop();
for(int i = 1; i <= n; i ++) st[i] = 0,e[i].clear();
for(int i = 1,x,y,z,l; i <= m; i ++) cin>>x>>y>>z>>l,e[x].push_back({y,z,l}),e[y].push_back({x,z,l});
for(auto x : e[1])
if(!st[x.a]) q[x.b].push({x.c,x.a});
st[1] = 1;
while(k --)
{
int xx,yy;
cin>>xx>>yy;
a.clear();
while(!q[xx].empty())
{
auto t = q[xx].top();
LL w = t.first,y = t.second;
if(w > yy) break;
q[xx].pop();
if(st[y]) continue;
st[y] = 1;
a.push_back(y);
for(auto z : e[y])
if(!st[z.a] && z.b == xx) q[xx].push({w + z.c,z.a});
}
for(auto y : a)
for(auto x : e[y])
if(!st[x.a]) q[x.b].push({x.c,x.a});
}
for(int i = 1; i <= n; i ++) cout<<st[i];
cout<<'\n';
}
return 0;
}