답안 #680701

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
680701 2023-01-11T14:34:12 Z vinnipuh01 Olympic Bus (JOI20_ho_t4) C++17
100 / 100
167 ms 4052 KB
#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 sqrt sqrtl

using namespace std;

const long long oo = 1e18;

long long sum, ans = 0, mx = 0, mn = oo, num, pos;


/*
    ViHHiPuh

   (( `'-""``""-'` ))
     )-__-_.._-__-(
   / --- (o _ o) --- \
   \ .-* ( .0. ) *-. /
   _'-. ,_ '=' _, .-'_
  / `;#'#'# - #'#'#;` \
 \_)) -----'#'----- ((_/
      # --------- #
  '# ------- ------ #'
  /..-'# ------- #'-.\
  _\...-\'# -- #'/-.../_
  ((____)- '#' -(____))


    cout << fixed << setprecision(6) << x;


    freopen ( "sum.in", "r", stdin )
*/


int n, m, x[ 50001 ], y[ 50001 ], a[ 50001 ], b[ 50001 ];
long long dp[ 201 ][ 201 ], d[ 201 ];
int p[ 201 ], pp[ 201 ], used[ 201 ];
long long dpp[ 201 ][ 201 ];
vector <pair<int, int> > v[ 222 ], vv[ 222 ];
set <pair<int, int> > st;
int mp[ 50001 ];

void dfs( int u ) {
	for ( int i = 1; i <= n; i ++ ) {
		d[ i ] = oo;
		p[ i ] = -1;
		used[ i ] = pp[ i ] = 0;
	}
	d[ u ] = 0;
	while ( true ) {
		num = -1;
		for ( int i = 1; i <= n; i ++ ) {
			if ( used[ i ] )
				continue;
			if ( num == -1 || d[ i ] < d[ num ] )
				num = i;
		}
		if ( num == -1 || d[ num ] == oo )
			break;
		used[ num ] = 1;
		for ( auto to : v[ num ] ) {
			if ( d[ to.first ] > d[ num ] + a[ to.second ] ) {
				d[ to.first ] = d[ num ] + a[ to.second ];
				p[ to.first ] = num;
				pp[ to.first ] = to.second;
			}
		}
	} 
}

void dfss( int u ) {
	for ( int i = 1; i <= n; i ++ ) {
		d[ i ] = oo;
		p[ i ] = -1;
		used[ i ] = pp[ i ] = 0;
	}
	d[ u ] = 0;
	while ( true ) {
		num = -1;
		for ( int i = 1; i <= n; i ++ ) {
			if ( used[ i ] )
				continue;
			if ( num == -1 || d[ i ] < d[ num ] )
				num = i;
		}
		if ( num == -1 || d[ num ] == oo )
			break;
		used[ num ] = 1;
		for ( auto to : vv[ num ] ) {
			if ( d[ to.first ] > d[ num ] + a[ to.second ] ) {
				d[ to.first ] = d[ num ] + a[ to.second ];
				p[ to.first ] = num;
				pp[ to.first ] = to.second;
			}
		}
	}
}

main () {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for ( int i = 1; i <= m; i ++ ) {
		cin >> x[ i ] >> y[ i ] >> a[ i ] >> b[ i ];
		v[ x[ i ] ].push_back( { y[ i ], i } );	
		vv[ y[ i ] ].push_back( { x[ i ], i } );
	}
	dfss( 1 );
	for ( int i = 1; i <= n; i ++ )
		dpp[ i ][ 1 ] = d[ i ];
	dfss( n );
	for ( int i = 1; i <= n; i ++ )
		dpp[ i ][ n ] = d[ i ];
	dfs( 1 );
	for ( int i = 1; i <= n; i ++ )
		dp[ 1 ][ i ] = d[ i ];
	for ( int i = n; i != -1; i = p[ i ] )
		mp[ pp[ i ] ] = 1;
	dfs( n );
	for ( int i = 1; i <= n; i ++ )
		dp[ n ][ i ] = d[ i ];
	for ( int i = 1; i != -1; i = p[ i ] )
		mp[ pp[ i ] ] = 1;
	ans = oo;
	if ( dp[ 1 ][ n ] != oo && dp[ n ][ 1 ] != oo )
		ans = dp[ 1 ][ n ] + dp[ n ][ 1 ];
	for ( int i = 1; i <= m; i ++ ) {
		if ( mp[ i ] ) {
			for ( int j = 0; j < v[ x[ i ] ].size(); j ++ ) {
				if ( v[ x[ i ] ][ j ].second == i ) {
					v[ x[ i ] ].erase( v[ x[ i ] ].begin() + j );
					break;
				}
			}
			v[ y[ i ] ].push_back( { x[ i ], i } );
			dfs( 1 );
			sum = d[ n ];
			dfs( n );
			if ( sum != oo && d[ 1 ] != oo )
				ans = min( ans, d[ 1 ] + sum + b[ i ] );
			v[ y[ i ] ].pop_back();
			v[ x[ i ] ].push_back( { y[ i ], i } );
		}
		else {
			num = dp[ 1 ][ n ];
			sum = dp[ n ][ 1 ];
			num = min( num, dp[ 1 ][ y[ i ] ] + a[ i ] + dpp[ x[ i ] ][ n ] + 0ll );
			sum = min( sum, dp[ n ][ y[ i ] ] + a[ i ] + dpp[ x[ i ] ][ 1 ] + 0ll );
//			cout << num << " " << sum << " " << b[ i ] << "\n";
			if ( num != oo && sum != oo )
				ans = min( sum + num + b[ i ], ans );
		}
//		cout << i << " - " << ans << "\n";
	}
	if ( ans == oo )
		cout << "-1";
	else
		cout << ans;
}

/*

dp[ 1 ][ y ] + a[ i ] + b[ i ] + dp[ x ][ n ]
dp[ n ][ x ] + a[ i ] + b[ i ] + dp[ x ][ 1 ]


*/

Compilation message

ho_t4.cpp:109:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  109 | main () {
      | ^~~~
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:139:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |    for ( int j = 0; j < v[ x[ i ] ].size(); j ++ ) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 724 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 5 ms 772 KB Output is correct
4 Correct 5 ms 724 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 45 ms 860 KB Output is correct
11 Correct 61 ms 748 KB Output is correct
12 Correct 58 ms 736 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 4 ms 736 KB Output is correct
15 Correct 3 ms 740 KB Output is correct
16 Correct 3 ms 648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 3804 KB Output is correct
2 Correct 29 ms 3676 KB Output is correct
3 Correct 29 ms 3928 KB Output is correct
4 Correct 3 ms 740 KB Output is correct
5 Correct 5 ms 724 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 19 ms 3896 KB Output is correct
10 Correct 27 ms 3844 KB Output is correct
11 Correct 24 ms 3940 KB Output is correct
12 Correct 22 ms 3928 KB Output is correct
13 Correct 21 ms 3920 KB Output is correct
14 Correct 21 ms 4052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 652 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 14 ms 2896 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 18 ms 3684 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 20 ms 3576 KB Output is correct
9 Correct 18 ms 3628 KB Output is correct
10 Correct 18 ms 3624 KB Output is correct
11 Correct 17 ms 3660 KB Output is correct
12 Correct 17 ms 3724 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 19 ms 3652 KB Output is correct
20 Correct 20 ms 3596 KB Output is correct
21 Correct 24 ms 3676 KB Output is correct
22 Correct 24 ms 3548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 724 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 5 ms 772 KB Output is correct
4 Correct 5 ms 724 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 45 ms 860 KB Output is correct
11 Correct 61 ms 748 KB Output is correct
12 Correct 58 ms 736 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 4 ms 736 KB Output is correct
15 Correct 3 ms 740 KB Output is correct
16 Correct 3 ms 648 KB Output is correct
17 Correct 19 ms 3804 KB Output is correct
18 Correct 29 ms 3676 KB Output is correct
19 Correct 29 ms 3928 KB Output is correct
20 Correct 3 ms 740 KB Output is correct
21 Correct 5 ms 724 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 19 ms 3896 KB Output is correct
26 Correct 27 ms 3844 KB Output is correct
27 Correct 24 ms 3940 KB Output is correct
28 Correct 22 ms 3928 KB Output is correct
29 Correct 21 ms 3920 KB Output is correct
30 Correct 21 ms 4052 KB Output is correct
31 Correct 3 ms 652 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 14 ms 2896 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 18 ms 3684 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 20 ms 3576 KB Output is correct
39 Correct 18 ms 3628 KB Output is correct
40 Correct 18 ms 3624 KB Output is correct
41 Correct 17 ms 3660 KB Output is correct
42 Correct 17 ms 3724 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 19 ms 3652 KB Output is correct
50 Correct 20 ms 3596 KB Output is correct
51 Correct 24 ms 3676 KB Output is correct
52 Correct 24 ms 3548 KB Output is correct
53 Correct 20 ms 3752 KB Output is correct
54 Correct 23 ms 3660 KB Output is correct
55 Correct 30 ms 3628 KB Output is correct
56 Correct 4 ms 724 KB Output is correct
57 Correct 3 ms 732 KB Output is correct
58 Correct 83 ms 3192 KB Output is correct
59 Correct 90 ms 3172 KB Output is correct
60 Correct 123 ms 3156 KB Output is correct
61 Correct 77 ms 3188 KB Output is correct
62 Correct 87 ms 3204 KB Output is correct
63 Correct 112 ms 3204 KB Output is correct
64 Correct 127 ms 3532 KB Output is correct
65 Correct 140 ms 3300 KB Output is correct
66 Correct 167 ms 3352 KB Output is correct
67 Correct 12 ms 2600 KB Output is correct
68 Correct 19 ms 3848 KB Output is correct
69 Correct 26 ms 3864 KB Output is correct
70 Correct 28 ms 3960 KB Output is correct
71 Correct 22 ms 3924 KB Output is correct
72 Correct 21 ms 3916 KB Output is correct
73 Correct 23 ms 3940 KB Output is correct
74 Correct 24 ms 3920 KB Output is correct