106532024-04-07 18:57:32999Nagysebességű vasútcpp17Wrong answer 0/1001.544s29752 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;
		proc1[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[a]+disE[b]+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
1Wrong answer3ms1812 KiB
2Wrong answer3ms2004 KiB
subtask20/35
3Wrong answer6ms2484 KiB
4Wrong answer8ms2704 KiB
5Wrong answer4ms2592 KiB
6Wrong answer8ms2880 KiB
7Wrong answer10ms2860 KiB
8Wrong answer8ms3160 KiB
subtask30/30
9Wrong answer228ms15392 KiB
10Wrong answer178ms15604 KiB
11Wrong answer250ms15816 KiB
12Wrong answer180ms15816 KiB
13Wrong answer254ms15848 KiB
14Wrong answer241ms15884 KiB
subtask40/35
15Wrong answer1.1s29212 KiB
16Wrong answer1.008s29348 KiB
17Wrong answer851ms29484 KiB
18Wrong answer1.544s29424 KiB
19Wrong answer598ms29500 KiB
20Wrong answer549ms29512 KiB
21Wrong answer575ms29516 KiB
22Wrong answer569ms29740 KiB
23Wrong answer665ms29752 KiB
24Wrong answer691ms20960 KiB
25Wrong answer233ms16656 KiB
26Wrong answer180ms16876 KiB
27Wrong answer375ms17008 KiB