Submission #513456

#TimeUsernameProblemLanguageResultExecution timeMemory
513456vinnipuh01Commuter Pass (JOI18_commuter_pass)C++17
55 / 100
752 ms54320 KiB
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define int long long
 
using namespace std;
 
const long long oo = 1000000000000000000;
 
long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
 
 
/*
    ViHHiPuh
 
   (( `'-""``""-'` ))
     )-__-_.._-__-(
   / --- (o _ o) --- \
   \ .-* ( .0. ) *-. /
   _'-. ,_ '=' _, .-'_
  / `;#'#'# - #'#'#;` \
 \_)) -----'#'----- ((_/
      # --------- #
  '# ------- ------ #'
  /..-'# ------- #'-.\
  _\...-\'# -- #'/-.../_
  ((____)- '#' -(____))
 
 
    cout << fixed << setprecision(6) << x;
 
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
 
    freopen ( "sum.in", "r", stdin )
*/
 
vector <pair<int, int> > v[ 100001 ], vv[ 100001 ];
set <pair<int, int> > st;
int d[ 100001 ], p[ 100001 ], dd[ 100001 ], d1[ 100001 ], d2[ 301 ][ 301 ];
map <pair<int, int>, bool> mp;
 
main () {
	int n, m;
	cin >> n >> m;
	int s, t;
	cin >> s >> t;
	int ss, tt;
	cin >> ss >> tt;
	int x[ m + 1 ], y[ m + 1 ], z[ m + 1 ];
	for ( int i = 1; i <= n; i ++ )
		d[ i ] = dd[ i ] = d1[ i ] = oo;
	for ( int i = 1; i <= m; i ++ ) {
		cin >> x[ i ] >> y[ i ] >> z[ i ];
		v[ x[ i ] ].push_back( { y[ i ], z[ i ] } );
		v[ y[ i ] ].push_back( { x[ i ], z[ i ] } );
	}
	if ( s == ss ) {
		d[ t ] = 0;
		st.insert( { d[ t ], t } );
		while ( st.size() ) {
			num = st.begin()->second;
			st.erase( st.begin() );
			for ( auto to : v[ num ] ) {
				if ( d[ to.first ] > d[ num ] + to.second ) {
					st.erase( { d[ to.first ], to.first } );
					d[ to.first ] = d[ num ] + to.second;
					st.insert( { d[ to.first ], to.first } );
				}
			}
		}
		dd[ tt ] = 0;
		st.insert( { dd[ tt ], tt } );
		while ( st.size() ) {
			num = st.begin()->second;
			st.erase( st.begin() );
			for ( auto to : v[ num ] ) {
				if ( dd[ to.first ] > dd[ num ] + to.second ) {
					st.erase( { dd[ to.first ], to.first } );
					dd[ to.first ] = dd[ num ] + to.second;
					st.insert( { dd[ to.first ], to.first } );
				}
			}  
		}
		d1[ s ] = 0;
		st.insert( { d1[ s ], s } );
		while ( st.size() ) {
			num = st.begin()->second;
			st.erase( st.begin() );
			for ( auto to : v[ num ] ) {
				if ( d1[ to.first ] > d1[ num ] + to.second ) {
					st.erase( { d1[ to.first ], to.first } );
					d1[ to.first ] = d1[ num ] + to.second;
					st.insert( { d1[ to.first ], to.first } );
				}
			}
		}
		mn = oo;
		for ( int i = 1; i <= n; i ++ ) {
			if ( d[ i ] + d1[ i ] == d1[ t ] ) {
//				cout << i << " - " << dd[ i ] << "\n";
				mn = min( mn, dd[ i ] );
			}
		}
		cout << mn;
	}
	else if ( n > 300 ) {
		d[ s ] = 0;
		st.insert( { d[ s ], s } );
		p[ s ] = -1;
		while ( st.size() ) {
			num = st.begin()->second;
			st.erase( st.begin() );
			for ( auto to : v[ num ] ) {
				if ( d[ to.first ] > to.second + d[ num ] ) {
					st.erase( { d[ to.first ], to.first } );
					d[ to.first ] = d[ num ] + to.second;
					p[ to.first ] = num;
					st.insert( { d[ to.first ], to.first } );
				}
			}
		}
		for ( int i = t; i != s; i = p[ i ] ) {
			mp[ { i, p[ i ] } ] = mp[ { p[ i ], i } ] = 1;
		}
		for ( int i = 1; i <= m; i ++ ) {
			if ( mp[ { x[ i ], y[ i ] } ] )
				z[ i ] = 0;
			vv[ x[ i ] ].push_back( { y[ i ], z[ i ] } );
			vv[ y[ i ] ].push_back( { x[ i ], z[ i ] } );
		}
		for ( int i = 1; i <= n; i ++ )
			dd[ i ] = oo;
		dd[ ss ] = 0;
		st.insert( { dd[ ss ], ss } );
		while ( st.size() ) {
			num = st.begin()->second;
			st.erase( st.begin() );
			for ( auto to : vv[ num ] ) {
				if ( dd[ to.first ] > to.second + dd[ num ] ) {
					st.erase( { dd[ to.first ], to.first } );
					dd[ to.first ] = to.second + dd[ num ];
					st.insert( { dd[ to.first ], to.first } );
				}
			}
		}
		cout << dd[ tt ];
	}
	else {
		for ( int i = 1; i <= n; i ++ ) {
			for ( int j = 1; j <= n; j ++ )
				d2[ i ][ j ] = oo;
		}
		for ( int i = 1; i <= n; i ++ ) {
			d2[ i ][ i ] = 0;
			st.insert( { d2[ i ][ i ], i } );
			while ( st.size() ) {
				num = st.begin()->second;
				st.erase( st.begin() );
				for ( auto to : v[ num ] ) {
					if ( d2[ i ][ to.first ] > d2[ i ][ num ] + to.second ) {
						st.erase( { d2[ i ][ to.first ], to.first } );
						d2[ i ][ to.first ] = d2[ i ][ num ] + to.second;
						st.insert( { d2[ i ][ to.first ], to.first } );
					}
				}
			}
		}
		mn = d2[ ss ][ tt ];
		for ( int i = 1; i <= n; i ++ ) {
			for ( int j = 1; j <= n; j ++ ) {
				if ( d2[ s ][ i ] + d2[ j ][ t ] + d2[ i ][ j ] == d2[ s ][ t ] ) {
//					cout << i << " " << j << "\n";
					mn = min( mn, d2[ ss ][ i ] + d2[ j ][ tt ] );
					mn = min( mn, d2[ ss ][ j ] + d2[ i ][ tt ] );
				}
			}
		}
		cout << mn;
	}
}

Compilation message (stderr)

commuter_pass.cpp:51:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   51 | main () {
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...