답안 #635436

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635436 2022-08-26T09:08:39 Z S2speed LOSTIKS (INOI20_lostiks) C++17
59 / 100
2000 ms 247404 KB
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize ("Ofast")

#define sze(x) (int)(x.size())
typedef long long ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;
typedef pair<pii , int> piii;

const ll maxn = (1 << 20) + 17 , inf = 2e8;

int n , s , t;
pii adj[maxn << 1];
vector<piii> ed;
int dg[maxn] , st[maxn] , ft[maxn] , x[maxn];
int dis[22][maxn];
int bfs[maxn] , sz = 0;
int g[maxn] , k[22] , d[22] , e[22] , f[22][22] , dp[maxn][22] , ind[22];

void rBFS(int r , int h){
	sz = 0;
	dis[h][r] = 0;
	g[r] = 0;
	bfs[sz++] = r;
	int x = 0;
	while(x < sz){
		int v = bfs[x++];
		for(int e = st[v] ; e < ft[v] ; e++){
			pii p = adj[e];
			int i = p.first , t = p.second;
			if(dis[h][i] < dis[h][v] + 1) continue;
			g[i] = g[v];
			if(t != -1){
				g[i] ^= (1 << t);
				d[t] = v;
			}
			dis[h][i] = dis[h][v] + 1;
			bfs[sz++] = i;
		}
	}
	return;
}

void BFS(int r , int h){
	sz = 0;
	dis[h][r] = 0;
	g[r] = 0;
	bfs[sz++] = r;
	int x = 0;
	while(x < sz){
		int v = bfs[x++];
		for(int e = st[v] ; e < ft[v] ; e++){
			pii p = adj[e];
			int i = p.first , t = p.second;
			if(dis[h][i] < dis[h][v] + 1) continue;
			dis[h][i] = dis[h][v] + 1;
			bfs[sz++] = i;
		}
	}
	return;
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	memset(dp , 63 , sizeof(dp));
	memset(dis , 63 , sizeof(dis));
	cin>>n>>s>>t; s--; t--;
	int m = 0;
	for(int i = 1 ; i < n ; i++){
		int v , u , w;
		cin>>v>>u>>w; v--; u--; w--;
		dg[v]++; dg[u]++;
		if(w != -1){
			k[m] = w;
			ed.push_back({{v , u} , m});
			m++;
		} else {
			ed.push_back({{v , u} , -1});
		}
	}
	x[0] = st[0] = 0; ft[0] = dg[0];
	for(int i = 1 ; i < n ; i++){
		x[i] = st[i] = ft[i - 1]; ft[i] = st[i] + dg[i];
	}
	for(auto p : ed){
		int v = p.first.first , u = p.first.second , w = p.second;
		adj[x[v]++] = {u , w}; adj[x[u]++] = {v , w};
	}
	rBFS(s , m);
	if(g[t] == 0){
		cout<<dis[m][t]<<'\n';
		return 0;
	}
	for(int j = 0 ; j < m ; j++){
		e[j] = g[k[j]] | g[d[j]];
		BFS(d[j] , j);
		if(e[j] == 0){
			dp[(1 << j)][j] = dis[m][k[j]] + dis[j][k[j]];
		}
	}
	for(int i = 0 ; i < m ; i++){
		for(int j = 0 ; j < m ; j++){
			f[i][j] = dis[i][k[j]] + dis[j][k[j]];
		}
	}
	int lm = (1 << m);
	for(int mask = 3 ; mask < lm ; mask++){
		int bp = 0;
		for(int j = 0 ; j < m ; j++){
			if(mask & (1 << j)) ind[bp++] = j;
		}
		if(bp == 1) continue;
		for(int jj = 0 ; jj < bp ; jj++){
			int j = ind[jj];
			if((e[j] & mask) != e[j]) continue;
			int h = inf , msk = mask ^ (1 << j);
			for(int ii = 0 ; ii < bp ; ii++){
				int i = ind[ii];
				if(i == j) continue;
				h = min(h , dp[msk][i] + f[i][j]);
			}
			dp[mask][j] = h;
		}
	}
	int ans = inf;
	for(int mask = 1 ; mask < lm ; mask++){
		if((g[t] & mask) != g[t]) continue;
		int h = inf;
		for(int i = 0 ; i < m ; i++){
			if(!(mask & (1 << i))) continue;
//			cout<<mask<<' '<<i<<'\n';
			h = min(h , dp[mask][i] + dis[i][t]);
		}
		ans = min(ans , h);
	}
	cout<<(ans == inf ? -1 : ans)<<'\n';
	return 0;
}

Compilation message

Main.cpp: In function 'void BFS(int, int)':
Main.cpp:57:22: warning: unused variable 't' [-Wunused-variable]
   57 |    int i = p.first , t = p.second;
      |                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 180812 KB Output is correct
2 Correct 76 ms 180924 KB Output is correct
3 Correct 114 ms 186184 KB Output is correct
4 Correct 113 ms 186096 KB Output is correct
5 Correct 111 ms 186108 KB Output is correct
6 Correct 110 ms 186188 KB Output is correct
7 Correct 111 ms 186120 KB Output is correct
8 Correct 118 ms 186348 KB Output is correct
9 Correct 113 ms 186092 KB Output is correct
10 Correct 107 ms 186112 KB Output is correct
11 Correct 114 ms 186132 KB Output is correct
12 Correct 115 ms 186136 KB Output is correct
13 Correct 114 ms 186148 KB Output is correct
14 Correct 121 ms 186112 KB Output is correct
15 Correct 115 ms 186236 KB Output is correct
16 Correct 122 ms 186120 KB Output is correct
17 Correct 120 ms 186172 KB Output is correct
18 Correct 121 ms 186180 KB Output is correct
19 Correct 118 ms 186080 KB Output is correct
20 Correct 116 ms 186160 KB Output is correct
21 Correct 115 ms 186088 KB Output is correct
22 Correct 69 ms 180888 KB Output is correct
23 Correct 70 ms 180860 KB Output is correct
24 Correct 68 ms 180828 KB Output is correct
25 Correct 66 ms 180876 KB Output is correct
26 Correct 68 ms 180944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 180840 KB Output is correct
2 Correct 67 ms 180908 KB Output is correct
3 Correct 70 ms 180860 KB Output is correct
4 Correct 67 ms 180948 KB Output is correct
5 Correct 156 ms 181372 KB Output is correct
6 Correct 133 ms 181388 KB Output is correct
7 Correct 172 ms 181404 KB Output is correct
8 Correct 138 ms 181332 KB Output is correct
9 Correct 143 ms 181324 KB Output is correct
10 Correct 114 ms 181392 KB Output is correct
11 Correct 115 ms 181364 KB Output is correct
12 Correct 128 ms 181400 KB Output is correct
13 Correct 117 ms 181412 KB Output is correct
14 Correct 115 ms 181400 KB Output is correct
15 Correct 138 ms 181288 KB Output is correct
16 Correct 163 ms 181452 KB Output is correct
17 Correct 143 ms 181340 KB Output is correct
18 Correct 118 ms 181304 KB Output is correct
19 Correct 116 ms 181316 KB Output is correct
20 Correct 118 ms 181380 KB Output is correct
21 Correct 124 ms 181360 KB Output is correct
22 Correct 120 ms 181408 KB Output is correct
23 Correct 115 ms 181360 KB Output is correct
24 Correct 114 ms 181408 KB Output is correct
25 Correct 364 ms 180936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 180812 KB Output is correct
2 Correct 76 ms 180924 KB Output is correct
3 Correct 114 ms 186184 KB Output is correct
4 Correct 113 ms 186096 KB Output is correct
5 Correct 111 ms 186108 KB Output is correct
6 Correct 110 ms 186188 KB Output is correct
7 Correct 111 ms 186120 KB Output is correct
8 Correct 118 ms 186348 KB Output is correct
9 Correct 113 ms 186092 KB Output is correct
10 Correct 107 ms 186112 KB Output is correct
11 Correct 114 ms 186132 KB Output is correct
12 Correct 115 ms 186136 KB Output is correct
13 Correct 114 ms 186148 KB Output is correct
14 Correct 121 ms 186112 KB Output is correct
15 Correct 115 ms 186236 KB Output is correct
16 Correct 122 ms 186120 KB Output is correct
17 Correct 120 ms 186172 KB Output is correct
18 Correct 121 ms 186180 KB Output is correct
19 Correct 118 ms 186080 KB Output is correct
20 Correct 116 ms 186160 KB Output is correct
21 Correct 115 ms 186088 KB Output is correct
22 Correct 69 ms 180888 KB Output is correct
23 Correct 70 ms 180860 KB Output is correct
24 Correct 68 ms 180828 KB Output is correct
25 Correct 66 ms 180876 KB Output is correct
26 Correct 68 ms 180944 KB Output is correct
27 Correct 67 ms 180840 KB Output is correct
28 Correct 67 ms 180908 KB Output is correct
29 Correct 70 ms 180860 KB Output is correct
30 Correct 67 ms 180948 KB Output is correct
31 Correct 156 ms 181372 KB Output is correct
32 Correct 133 ms 181388 KB Output is correct
33 Correct 172 ms 181404 KB Output is correct
34 Correct 138 ms 181332 KB Output is correct
35 Correct 143 ms 181324 KB Output is correct
36 Correct 114 ms 181392 KB Output is correct
37 Correct 115 ms 181364 KB Output is correct
38 Correct 128 ms 181400 KB Output is correct
39 Correct 117 ms 181412 KB Output is correct
40 Correct 115 ms 181400 KB Output is correct
41 Correct 138 ms 181288 KB Output is correct
42 Correct 163 ms 181452 KB Output is correct
43 Correct 143 ms 181340 KB Output is correct
44 Correct 118 ms 181304 KB Output is correct
45 Correct 116 ms 181316 KB Output is correct
46 Correct 118 ms 181380 KB Output is correct
47 Correct 124 ms 181360 KB Output is correct
48 Correct 120 ms 181408 KB Output is correct
49 Correct 115 ms 181360 KB Output is correct
50 Correct 114 ms 181408 KB Output is correct
51 Correct 364 ms 180936 KB Output is correct
52 Correct 1284 ms 232024 KB Output is correct
53 Correct 1562 ms 232076 KB Output is correct
54 Correct 1656 ms 247404 KB Output is correct
55 Correct 1087 ms 247296 KB Output is correct
56 Correct 1463 ms 247368 KB Output is correct
57 Correct 1472 ms 247332 KB Output is correct
58 Correct 73 ms 180872 KB Output is correct
59 Correct 66 ms 180856 KB Output is correct
60 Correct 68 ms 180832 KB Output is correct
61 Correct 809 ms 247288 KB Output is correct
62 Execution timed out 2089 ms 247332 KB Time limit exceeded
63 Halted 0 ms 0 KB -