B. Time Travel
题解
1.注意,路的数量总和不超过
2.由于可以原地等待,所以早到一个点一定比晚到更优
3.每到一个点,遍历所有邻边,如果这条边能在之后激活,那么看看经过这条边到达对面点的时间是否最少
code
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
struct node
{
int to,id;
};
vector<node> G[200005];
struct fresh
{
int pos,val;
bool operator<(const fresh &b) const{return b.val<val;}
};
int ti[200005];
vector<int> a[200005];
int main()
{
int n,t;
cin>>n>>t;
for(int i=1;i<=n;i++) ti[i]=inf;
for(int i=1;i<=t;i++)
{
int m;
cin>>m;
for(int j=1;j<=m;j++)
{
int x,y;
cin>>x>>y;
G[x].push_back({y,i});
G[y].push_back({x,i});
}
}
int k;
cin>>k;
for(int i=1;i<=k;i++)
{
int x;
cin>>x;
a[x].push_back(i);
}
priority_queue<fresh> q;
q.push({1,1});
while(q.size())
{
int now=q.top().pos,val=q.top().val;
q.pop();
//printf("now:%d arrive:%d\n",now,val);
if(ti[now]<=val) continue;
ti[now]=val;
for(auto next:G[now])
{
int to=next.to,id=next.id;
auto near=lower_bound(a[id].begin(),a[id].end(),ti[now]);
if(near==a[id].end()) continue;
if(*near+1>=ti[to]) continue;
q.push({to,*near+1});
}
}
if(ti[n]==inf) puts("-1");
else cout<<ti[n]-1;
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验