Submission #367952

# Submission time Handle Problem Language Result Execution time Memory
367952 2021-02-19T01:39:47 Z rqi Olympic Bus (JOI20_ho_t4) C++14
100 / 100
368 ms 3948 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

#define mp make_pair
#define sz(x) (int)(x).size()
#define f first
#define s second
#define pb push_back
#define all(x) begin(x), end(x)

void ckmin(ll& a, ll b){
	a = min(a, b);
}

const int mx = 50005;

const ll INF = 1e18;

int N, M;
int U[mx];
int V[mx];
int C[mx];
int D[mx];

vi adj[mx];

vi S = vi{0, 0};
vi spath[2]; //edge list of shortest path

int forbidden = -1; //forbidden edge
ll dist[mx];
ll o_dist[2][2][mx];

pi from[mx];
bool processed[mx];

void genDist(int typ, int disallowed = -1){
	forbidden = disallowed;

	for(int i = 1; i <= N; i++){
		dist[i] = INF;
	}


		
	// priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
	// dist[S[typ]] = 0;
	// pq.push(mp(dist[S[typ]], S[typ]));

	// while(sz(pq)){
	// 	int node = pq.top().s;
	// 	pq.pop();

	// 	for(auto u: adj[node]){
	// 		if(u == forbidden) continue;

	// 		int neigh = V[u];
	// 		ll cost = C[u];

	// 		if(dist[neigh] > dist[node]+cost){
	// 			dist[neigh] = dist[node]+cost;
	// 			from[neigh] = mp(node, u);
	// 			pq.push(mp(dist[neigh], neigh));
	// 		}
	// 	}
	// }

	for(int i = 1; i <= N; i++){
		processed[i] = 0;
	}

	dist[S[typ]] = 0;

	for(int iter = 1; iter <= N; iter++){
		ll min_dist = INF;
		int min_dist_node = 1;
		for(int i = 1; i <= N; i++){
			if(processed[i]) continue;
			if(dist[i] <= min_dist){
				min_dist = dist[i];
				min_dist_node = i;
			}
		}

		processed[min_dist_node] = 1;

		for(auto u: adj[min_dist_node]){
			if(u == forbidden) continue;
			int neigh = V[u];
			ll cost = C[u];

			if(dist[neigh] > dist[min_dist_node]+cost){
				dist[neigh] = dist[min_dist_node]+cost;
				from[neigh] = mp(min_dist_node, u);
			}
		}
	}

}

void reverseEdges(){
	for(int i = 1; i <= N; i++){
		adj[i].clear();
	}

	for(int i = 1; i <= M; i++){
		swap(U[i], V[i]);
	}

	for(int i = 1; i <= M; i++){
		adj[U[i]].pb(i);
	}
}

bool spec[mx];

int main(){
	cin >> N >> M;
	for(int i = 1; i <= M; i++){
		cin >> U[i] >> V[i] >> C[i] >> D[i];
		adj[U[i]].pb(i);
	}

	S[0] = 1;
	S[1] = N;

	ll ans = 0;

	for(int typ = 0; typ < 2; typ++){
		genDist(typ);
		ans+=dist[S[1-typ]]; //initialize to initial shortest paths

		// for(int i = 1; i <= N; i++){
		// 	cout << i << " " << dist[i] << "\n";
		// }

		// cout << dist[S[1-typ]] << "\n";

		if(dist[S[1-typ]] != INF){
			int curnode = S[1-typ];
			while(curnode != S[typ]){
				spath[typ].pb(from[curnode].s);
				curnode = from[curnode].f;
			}
			reverse(all(spath[typ]));
		}

		for(int i = 1; i <= N; i++){
			o_dist[typ][0][i] = dist[i];
		}
	}

	reverseEdges();

	for(int typ = 0; typ < 2; typ++){
		genDist(typ);

		for(int i = 1; i <= N; i++){
			o_dist[typ][1][i] = dist[i];
		}
	}

	reverseEdges();



	for(int typ = 0; typ < 2; typ++){
		for(auto ed: spath[typ]){
			spec[ed] = 1;
			ll pans = D[ed];

			U[M+1] = V[ed];
			V[M+1] = U[ed];
			C[M+1] = C[ed];

			adj[V[ed]].pb(M+1);

			genDist(0, ed);

			pans+=dist[S[1]];
			genDist(1, ed);
			adj[V[ed]].pop_back();
			pans+=dist[S[0]];
			ckmin(ans, pans);
		}
	}

	// for(int ed = 1; ed <= M; ed++){
	// 	spec[ed] = 1;
	// 	ll pans = D[ed];

	// 	U[M+1] = V[ed];
	// 	V[M+1] = U[ed];
	// 	C[M+1] = C[ed];

	// 	adj[V[ed]].pb(M+1);

	// 	genDist(0, ed);

	// 	pans+=dist[S[1]];
	// 	genDist(1, ed);
	// 	adj[V[ed]].pop_back();
	// 	pans+=dist[S[0]];
	// 	ckmin(ans, pans);
	// }

	for(int i = 1; i <= M; i++){
		if(spec[i]) continue;
		ll pans = D[i];
		pans+=min(o_dist[0][0][S[1]], o_dist[0][0][V[i]]+o_dist[1][1][U[i]]+C[i]);
		pans+=min(o_dist[1][0][S[0]], o_dist[1][0][V[i]]+o_dist[0][1][U[i]]+C[i]);

		// cout << i << " " << pans << "\n";
		// cout << o_dist[0][0][S[1]] << " " << o_dist[1][0][S[0]] << "\n";
		ckmin(ans, pans);
	}

	if(ans >= INF){
		cout << -1 << "\n";
	}
	else{
		cout << ans << "\n";
	}

}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1516 KB Output is correct
2 Correct 2 ms 1516 KB Output is correct
3 Correct 5 ms 1516 KB Output is correct
4 Correct 6 ms 1644 KB Output is correct
5 Correct 2 ms 1536 KB Output is correct
6 Correct 2 ms 1516 KB Output is correct
7 Correct 1 ms 1516 KB Output is correct
8 Correct 1 ms 1516 KB Output is correct
9 Correct 2 ms 1516 KB Output is correct
10 Correct 92 ms 1644 KB Output is correct
11 Correct 90 ms 1644 KB Output is correct
12 Correct 89 ms 1644 KB Output is correct
13 Correct 4 ms 1644 KB Output is correct
14 Correct 5 ms 1660 KB Output is correct
15 Correct 4 ms 1516 KB Output is correct
16 Correct 5 ms 1644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 2708 KB Output is correct
2 Correct 61 ms 2796 KB Output is correct
3 Correct 65 ms 2796 KB Output is correct
4 Correct 6 ms 1644 KB Output is correct
5 Correct 6 ms 1644 KB Output is correct
6 Correct 3 ms 1516 KB Output is correct
7 Correct 2 ms 1644 KB Output is correct
8 Correct 2 ms 1536 KB Output is correct
9 Correct 63 ms 2796 KB Output is correct
10 Correct 62 ms 2796 KB Output is correct
11 Correct 67 ms 2804 KB Output is correct
12 Correct 67 ms 2796 KB Output is correct
13 Correct 66 ms 2796 KB Output is correct
14 Correct 67 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1664 KB Output is correct
2 Correct 2 ms 1536 KB Output is correct
3 Correct 46 ms 2412 KB Output is correct
4 Correct 2 ms 1516 KB Output is correct
5 Correct 56 ms 2796 KB Output is correct
6 Correct 1 ms 1516 KB Output is correct
7 Correct 1 ms 1516 KB Output is correct
8 Correct 55 ms 2796 KB Output is correct
9 Correct 54 ms 2796 KB Output is correct
10 Correct 55 ms 2668 KB Output is correct
11 Correct 56 ms 2668 KB Output is correct
12 Correct 55 ms 2668 KB Output is correct
13 Correct 1 ms 1516 KB Output is correct
14 Correct 1 ms 1516 KB Output is correct
15 Correct 1 ms 1516 KB Output is correct
16 Correct 1 ms 1516 KB Output is correct
17 Correct 1 ms 1516 KB Output is correct
18 Correct 1 ms 1516 KB Output is correct
19 Correct 56 ms 2668 KB Output is correct
20 Correct 55 ms 2668 KB Output is correct
21 Correct 56 ms 2668 KB Output is correct
22 Correct 57 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1516 KB Output is correct
2 Correct 2 ms 1516 KB Output is correct
3 Correct 5 ms 1516 KB Output is correct
4 Correct 6 ms 1644 KB Output is correct
5 Correct 2 ms 1536 KB Output is correct
6 Correct 2 ms 1516 KB Output is correct
7 Correct 1 ms 1516 KB Output is correct
8 Correct 1 ms 1516 KB Output is correct
9 Correct 2 ms 1516 KB Output is correct
10 Correct 92 ms 1644 KB Output is correct
11 Correct 90 ms 1644 KB Output is correct
12 Correct 89 ms 1644 KB Output is correct
13 Correct 4 ms 1644 KB Output is correct
14 Correct 5 ms 1660 KB Output is correct
15 Correct 4 ms 1516 KB Output is correct
16 Correct 5 ms 1644 KB Output is correct
17 Correct 61 ms 2708 KB Output is correct
18 Correct 61 ms 2796 KB Output is correct
19 Correct 65 ms 2796 KB Output is correct
20 Correct 6 ms 1644 KB Output is correct
21 Correct 6 ms 1644 KB Output is correct
22 Correct 3 ms 1516 KB Output is correct
23 Correct 2 ms 1644 KB Output is correct
24 Correct 2 ms 1536 KB Output is correct
25 Correct 63 ms 2796 KB Output is correct
26 Correct 62 ms 2796 KB Output is correct
27 Correct 67 ms 2804 KB Output is correct
28 Correct 67 ms 2796 KB Output is correct
29 Correct 66 ms 2796 KB Output is correct
30 Correct 67 ms 2796 KB Output is correct
31 Correct 5 ms 1664 KB Output is correct
32 Correct 2 ms 1536 KB Output is correct
33 Correct 46 ms 2412 KB Output is correct
34 Correct 2 ms 1516 KB Output is correct
35 Correct 56 ms 2796 KB Output is correct
36 Correct 1 ms 1516 KB Output is correct
37 Correct 1 ms 1516 KB Output is correct
38 Correct 55 ms 2796 KB Output is correct
39 Correct 54 ms 2796 KB Output is correct
40 Correct 55 ms 2668 KB Output is correct
41 Correct 56 ms 2668 KB Output is correct
42 Correct 55 ms 2668 KB Output is correct
43 Correct 1 ms 1516 KB Output is correct
44 Correct 1 ms 1516 KB Output is correct
45 Correct 1 ms 1516 KB Output is correct
46 Correct 1 ms 1516 KB Output is correct
47 Correct 1 ms 1516 KB Output is correct
48 Correct 1 ms 1516 KB Output is correct
49 Correct 56 ms 2668 KB Output is correct
50 Correct 55 ms 2668 KB Output is correct
51 Correct 56 ms 2668 KB Output is correct
52 Correct 57 ms 2668 KB Output is correct
53 Correct 62 ms 2796 KB Output is correct
54 Correct 62 ms 2796 KB Output is correct
55 Correct 64 ms 2796 KB Output is correct
56 Correct 5 ms 1644 KB Output is correct
57 Correct 4 ms 1516 KB Output is correct
58 Correct 226 ms 2412 KB Output is correct
59 Correct 224 ms 2412 KB Output is correct
60 Correct 224 ms 2540 KB Output is correct
61 Correct 190 ms 2412 KB Output is correct
62 Correct 231 ms 2540 KB Output is correct
63 Correct 225 ms 2540 KB Output is correct
64 Correct 368 ms 2796 KB Output is correct
65 Correct 345 ms 3564 KB Output is correct
66 Correct 337 ms 3564 KB Output is correct
67 Correct 46 ms 3560 KB Output is correct
68 Correct 63 ms 3948 KB Output is correct
69 Correct 62 ms 3948 KB Output is correct
70 Correct 68 ms 3948 KB Output is correct
71 Correct 67 ms 3948 KB Output is correct
72 Correct 65 ms 3948 KB Output is correct
73 Correct 66 ms 3948 KB Output is correct
74 Correct 68 ms 3948 KB Output is correct