24~32行为何访问违规
  • 板块学术版
  • 楼主xzc_yhxr
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/10/4 11:27
  • 上次更新2024/10/4 14:27:22
查看原帖
24~32行为何访问违规
1072970
xzc_yhxr楼主2024/10/4 11:27
#include<bits/stdc++.h>
using namespace std ;
const int N = 1e5 + 500 ; 
int n , m ;
int x , y , z ;
struct egde{
	int to , w ;
};
int dis[N] ; 
vector<egde> tu[N] ;
bool used[N] ;
void spfa(int x){
	deque<int> dq ;
	memset(dis , 0x3f , sizeof(dis)) ;
	dis[x] = 0 ;
	dq.push_back(x) ;
	used[x] = 1 ; 
	while(!dq.empty()){
		int now = dq.front() ;
		dq.pop_front() ;
		used[now] = 0 ;
		for(int i = 0 ; i < tu[now].size() ; i++){
			int now_w = tu[now][i].w , now_to = tu[now][i].to ;
			if(dis[now] + now_w < dis[now_to]){
				dis[now_to] = dis[now] + now_w ;
				if(!used[now_to]){
					if(dis[now_to] < dis[dq.front()]) dq.push_front(now_to) ;
					else dq.push_back(now_to) ;
					used[now_to] = 1 ;
				}
			}
		}
	}
}
int main(){
	scanf("%d%d" , &n , &m) ;
	for(int i = 1 ; i <= m ; i++){
		scanf("%d%d%d" , &x , &y , &z) ;
		tu[x].push_back({y , z}) ;
	}
	spfa(1) ;
	if(dis[n] > 1e9 / 2){
		printf("impossible\n") ;
	}else{
		printf("%d\n" , dis[n]) ;
	}
	return 0 ;
} 
2024/10/4 11:27
加载中...