Acing4871. 最早时刻(dijistra有禁止离开时刻,出发时等待直到不禁止)

4871. 最早时刻(有禁止离开时刻,出发时等待直到不禁止)

在这里插入图片描述
输入样例1:

4 6
1 2 2
1 3 3
1 4 8
2 3 4
2 4 5
3 4 3
0
1 3
2 3 4
0

输出样例1:

7

在这里插入图片描述
输入样例2:

3 1
1 2 3
0
1 3
0

输出样例2:

-1
int fun(int u,int t){//fun(u,dist[u])  u就是城市的编号,dist[u]即走到t的时间
//跟经典的单源最短路径相比,这里从城市u到城市v,不能直接比较
//dist[to]>dist[u]+w,因为在城市u准备出发的时候,可能此时时刻t(即dist[u])是不能
//出发的,需要等待,于是从城市u到城市v的时间是 w+等待的时间
	int wait=0;
	while(prohibit[u].count(t)){
		t++;
		wait++;
	}//t时刻可以出发了
	return wait;
	
}
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct edge{
	int nxt,to,w;
}e[N<<1];
int head[N];
int cnt=0;
void add(int u,int v,int w){
	e[++cnt].to=v;
	e[cnt].w=w;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
int n,m;
const int inf=0x3f3f3f3f;
int dist[N];
bool vis[N];
typedef struct node{
	int dis,u;
	bool operator<(const node & b)const{
		return dis>b.dis;//默认大顶堆
	}
}node;
set<int> prohibit[N];

int fun(int u,int t){//fun(u,dist[u])  u就是城市的编号,dist[u]即走到t的时间
//跟经典的单源最短路径相比,这里从城市u到城市v,不能直接比较
//dist[to]>dist[u]+w,因为在城市u准备出发的时候,可能此时时刻t(即dist[u])是不能
//出发的,需要等待,于是从城市u到城市v的时间是 w+等待的时间
	int wait=0;
	while(prohibit[u].count(t)){
		t++;
		wait++;
	}//t时刻可以出发了
	return wait;
	
}
priority_queue<node> Q;
void dijistra(int s){
	for(int i=1;i<=n;i++){
		dist[i]=inf;
		vis[i]=false;
	}
	dist[s]=0;
	
	Q.push({0,s});
	for(int t=0;t<n;t++){
		if(Q.empty()){
//			cout<<"-1";
//			dist[n]=-1;
			return ;
		}
		node p=Q.top();Q.pop();
		int u=p.u;
		if(vis[u]){
			t--;
			continue;
		}
		vis[u]=true;
		int wait=fun(u,dist[u]);
		for(int i=head[u];i;i=e[i].nxt){
			int to=e[i].to;
			int w=e[i].w;
			
				if(!vis[to]&&dist[to]>dist[u]+w+wait){
					dist[to]=dist[u]+w+wait;
					Q.push({dist[to],to});
//					vis[to]=true;
				}
		
		}
		
	}
}
signed main(){
	cin>>n>>m;
	int u,v,w;
	for(int i=1;i<=m;i++){
		cin>>u>>v>>w;
		add(u,v,w);
		add(v,u,w);
	}
	int x,k;
	for(int i=1;i<=n;i++){
		cin>>k;
		while(k--){
			cin>>x;
			prohibit[i].insert(x);
		}
	}
	dijistra(1);
	if(dist[n]==inf)cout<<"-1";
	else cout<<dist[n];
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值