106192024-04-06 22:36:36111Nagysebességű vasútcpp17Accepted 100/100361ms84432 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

#define INF (int)1e16

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,M;
	cin>>N>>M;
	vector<vector<pair<int,int>>>g(N);
	vector<tuple<int,int,int>>e;
	for(int i=0;i<M;i++){
		int a,b,c;
		cin>>a>>b>>c;
		g[a].emplace_back(b,c);
		g[b].emplace_back(a,c);
		e.emplace_back(a,b,c);
		e.emplace_back(b,a,c);
	}
	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;
	vector<int>d0(N,INF),d1(N,INF),p(N,-1),v(N);
	d0[0]=0;
	q.emplace(0,0);
	while(!q.empty()){
		auto[d,i]=q.top();
		q.pop();
		if(d0[i]<d){
			continue;
		}
		for(auto[j,w]:g[i]){
			if(d0[i]+w<d0[j]){
				d0[j]=d0[i]+w;
				p[j]=i;
				q.emplace(d0[j],j);
			}
		}
	}
	for(int i=N-1;i>=0;i=p[i]){
		v[i]=1;
	}
	d1[N-1]=0;
	q.emplace(0,N-1);
	while(!q.empty()){
		auto[d,i]=q.top();
		q.pop();
		if(d1[i]<d){
			continue;
		}
		for(auto[j,w]:g[i]){
			if(d1[i]+w<d1[j]){
				d1[j]=d1[i]+w;
				q.emplace(d1[j],j);
			}
		}
	}
	int ans=INF-1;
	for(auto[a,b,c]:e){
		if(v[a]&&v[b]){
			continue;
		}
		int x=d0[N-1]-d0[a]-d1[b];
		if(x<=1){
			continue;
		}
		ans=min(ans,c-x+1);
	}
	cout<<(ans+1)%INF-1<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2060 KiB
subtask235/35
3Accepted4ms2916 KiB
4Accepted4ms3228 KiB
5Accepted4ms3272 KiB
6Accepted4ms3704 KiB
7Accepted4ms3604 KiB
8Accepted4ms3716 KiB
subtask330/30
9Accepted92ms33864 KiB
10Accepted96ms33960 KiB
11Accepted92ms33900 KiB
12Accepted93ms33896 KiB
13Accepted93ms33920 KiB
14Accepted92ms33916 KiB
subtask435/35
15Accepted345ms84192 KiB
16Accepted344ms84196 KiB
17Accepted344ms84344 KiB
18Accepted349ms84332 KiB
19Accepted354ms84432 KiB
20Accepted356ms84156 KiB
21Accepted361ms84132 KiB
22Accepted358ms84288 KiB
23Accepted360ms84144 KiB
24Accepted174ms50204 KiB
25Accepted92ms34096 KiB
26Accepted93ms34112 KiB
27Accepted90ms34100 KiB