106752024-04-07 21:16:33999Nagysebességű vasútcpp17Time limit exceeded 0/1002.099s30220 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>> q;
	disS[0]=0;
	q.push({0,0});
	while(!q.empty()){
		auto[c,a]=q.top();
		q.pop();
		c*=-1;
		if(c>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});
	disE[n-1]=0;
	while(!q.empty()){
		auto[c,a]=q.top();
		q.pop();
		c*=-1;
		if(c>disE[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});
				os[b]=a;
			}
		}
	}
	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]&&disS[n-1]==c+disS[a]+disE[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]&&disS[n-1]==c+disE[a]+disS[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
1Accepted3ms1828 KiB
2Accepted3ms2024 KiB
subtask20/35
3Time limit exceeded2.099s2452 KiB
4Time limit exceeded2.081s2544 KiB
5Wrong answer4ms2656 KiB
6Accepted4ms2692 KiB
7Accepted4ms2904 KiB
8Wrong answer4ms2668 KiB
subtask30/30
9Accepted72ms16100 KiB
10Accepted72ms16056 KiB
11Accepted71ms16252 KiB
12Accepted71ms16252 KiB
13Accepted72ms16320 KiB
14Wrong answer75ms16504 KiB
subtask40/35
15Time limit exceeded2.069s15832 KiB
16Time limit exceeded2.053s15820 KiB
17Time limit exceeded2.046s15776 KiB
18Time limit exceeded2.082s15964 KiB
19Accepted305ms29884 KiB
20Accepted287ms29884 KiB
21Accepted300ms29840 KiB
22Accepted289ms29852 KiB
23Accepted307ms30220 KiB
24Wrong answer145ms21596 KiB
25Accepted71ms17036 KiB
26Accepted72ms17036 KiB
27Wrong answer79ms17368 KiB