106812024-04-07 21:38:55999Nagysebességű vasútcpp17Time limit exceeded 30/1002.075s31084 KiB
// Source: https://usaco.guide/general/io

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

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	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),os(n,-1);
	priority_queue<pair<int, int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
	disS[0]=0;
	disE[n-1]=0;
	q.push({0,0});
	while(!q.empty()){
		auto [d,a]=q.top();
		q.pop();
		if(d>disS[a])continue;
		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});
				os[b]=a;
			}
		}
	}
	vector<bool> shortestPath(n);
    int curr = n-1;
    while(curr!=-1) {
        shortestPath[curr]=true;
        curr=os[curr];
    }
	q.push({0,n-1});
	while(!q.empty()){
		auto [d,a]=q.top();
		q.pop();
		if(d>disS[a])continue;
		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&&!(shortestPath[a]&&shortestPath[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&&!(shortestPath[a]&&shortestPath[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
1Accepted3ms1976 KiB
2Accepted3ms2216 KiB
subtask20/35
3Time limit exceeded2.049s2552 KiB
4Time limit exceeded2.073s1900 KiB
5Wrong answer4ms2568 KiB
6Wrong answer4ms2860 KiB
7Accepted4ms2796 KiB
8Accepted4ms3056 KiB
subtask330/30
9Accepted74ms16496 KiB
10Accepted71ms16808 KiB
11Accepted72ms17024 KiB
12Accepted71ms17140 KiB
13Accepted79ms17104 KiB
14Accepted72ms17188 KiB
subtask40/35
15Time limit exceeded2.058s16604 KiB
16Time limit exceeded2.058s16540 KiB
17Time limit exceeded2.066s16488 KiB
18Time limit exceeded2.075s16424 KiB
19Accepted284ms30616 KiB
20Accepted296ms30576 KiB
21Wrong answer291ms30724 KiB
22Accepted287ms30696 KiB
23Accepted303ms31084 KiB
24Accepted138ms22480 KiB
25Accepted74ms18176 KiB
26Accepted75ms18152 KiB
27Accepted74ms18076 KiB