106572024-04-07 19:02:29999Nagysebességű vasútcpp17Wrong answer 0/100508ms29368 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
#include <queue>
using namespace std;

int main() {
	int n,m;cin>>n>>m;
	vector<vector<pair<int,int>>> v(n);
	for(int i = 0;i<m;i++){
		int a,b,c;
		cin>>a>>b>>c;
		v[a].push_back({b,c});
		v[b].push_back({a,c});
	}
	vector<int> disS(n,INT_MAX),disE(n,INT_MAX);
	priority_queue<pair<int, int>> q;
	disS[0]=0;
	disE[n-1]=0;
	q.push({0,0});
	vector<bool> proc1(n),proc2(n);
	while(!q.empty()){
		int a=q.top().second;
		q.pop();
		if(proc1[a])continue;
		proc1[a]=true;
		for(auto u:v[a]){
			int b=u.first,w=u.second;
			if(disS[b]>disS[a]+w){
				disS[b]=disS[a]+w;
				q.push({-disS[b],b});
			}
		}
	}
	q.push({0,n-1});
	while(!q.empty()){
		int a=q.top().second;
		q.pop();
		if(proc2[a])continue;
		proc2[a]=true;
		for(auto u:v[a]){
			int b=u.first,w=u.second;
			if(disE[b]>disE[a]+w){
				disE[b]=disE[a]+w;
				q.push({-disE[b],b});
			}
		}
	}
	int ans=INT_MAX;
	for(int i = 0;i<n;i++){
		for(auto u : v[i]){
			int a=i,b=u.first,c=u.second;
			if(disS[a]+disE[b]+c>disS[n-1]){
				if(disS[a]+disE[b]+c-disS[n-1]+1<c){//cout<<a<<' '<<b<<endl;
					ans=min(ans,disS[a]+disE[b]+c-disS[n-1]+1);
				}
			}
			if(disS[b]+disE[a]+c>disS[n-1]){
				if(disS[b]+disE[a]+c-disS[n-1]+1<c){//cout<<a<<' '<<b<<endl;
					ans=min(ans,disS[b]+disE[a]+c-disS[n-1]+1);
				}
			}
		}
	}cout<<(ans==INT_MAX?-1:ans)<<endl;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1808 KiB
2Accepted3ms2008 KiB
subtask20/35
3Wrong answer4ms2476 KiB
4Wrong answer4ms2812 KiB
5Accepted4ms2680 KiB
6Accepted4ms2908 KiB
7Accepted4ms2848 KiB
8Wrong answer4ms3128 KiB
subtask30/30
9Accepted129ms15224 KiB
10Accepted129ms15316 KiB
11Accepted128ms15228 KiB
12Accepted128ms15464 KiB
13Accepted129ms15568 KiB
14Wrong answer131ms15660 KiB
subtask40/35
15Wrong answer499ms29032 KiB
16Wrong answer508ms29208 KiB
17Wrong answer492ms29368 KiB
18Wrong answer495ms29252 KiB
19Accepted488ms29204 KiB
20Accepted507ms29356 KiB
21Accepted481ms29284 KiB
22Accepted481ms29280 KiB
23Accepted486ms29276 KiB
24Wrong answer259ms20648 KiB
25Accepted128ms16224 KiB
26Accepted128ms16320 KiB
27Wrong answer130ms16412 KiB