106602024-04-07 19:13:52999Nagysebességű vasútcpp17Time limit exceeded 0/1002.081s1047944 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});
			}
		}
	}
	vector<vector<bool>> shortest(n,vector<bool>(n));
	int node=n-1;
	while(node!=0){
		int minv=INT_MAX,mindex;
		for(auto u:v[node]){
			int a=u.first;
			if(disS[a]<minv){
				mindex=a;
				minv=disS[a];
			}
		}
		shortest[node][mindex]=true;
		node=mindex;
	}
	
	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&&!shortest[a][b]){//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&&!shortest[a][b]){//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
1Accepted3ms1812 KiB
2Accepted3ms2008 KiB
subtask20/35
3Time limit exceeded2.061s1760 KiB
4Time limit exceeded2.081s1984 KiB
5Accepted4ms2876 KiB
6Accepted4ms3212 KiB
7Accepted4ms2968 KiB
8Wrong answer4ms3136 KiB
subtask30/30
9Runtime error483ms1047944 KiB
10Runtime error470ms1047700 KiB
11Runtime error569ms1047676 KiB
12Runtime error472ms1047656 KiB
13Runtime error469ms1047412 KiB
14Runtime error476ms1047200 KiB
subtask40/35
15Runtime error822ms1046964 KiB
16Runtime error822ms1046956 KiB
17Runtime error822ms1046976 KiB
18Runtime error822ms1046952 KiB
19Runtime error815ms1046948 KiB
20Runtime error815ms1046944 KiB
21Runtime error819ms1046944 KiB
22Runtime error816ms1046948 KiB
23Runtime error819ms1046948 KiB
24Runtime error596ms1046952 KiB
25Runtime error479ms1046952 KiB
26Runtime error472ms1046944 KiB
27Runtime error474ms1046948 KiB