답안 #625831

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
625831 2022-08-10T20:36:11 Z Arsa Olympic Bus (JOI20_ho_t4) C++14
100 / 100
848 ms 5444 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;

const int N = 310;
const int M = 5e4+10;
const ll INF = 1e16;

ll n,m;
vector <ll> G[N];
vector <ll> vec[2];
ll dis[N][N][2];
ll path0, path1;
ll par[N];
bool hame[M][2];
ll ans;

void Input();
void Dijkstra(int,int,int);
void Halat0_0();
void Halat1_0();
void Halat0_1();
void Path_0();
void Path_1();

struct edge{
	ll u, v, w, c;		
};

edge e[M];

int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	Input();
	
	Dijkstra(0, -1, 0);
	if(dis[0][n-1][0] != INF)
		Path_0();
	path0 = dis[0][n-1][0];
	
	Dijkstra(n-1, -1, 0);
	if(dis[n-1][0][0] != INF)
		Path_1();
	path1 = dis[n-1][0][0];
	
	ans = path0 + path1;
	
	for(int i = 1;i < n-1;i++)
		Dijkstra(i, -1, 0);
	
	Halat0_0();
	Halat0_1();
	Halat1_0();
	
	if(ans >= INF)
		cout << -1 << endl;
	else
		cout << ans << endl;	
}

void Input(){
	cin >> n >> m;
	for(int i = 0;i < m;i++){
		ll u, v, w, c;
		cin >> u >> v >> w >> c, u--, v--;
		e[i].u = u, e[i].v = v, e[i].w = w, e[i].c = c;
		G[u].push_back(i), G[v].push_back(i);
	}
}

void Dijkstra(int s,int swp,int type){
	set<pii> st;
	bool mark[N];
	fill(mark, mark + n + 10, 0);
	
	for(int i = 0;i < n;i++)
		dis[s][i][type] = INF;
	dis[s][s][type] = 0, par[s] = -1;
	st.insert({0, s});
	
	while(st.size()){
		int a = st.begin() -> second;
		st.erase(st.begin());
		if(!mark[a]){
			mark[a] = true;
			for(auto i : G[a]){
				ll w = e[i].w, u = e[i].u, v = e[i].v; 
				if(a == u and i != swp){
					if(dis[s][v][type] > dis[s][u][type] + w)
						dis[s][v][type] = dis[s][u][type] + w, par[v] = i, st.insert({dis[s][v][type], v});	
				}else if(a == v and i == swp){
					if(dis[s][u][type] > dis[s][v][type] + w)
						dis[s][u][type] = dis[s][v][type] + w, par[u] = i, st.insert({dis[s][u][type], u});
				}
			}
		}
	}
}

void Path_0(){
	int v = n-1;
	while(par[v] != -1){
		vec[0].push_back(par[v]);
		hame[par[v]][0] = true;
		v = e[par[v]].u;
	}
}

void Path_1(){
	int v = 0;
	while(par[v] != -1){
		vec[1].push_back(par[v]);
		hame[par[v]][1] = true;
		v = e[par[v]].u;
	}
}

void Halat0_0(){
	for(int i = 0;i < m;i++){
		ll u = e[i].u, v = e[i].v, w = e[i].w, cost = e[i].c;
		if(dis[0][v][0] + w + dis[u][n-1][0] < path0)
			if(dis[n-1][v][0] + w + dis[u][0][0] < path1)
				ans = min(ans, dis[0][v][0] + w + dis[u][n-1][0] + dis[n-1][v][0] + w + dis[u][0][0] + cost);
	}
}

void Halat0_1(){
	for(int i = 0;i < m;i++){
		ll u = e[i].u, v = e[i].v, w = e[i].w, cost = e[i].c;
		if(dis[0][v][0] + w + dis[u][n-1][0] < path0){
			if(!hame[i][1])
				ans = min(ans, dis[0][v][0] + w + dis[u][n-1][0] + path1 + cost);
			else{
				Dijkstra(n-1, i, 1);
				ans = min(ans, dis[0][v][0] + w + dis[u][n-1][0] + dis[n-1][0][1] + cost);
			}
		}
	}
}

void Halat1_0(){
	for(int i = 0;i < m;i++){
		ll u = e[i].u, v = e[i].v, w = e[i].w, cost = e[i].c;
		if(dis[n-1][v][0] + w + dis[u][0][0] < path1){
			if(!hame[i][0])
				ans = min(ans, dis[n-1][v][0] + w + dis[u][0][0]  + path0 + cost);
			else{
				Dijkstra(0, i, 1);
				ans = min(ans, dis[n-1][v][0] + w + dis[u][0][0]  + dis[0][n-1][1] + cost);
			}
		}
	}	
}


# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 1364 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
3 Correct 12 ms 1384 KB Output is correct
4 Correct 13 ms 1380 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 1236 KB Output is correct
7 Correct 0 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 17 ms 1364 KB Output is correct
11 Correct 24 ms 1344 KB Output is correct
12 Correct 23 ms 1300 KB Output is correct
13 Correct 8 ms 1364 KB Output is correct
14 Correct 9 ms 1320 KB Output is correct
15 Correct 9 ms 1364 KB Output is correct
16 Correct 9 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 172 ms 4132 KB Output is correct
2 Correct 167 ms 4172 KB Output is correct
3 Correct 167 ms 4084 KB Output is correct
4 Correct 14 ms 1428 KB Output is correct
5 Correct 8 ms 1320 KB Output is correct
6 Correct 3 ms 1236 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 115 ms 4176 KB Output is correct
10 Correct 113 ms 4172 KB Output is correct
11 Correct 143 ms 4272 KB Output is correct
12 Correct 130 ms 4188 KB Output is correct
13 Correct 125 ms 4256 KB Output is correct
14 Correct 123 ms 4324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 1280 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
3 Correct 146 ms 3428 KB Output is correct
4 Correct 2 ms 1236 KB Output is correct
5 Correct 185 ms 4160 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 131 ms 4128 KB Output is correct
9 Correct 124 ms 4164 KB Output is correct
10 Correct 143 ms 4248 KB Output is correct
11 Correct 155 ms 4984 KB Output is correct
12 Correct 148 ms 5232 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 152 ms 5224 KB Output is correct
20 Correct 143 ms 5080 KB Output is correct
21 Correct 140 ms 5096 KB Output is correct
22 Correct 141 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 1364 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
3 Correct 12 ms 1384 KB Output is correct
4 Correct 13 ms 1380 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 1236 KB Output is correct
7 Correct 0 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 17 ms 1364 KB Output is correct
11 Correct 24 ms 1344 KB Output is correct
12 Correct 23 ms 1300 KB Output is correct
13 Correct 8 ms 1364 KB Output is correct
14 Correct 9 ms 1320 KB Output is correct
15 Correct 9 ms 1364 KB Output is correct
16 Correct 9 ms 1364 KB Output is correct
17 Correct 172 ms 4132 KB Output is correct
18 Correct 167 ms 4172 KB Output is correct
19 Correct 167 ms 4084 KB Output is correct
20 Correct 14 ms 1428 KB Output is correct
21 Correct 8 ms 1320 KB Output is correct
22 Correct 3 ms 1236 KB Output is correct
23 Correct 1 ms 1236 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 115 ms 4176 KB Output is correct
26 Correct 113 ms 4172 KB Output is correct
27 Correct 143 ms 4272 KB Output is correct
28 Correct 130 ms 4188 KB Output is correct
29 Correct 125 ms 4256 KB Output is correct
30 Correct 123 ms 4324 KB Output is correct
31 Correct 9 ms 1280 KB Output is correct
32 Correct 2 ms 1236 KB Output is correct
33 Correct 146 ms 3428 KB Output is correct
34 Correct 2 ms 1236 KB Output is correct
35 Correct 185 ms 4160 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 131 ms 4128 KB Output is correct
39 Correct 124 ms 4164 KB Output is correct
40 Correct 143 ms 4248 KB Output is correct
41 Correct 155 ms 4984 KB Output is correct
42 Correct 148 ms 5232 KB Output is correct
43 Correct 0 ms 468 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 152 ms 5224 KB Output is correct
50 Correct 143 ms 5080 KB Output is correct
51 Correct 140 ms 5096 KB Output is correct
52 Correct 141 ms 4992 KB Output is correct
53 Correct 231 ms 5300 KB Output is correct
54 Correct 219 ms 5168 KB Output is correct
55 Correct 213 ms 5076 KB Output is correct
56 Correct 12 ms 1364 KB Output is correct
57 Correct 12 ms 1412 KB Output is correct
58 Correct 191 ms 4368 KB Output is correct
59 Correct 267 ms 4320 KB Output is correct
60 Correct 337 ms 4404 KB Output is correct
61 Correct 207 ms 4352 KB Output is correct
62 Correct 253 ms 4300 KB Output is correct
63 Correct 339 ms 4388 KB Output is correct
64 Correct 507 ms 4828 KB Output is correct
65 Correct 656 ms 5152 KB Output is correct
66 Correct 848 ms 4952 KB Output is correct
67 Correct 13 ms 3272 KB Output is correct
68 Correct 155 ms 5316 KB Output is correct
69 Correct 157 ms 5236 KB Output is correct
70 Correct 181 ms 5196 KB Output is correct
71 Correct 164 ms 5204 KB Output is correct
72 Correct 190 ms 5228 KB Output is correct
73 Correct 178 ms 5444 KB Output is correct
74 Correct 180 ms 5408 KB Output is correct