답안 #927089

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
927089 2024-02-14T08:57:22 Z socpite 한자 끝말잇기 (JOI14_kanji) C++14
100 / 100
129 ms 14612 KB
#include "Annalib.h"
#include<bits/stdc++.h>
using namespace std;

const long long INF = LLONG_MAX/2;
const int maxn = 305;

namespace anna{
  	vector<pair<int, int>> g[maxn];

	vector<long long> shortest_path(int N, int s, long long C[]){
		vector<long long> d(N, INF);
		vector<int> vis(N, 0);
		d[s] = 0;
		for(int i = 1; i <= N; i++){
			long long best = INF;
			int x = -1;
			for(int j = 0; j < N; j++){
				if(vis[j])continue;
				if(d[j] < best){
					best = d[j];
					x = j;
				}
			}
			if(x == -1)break;
			vis[x] = 1;
			for(auto v: g[x]) if(d[v.first] > C[v.second] + best)d[v.first] = C[v.second] + best;
		}
		return d;
	}

	

	void solve(int K, int Q, vector<long long>& forgor_C, vector<vector<long long>>& cost_to_T){
		// eval(i, x) = forgor(x) + cost_to_T
		vector<vector<int>> queries(K+1);
		for(int i = 0; i < Q; i++)queries[0].push_back(i);
		unsigned long long total_case = 1;
		vector<pair<int, int>> cc;
		for(int b = 1; b <= K; b++){
			for(int a = 0; a < b; a++){
				vector<int> a_wins;
				for(auto v: queries[a]){
					if(forgor_C[a] + cost_to_T[a][v] < forgor_C[b] + cost_to_T[b][v])a_wins.push_back(v);
					else queries[b].push_back(v);
				}
				int cases = queries[a].size() + 1, num = a_wins.size();
				total_case *= cases;
				queries[a] = a_wins;
				cc.push_back({num, cases});
			}
		}
		// cout << cc.size() << endl;
		unsigned long long crr = 0;
		while(!cc.empty()){
			crr = crr*cc.back().second;
			crr = crr+cc.back().first;
			cc.pop_back();
		}
		while(total_case > 1){
			Tap(crr&1);
			total_case = (total_case+1)>>1;
			crr>>=1;
		}
	}
}


void Anna(int N, int M, int A[], int B[], long long C[], int Q, int S[], int T[], int K, int U[]) {
	vector<long long> forgor_C(K+1, 0);
	vector<vector<long long>> cost_to_T(K+1, vector<long long>(Q));
	for(int i = 0; i < K; i++){
		forgor_C[i] = C[U[i]];
	}	
	for(int i = 0; i < K; i++)C[U[i]] = INF;
	for(int i = 0; i < M; i++){
		anna::g[A[i]].push_back({B[i], i});
	}
	for(int i = 0; i < K; i++){
		auto d = anna::shortest_path(N, B[U[i]], C);
		for(int j = 0; j < Q; j++)cost_to_T[i][j] = d[T[j]];
	}
	for(int i = 0; i < Q; i++){
		auto d = anna::shortest_path(N, S[i], C);
		for(int j = 0; j < K; j++)cost_to_T[j][i] += d[A[U[j]]];
		cost_to_T[K][i] = d[T[i]];
	}
	anna::solve(K, Q, forgor_C, cost_to_T);
	// eval(i, x) = forgor(x) + cost_to_A + cost_to_T



}
#include "Brunolib.h"
#include<bits/stdc++.h>
using namespace std;


const int maxn = 305;
const long long INF = 1e16*maxn;

namespace bruno {
  	vector<pair<int, int>> g[maxn];

	vector<long long> shortest_path(int N, int s, long long C[]){
		vector<long long> d(N, INF);
		vector<int> vis(N, 0);
		d[s] = 0;
		for(int i = 1; i <= N; i++){
			long long best = INF;
			int x = -1;
			for(int j = 0; j < N; j++){
				if(vis[j])continue;
				if(d[j] < best){
					best = d[j];
					x = j;
				}
			}
			if(x == -1)break;
			vis[x] = 1;
			for(auto v: g[x]) if(d[v.first] > C[v.second] + best)d[v.first] = C[v.second] + best;
		}
		return d;
	}

	vector<int> path[60];

	vector<int> solve_query(int N, int s, int t, long long C[]){
		vector<long long> d(N, INF);
		vector<int> vis(N, 0);
		vector<pair<int, int>> trace(N);
		d[s] = 0;
		for(int i = 1; i <= N; i++){
			long long best = INF;
			int x = -1;
			for(int j = 0; j < N; j++){
				if(vis[j])continue;
				if(d[j] < best){
					best = d[j];
					x = j;
				}
			}
			if(x == -1)break;
			vis[x] = 1;
			for(auto v: g[x]) if(d[v.first] > C[v.second] + best){
				d[v.first] = C[v.second] + best;
				trace[v.first] = {x, v.second};
			}
		}
		vector<int> ans;
		while(t != s){
			ans.push_back(trace[t].second);
			t = trace[t].first;
		}
		reverse(ans.begin(), ans.end());
		return ans;
	}

	vector<vector<int>> solve(int K, int Q, vector<vector<long long>>& cost_to_T, int L, int X[]){
		unsigned long long crr = 0;
		for(int i = 0; i < L; i++)crr ^= (1ULL*X[i])<<i;
		vector<vector<int>> queries(K+1);
		for(int i = 0; i < Q; i++)queries[0].push_back(i);
		for(int b = 1; b <= K; b++){
			for(int a = 0; a < b; a++){
				auto cmp = [&a, &b, &cost_to_T](int lhs, int rhs){
					return cost_to_T[a][lhs] - cost_to_T[b][lhs] < cost_to_T[a][rhs] - cost_to_T[b][rhs];  
				};
				int cases = queries[a].size() + 1;
				sort(queries[a].begin(), queries[a].end(), cmp);
				while(queries[a].size() > crr%cases){
					queries[b].push_back(queries[a].back());
					queries[a].pop_back();
				}
				crr/=cases;
			}
		}
		return queries;
	}
}

void Bruno(int N, int M, int A[], int B[], long long C[], int Q, int S[], int T[], int K, int U[], int L, int X[]) {
	vector<vector<long long>> cost_to_T(K+1, vector<long long>(Q));
	for(int i = 0; i < K; i++)C[U[i]] = INF;
	for(int i = 0; i < M; i++){
		bruno::g[A[i]].push_back({B[i], i});
	}
	for(int i = 0; i < K; i++){
		auto d = bruno::shortest_path(N, B[U[i]], C);
		for(int j = 0; j < Q; j++)cost_to_T[i][j] = d[T[j]];
	}
	for(int i = 0; i < Q; i++){
		auto d = bruno::shortest_path(N, S[i], C);
		for(int j = 0; j < K; j++)cost_to_T[j][i] += d[A[U[j]]];
		cost_to_T[K][i] = d[T[i]];
	};
	auto qtypes = bruno::solve(K, Q, cost_to_T, L, X);
	for(int i = 0; i <= K; i++){
		if(i < K)C[U[i]] = 0;
		for(auto v: qtypes[i]){
			bruno::path[v] = bruno::solve_query(N, S[v], T[v], C);
			if(i == K)for(auto ele: bruno::path[v]){
				for(int j = 0; j < K; j++)assert(ele != U[j]);
			}
		}
		if(i < K)C[U[i]] = INF;
	}
	for(int i = 0; i < Q; i++){
		for(auto v: bruno::path[i])Answer(v);
		Answer(-1);
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4392 KB Output is correct - L = 27
2 Correct 10 ms 4396 KB Output is correct - L = 22
3 Correct 7 ms 4400 KB Output is correct - L = 24
4 Correct 8 ms 4384 KB Output is correct - L = 25
5 Correct 7 ms 4380 KB Output is correct - L = 26
6 Correct 7 ms 4384 KB Output is correct - L = 25
7 Correct 8 ms 4664 KB Output is correct - L = 29
8 Correct 9 ms 4408 KB Output is correct - L = 18
9 Correct 10 ms 4424 KB Output is correct - L = 18
10 Correct 12 ms 4720 KB Output is correct - L = 18
11 Correct 4 ms 4388 KB Output is correct - L = 5
12 Correct 74 ms 14152 KB Output is correct - L = 18
13 Correct 5 ms 4392 KB Output is correct - L = 28
14 Correct 4 ms 4396 KB Output is correct - L = 1
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 4900 KB Output is correct - L = 62
2 Correct 28 ms 4396 KB Output is correct - L = 62
3 Correct 27 ms 5160 KB Output is correct - L = 52
4 Correct 32 ms 4900 KB Output is correct - L = 54
5 Correct 24 ms 4396 KB Output is correct - L = 62
6 Correct 28 ms 4916 KB Output is correct - L = 61
7 Correct 26 ms 5156 KB Output is correct - L = 45
8 Correct 28 ms 4904 KB Output is correct - L = 61
9 Correct 10 ms 4912 KB Output is correct - L = 64
10 Correct 10 ms 4912 KB Output is correct - L = 64
11 Correct 11 ms 4908 KB Output is correct - L = 64
12 Correct 32 ms 4904 KB Output is correct - L = 60
13 Correct 124 ms 14600 KB Output is correct - L = 33
14 Correct 28 ms 4904 KB Output is correct - L = 60
15 Correct 28 ms 4912 KB Output is correct - L = 52
16 Correct 34 ms 4936 KB Output is correct - L = 34
17 Correct 40 ms 5052 KB Output is correct - L = 39
18 Correct 37 ms 5336 KB Output is correct - L = 46
19 Correct 12 ms 4392 KB Output is correct - L = 42
20 Correct 30 ms 5908 KB Output is correct - L = 55
21 Correct 41 ms 5908 KB Output is correct - L = 30
22 Correct 10 ms 5028 KB Output is correct - L = 64
23 Correct 10 ms 4908 KB Output is correct - L = 64
24 Correct 11 ms 4920 KB Output is correct - L = 64
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 4900 KB Output is correct - L = 62
2 Correct 28 ms 4392 KB Output is correct - L = 62
3 Correct 25 ms 4900 KB Output is correct - L = 52
4 Correct 28 ms 4900 KB Output is correct - L = 54
5 Correct 24 ms 4500 KB Output is correct - L = 62
6 Correct 28 ms 4904 KB Output is correct - L = 61
7 Correct 27 ms 4912 KB Output is correct - L = 45
8 Correct 28 ms 4912 KB Output is correct - L = 61
9 Correct 10 ms 4912 KB Output is correct - L = 64
10 Correct 10 ms 4912 KB Output is correct - L = 64
11 Correct 10 ms 4920 KB Output is correct - L = 64
12 Correct 30 ms 4912 KB Output is correct - L = 60
13 Correct 118 ms 14612 KB Output is correct - L = 33
14 Correct 28 ms 4912 KB Output is correct - L = 60
15 Correct 28 ms 4912 KB Output is correct - L = 52
16 Correct 34 ms 4732 KB Output is correct - L = 34
17 Correct 36 ms 5140 KB Output is correct - L = 39
18 Correct 37 ms 5336 KB Output is correct - L = 46
19 Correct 12 ms 4652 KB Output is correct - L = 42
20 Correct 33 ms 6112 KB Output is correct - L = 55
21 Correct 41 ms 5816 KB Output is correct - L = 30
22 Correct 11 ms 4968 KB Output is correct - L = 64
23 Correct 10 ms 4920 KB Output is correct - L = 64
24 Correct 10 ms 4920 KB Output is correct - L = 64
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 4936 KB Output is correct - L = 62
2 Correct 28 ms 4388 KB Output is correct - L = 62
3 Correct 30 ms 5372 KB Output is correct - L = 52
4 Correct 28 ms 4896 KB Output is correct - L = 54
5 Correct 24 ms 4400 KB Output is correct - L = 62
6 Correct 28 ms 4904 KB Output is correct - L = 61
7 Correct 26 ms 4904 KB Output is correct - L = 45
8 Correct 28 ms 4908 KB Output is correct - L = 61
9 Correct 11 ms 4912 KB Output is correct - L = 64
10 Correct 10 ms 4912 KB Output is correct - L = 64
11 Correct 10 ms 4916 KB Output is correct - L = 64
12 Correct 28 ms 4920 KB Output is correct - L = 60
13 Correct 118 ms 14368 KB Output is correct - L = 33
14 Correct 28 ms 4912 KB Output is correct - L = 60
15 Correct 28 ms 4948 KB Output is correct - L = 52
16 Correct 37 ms 5188 KB Output is correct - L = 34
17 Correct 35 ms 4980 KB Output is correct - L = 39
18 Correct 38 ms 5336 KB Output is correct - L = 46
19 Correct 13 ms 4392 KB Output is correct - L = 42
20 Correct 31 ms 5920 KB Output is correct - L = 55
21 Correct 43 ms 5884 KB Output is correct - L = 30
22 Correct 10 ms 4916 KB Output is correct - L = 64
23 Correct 10 ms 4912 KB Output is correct - L = 64
24 Correct 10 ms 4964 KB Output is correct - L = 64
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 4908 KB Output is correct - L = 62
2 Correct 28 ms 4620 KB Output is correct - L = 62
3 Correct 25 ms 4908 KB Output is correct - L = 52
4 Correct 28 ms 4896 KB Output is correct - L = 54
5 Correct 24 ms 4392 KB Output is correct - L = 62
6 Correct 32 ms 5180 KB Output is correct - L = 61
7 Correct 27 ms 5156 KB Output is correct - L = 45
8 Correct 30 ms 4944 KB Output is correct - L = 61
9 Correct 10 ms 4912 KB Output is correct - L = 64
10 Correct 10 ms 4912 KB Output is correct - L = 64
11 Correct 10 ms 4912 KB Output is correct - L = 64
12 Correct 31 ms 4900 KB Output is correct - L = 60
13 Correct 129 ms 14392 KB Output is correct - L = 33
14 Correct 28 ms 4912 KB Output is correct - L = 60
15 Correct 27 ms 4912 KB Output is correct - L = 52
16 Correct 34 ms 4936 KB Output is correct - L = 34
17 Correct 36 ms 5044 KB Output is correct - L = 39
18 Correct 38 ms 5488 KB Output is correct - L = 46
19 Correct 12 ms 4400 KB Output is correct - L = 42
20 Correct 30 ms 5960 KB Output is correct - L = 55
21 Correct 41 ms 5888 KB Output is correct - L = 30
22 Correct 10 ms 4908 KB Output is correct - L = 64
23 Correct 10 ms 4976 KB Output is correct - L = 64
24 Correct 10 ms 4920 KB Output is correct - L = 64