답안 #244317

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
244317 2020-07-03T15:31:40 Z pit4h Olympic Bus (JOI20_ho_t4) C++14
37 / 100
1000 ms 5320 KB
#include<bits/stdc++.h>
using namespace std;

const int N = 201, M = 5e4+1;
const long long INF = 1e12+1;
int n, m, u[M], v[M];
long long c[M], d[M];
long long G[N][N], W[N][N], E[N][N][2];
long long skip[N][N][2], skipt[N][N][2]; // 0 -> (n, 1), 1 -> (1, n)

void calculate_skip(int p, int q) {
	for(int i=1; i<=n; ++i) {
		for(int j=1; j<=n; ++j) {
			skip[i][j][p==1] = INF;
		}
	}
	//calc skip here

	for(int k=1; k<=n; ++k) {
		if(k==q) continue;
		vector<bool> vis(n+1);
		vis[k] = 1;
		priority_queue<pair<long long, int>> Q;
		Q.push({0, q});
		skip[q][k][p==1] = 0;
		while(Q.size()) {
			auto cur = Q.top();
			cur.first *= -1;
			Q.pop();
			if(vis[cur.second]) continue;
			vis[cur.second] = 1;

			for(int i=1; i<=n; ++i) {
				if(i==k) continue;
				if(skip[i][k][p==1] > cur.first + W[i][cur.second]) {
					Q.push({-(cur.first + W[i][cur.second]), i});
					skip[i][k][p==1] = cur.first + W[i][cur.second];
				}
			}
		}
	}

	for(int i=1; i<=n; ++i) {
		for(int j=1; j<=n; ++j) {
			skipt[i][j][p==1] = INF;
		}
	}
	//calc skip here

	for(int k=1; k<=n; ++k) {
		if(k==q) continue;
		vector<bool> vis(n+1);
		vis[k] = 1;
		priority_queue<pair<long long, int>> Q;
		Q.push({0, q});
		skipt[q][k][p==1] = 0;
		while(Q.size()) {
			auto cur = Q.top();
			cur.first *= -1;
			Q.pop();
			if(vis[cur.second]) continue;
			vis[cur.second] = 1;

			for(int i=1; i<=n; ++i) {
				if(i==k) continue;
				if(skipt[i][k][p==1] > cur.first + W[cur.second][i]) {
					Q.push({-(cur.first + W[cur.second][i]), i});
					skipt[i][k][p==1] = cur.first + W[cur.second][i];
				}
			}
		}
	}
}

long long Dist(int U, bool q, int V) {
	if(!q && U == 1) return 0;
	if(q && U == n) return 0;
	long long res = INF;
	for(int i=1; i<=n; ++i) {
		if(i != U && i != V) {
			res = min(res, W[U][i] + skip[i][U][q]);	
		}
	}
	return res;
}

long long DistT(int U, bool q, int V) {
	if(!q && U == 1) return 0;
	if(q && U == n) return 0;
	long long res = INF;
	for(int i=1; i<=n; ++i) {
		if(i != U && i != V) {
			res = min(res, W[i][U] + skipt[i][U][q]);	
		}
	}
	return res;
}

long long solve(int p, int q) {

	long long ans = INF;

	for(int i=0; i<m; ++i) {

		long long path[2] = {0, 0};
// skipt[v[i]][u[i]][p==1]


		path[1] = DistT(v[i], p==1, u[i]) + c[i] + d[i] + Dist(u[i], p==n, v[i]);
		path[0] = INF;
		
		path[0] = min(path[0], DistT(v[i], p==n, u[i]) + c[i] + Dist(u[i], p==1, v[i]));
		
		

		long long replace_edge = INF;
		if(E[u[i]][v[i]][0] != c[i]) {
			replace_edge = E[u[i]][v[i]][0];
			path[0] = min(path[0], G[p][u[i]] + E[u[i]][v[i]][0] + G[v[i]][q]);
		}
		else {
			replace_edge = E[u[i]][v[i]][1];
			path[0] = min(path[0], G[p][u[i]] + E[u[i]][v[i]][1] + G[v[i]][q]);
		}
		if(replace_edge < INF) replace_edge = max(0LL, replace_edge - c[i]);
		if(G[p][u[i]] + c[i] + G[v[i]][q] > G[p][q]) path[0] = min(path[0], G[p][q]);

		else {
			path[0] = min(path[0], skip[p][u[i]][p==1]);
			for(int k=1; k<=n; ++k) {
				if(k != u[i] && k != v[i]) {
					path[0] = min(path[0], G[p][u[i]] + W[u[i]][k] + skip[k][u[i]][p==1]);
				}
			}
		}
		
		path[0] = min(path[0], replace_edge + G[p][v[i]] + c[i] + G[u[i]][q]);

		path[1] = min(path[1], replace_edge + G[q][v[i]] + c[i] + d[i] + G[u[i]][p]);


		ans = min(ans, path[0] + path[1]);
	}
	return ans;
}

int main() {

	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>m;

	for(int i=1; i<=n; ++i) {
		for(int j=1; j<=n; ++j) {
			G[i][j] = INF;
			W[i][j] = INF;
			E[i][j][0] = E[i][j][1] = INF;
		}
		G[i][i] = 0;
	}

	for(int i=0; i<m; ++i) {
		cin>>u[i]>>v[i]>>c[i]>>d[i];
		G[u[i]][v[i]] = min(G[u[i]][v[i]], c[i]);
		W[u[i]][v[i]] = min(W[u[i]][v[i]], c[i]);
		if(E[u[i]][v[i]][0] > c[i]) {
			E[u[i]][v[i]][1] = E[u[i]][v[i]][0];
			E[u[i]][v[i]][0] = c[i];
		}
		else if(E[u[i]][v[i]][1] > c[i]) {
			E[u[i]][v[i]][1] = c[i];
		}
	}

	for(int k=1; k<=n; ++k) {
		for(int i=1; i<=n; ++i) {
			for(int j=1; j<=n; ++j) {
				G[i][j] = min(G[i][j], G[i][k] + G[k][j]);	
			}
		}
	}
	
	calculate_skip(1, n);
	calculate_skip(n, 1);

	long long ans = min({G[1][n]+G[n][1], solve(1, n), solve(n, 1)});
	if(ans >= INF) cout<<-1;
	else cout<<ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 2944 KB Output is correct
2 Correct 23 ms 2944 KB Output is correct
3 Correct 91 ms 2992 KB Output is correct
4 Correct 94 ms 3064 KB Output is correct
5 Correct 7 ms 896 KB Output is correct
6 Correct 32 ms 2944 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 99 ms 2936 KB Output is correct
11 Correct 99 ms 2936 KB Output is correct
12 Correct 103 ms 3068 KB Output is correct
13 Correct 50 ms 2944 KB Output is correct
14 Correct 76 ms 2936 KB Output is correct
15 Correct 66 ms 2944 KB Output is correct
16 Correct 77 ms 3068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 303 ms 4860 KB Output is correct
2 Correct 295 ms 4984 KB Output is correct
3 Correct 292 ms 4856 KB Output is correct
4 Correct 89 ms 2944 KB Output is correct
5 Correct 75 ms 2936 KB Output is correct
6 Correct 41 ms 2944 KB Output is correct
7 Correct 18 ms 2944 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 219 ms 4860 KB Output is correct
10 Correct 237 ms 5112 KB Output is correct
11 Correct 253 ms 5112 KB Output is correct
12 Correct 302 ms 5240 KB Output is correct
13 Correct 256 ms 5240 KB Output is correct
14 Correct 256 ms 5240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 2936 KB Output is correct
2 Correct 37 ms 3064 KB Output is correct
3 Correct 233 ms 4572 KB Output is correct
4 Correct 31 ms 2944 KB Output is correct
5 Correct 274 ms 4856 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 238 ms 4984 KB Output is correct
9 Correct 239 ms 4984 KB Output is correct
10 Correct 260 ms 5112 KB Output is correct
11 Correct 255 ms 4984 KB Output is correct
12 Correct 256 ms 4984 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 276 ms 4984 KB Output is correct
20 Correct 281 ms 5112 KB Output is correct
21 Correct 269 ms 4984 KB Output is correct
22 Correct 257 ms 5112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 2944 KB Output is correct
2 Correct 23 ms 2944 KB Output is correct
3 Correct 91 ms 2992 KB Output is correct
4 Correct 94 ms 3064 KB Output is correct
5 Correct 7 ms 896 KB Output is correct
6 Correct 32 ms 2944 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 99 ms 2936 KB Output is correct
11 Correct 99 ms 2936 KB Output is correct
12 Correct 103 ms 3068 KB Output is correct
13 Correct 50 ms 2944 KB Output is correct
14 Correct 76 ms 2936 KB Output is correct
15 Correct 66 ms 2944 KB Output is correct
16 Correct 77 ms 3068 KB Output is correct
17 Correct 303 ms 4860 KB Output is correct
18 Correct 295 ms 4984 KB Output is correct
19 Correct 292 ms 4856 KB Output is correct
20 Correct 89 ms 2944 KB Output is correct
21 Correct 75 ms 2936 KB Output is correct
22 Correct 41 ms 2944 KB Output is correct
23 Correct 18 ms 2944 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
25 Correct 219 ms 4860 KB Output is correct
26 Correct 237 ms 5112 KB Output is correct
27 Correct 253 ms 5112 KB Output is correct
28 Correct 302 ms 5240 KB Output is correct
29 Correct 256 ms 5240 KB Output is correct
30 Correct 256 ms 5240 KB Output is correct
31 Correct 88 ms 2936 KB Output is correct
32 Correct 37 ms 3064 KB Output is correct
33 Correct 233 ms 4572 KB Output is correct
34 Correct 31 ms 2944 KB Output is correct
35 Correct 274 ms 4856 KB Output is correct
36 Correct 5 ms 384 KB Output is correct
37 Correct 5 ms 384 KB Output is correct
38 Correct 238 ms 4984 KB Output is correct
39 Correct 239 ms 4984 KB Output is correct
40 Correct 260 ms 5112 KB Output is correct
41 Correct 255 ms 4984 KB Output is correct
42 Correct 256 ms 4984 KB Output is correct
43 Correct 5 ms 384 KB Output is correct
44 Correct 5 ms 384 KB Output is correct
45 Correct 5 ms 384 KB Output is correct
46 Correct 5 ms 384 KB Output is correct
47 Correct 5 ms 384 KB Output is correct
48 Correct 5 ms 384 KB Output is correct
49 Correct 276 ms 4984 KB Output is correct
50 Correct 281 ms 5112 KB Output is correct
51 Correct 269 ms 4984 KB Output is correct
52 Correct 257 ms 5112 KB Output is correct
53 Correct 321 ms 5112 KB Output is correct
54 Correct 314 ms 5240 KB Output is correct
55 Correct 306 ms 5112 KB Output is correct
56 Correct 92 ms 2936 KB Output is correct
57 Correct 95 ms 3064 KB Output is correct
58 Correct 289 ms 4732 KB Output is correct
59 Correct 335 ms 4728 KB Output is correct
60 Correct 292 ms 4728 KB Output is correct
61 Correct 299 ms 4724 KB Output is correct
62 Correct 291 ms 4640 KB Output is correct
63 Correct 282 ms 4728 KB Output is correct
64 Execution timed out 1092 ms 5320 KB Time limit exceeded
65 Halted 0 ms 0 KB -