답안 #1032255

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1032255 2024-07-23T14:07:30 Z Antekb Board Game (JOI24_boardgame) C++17
68 / 100
4000 ms 18968 KB
#include<bits/stdc++.h>
 
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
 
#define st first
#define nd second
#define pb push_back
#define pp pop_back
#define eb emplace_back
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
 
using namespace std;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~~
 
template <typename H, typename T> 
ostream& operator<<(ostream& os, pair<H, T> m){
	return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H> 
ostream& operator<<(ostream& os, vector<H> V){
	os<<"{";
	for(int i=0; i<V.size(); i++){
		if(i)os<<" ";
		os<<V[i];
	}
	os<<"}";
	return os;
}
 
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#ifdef LOCAL
#define deb(x...) cerr<<#x<<" = ";debug(x);
#else
#define deb(...)
#endif
 
///~~~~~~~~~~~~~~~~~~~~~~~~~
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
 
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
 
const int N=2e5+5, INF=1e9+5, mod=1e9+7;
int typ[N], zera[N], bliska[N], dist2[N];
int licz[N];
ll tab[N], pref[N], pref2[N], dist[N];
vi E[N], co[N];
void solve(int n, int s, ll cost, bool b){
	//cost--;
	dist[s]=0;
	licz[s]=0;
	priority_queue<pair<ll, int> > Q;
	if(b==1)Q.push({0, s});
	else{
		for(int i=1; i<=n; i++)
			dist[i]+=cost*licz[i], Q.push({-dist[i], i});
	}
	while(Q.size()){
		int v=Q.top().nd;
		ll d=-Q.top().st;
		Q.pop();
		if(d!=dist[v])continue;
		for(int u:E[v]){
			if(dist[u]>dist[v]+1+typ[u]*cost){
				dist[u]=dist[v]+1+typ[u]*cost;
				licz[u]=licz[v]+typ[u];
				Q.push({-dist[u], u});
			}
		}
	}
}
int main(){
	BOOST;
	int n, m, k;
	cin>>n>>m>>k;
	//assert(n<=3000);
	for(int i=0; i<m; i++){
		int u, v;cin>>u>>v;
		E[u].pb(v);
		E[v].pb(u);
	}
	string s;
	cin>>s;
	for(int i=0; i<n; i++)typ[i+1]=s[i]-'0';
	for(int i=1; i<=n; i++){
		zera[i]=1e9;
		for(int u:E[i]){
			if(typ[u] && typ[i])zera[i]=0;
		}
	}
	vi V;
	for(int i=1; i<=n; i++){
		if(zera[i]==0)V.pb(i);
	}
	for(int i=0; i<V.size(); i++){
		int v=V[i];
		for(int u:E[v]){
			if(zera[u]>zera[v]+1-typ[u]){
				zera[u]=zera[v]+1-typ[u];
				V.pb(u);
			}
		}
	}
	V.clear();
	for(int i=1; i<=n; i++){
		if(typ[i]==0)bliska[i]=1e9;
		else V.pb(i);
	}
	for(int i=0; i<V.size(); i++){
		int v=V[i];
		for(int u:E[v]){
			if(bliska[u]==1e9){
				bliska[u]=bliska[v]+1;
				V.pb(u);
			}
		}
	}
	for(int i=1; i<=n; i++){
		deb(i, bliska[i], zera[i]);
	}
	vi X(k);
	for(int &i:X)cin>>i;
		deb(X);
	for(int i=1; i<k; i++){
		if(typ[X[i]]==1){
			/*for(int j=1; j<n; j++){
				tab[j]+=min(2*j, j+zera[X[i]]);
			}*/
			pref2[1]+=2;
			if(zera[X[i]]!=1e9)pref2[zera[X[i]]+1]--;
		}
		else{
			ll t=min(-1+bliska[X[i]], zera[X[i]]-1);
			/*for(int j=1; j<n; j++){
				//deb(j, 2*j-2+bliska[X[i]], j+zera[X[i]]);
				tab[j]+=min(j-2+bliska[X[i]], zera[X[i]]-1)-t;
			}*/
			pref2[1]+=1;
			pref[1]+=t;
			if(zera[X[i]]-1 >t){
				pref2[2]++;
				if(zera[X[i]]!=1e9){
					pref2[1+zera[X[i]]-t]--;
				}
			}
		}
	}
	for(int i=0; i<n; i++){
		pref2[i+1]+=pref2[i];
	}
	for(int i=0; i<n; i++){
		pref[i+1]+=pref[i]+pref2[i+1];
	}
	for(int i=0; i<n; i++){
		tab[i]+=pref[i];
	}
	vector<ll> ans(n+1, 1e18);
	ans[X[0]]=0;
	for(int i=1; i<=n; i++)dist[i]=1e9;
	dist[X[0]]=0;
	vector<int> changed={X[0]};
	for(int ile=0; ile<2; ile++){//ile skokow
		vi wazne;
		for(int i:changed){
			if(dist[i]!=1e9){
				co[dist[i]].pb(i);
				wazne.pb(dist[i]);
			}
		}
		sort(all(wazne));
		changed.clear();
		vii todo;
		for(int d:wazne){
			while(co[d].size()){
				for(int v:co[d]){
					for(int u:E[v]){
						if(typ[u]==0 && dist[u]>dist[v]+1){
							changed.pb(u);
							dist[u]=dist[v]+1;
							co[dist[u]].pb(u);
						}
						if(typ[u]==1 && dist[v]<dist[u]){
							todo.pb({dist[v], u});
							//dist[u]=dist[v];
							changed.pb(u);
						}
					}
				}
				co[d].clear();
				d++;
			}
		}
		sort(all(todo));
		reverse(all(todo));
		for(pii i:todo){
			dist[i.nd]=i.st;
		}
		for(int i:changed){
			if(dist[i]==1e9)continue;
			if(i==1){
				deb(ile, i, dist[i], tab[ile], typ[i],dist[i]+tab[ile]+typ[i]+ile);
			}
			if(ile<2)
				ans[i]=min(ans[i], dist[i]+tab[ile]+typ[i]+ile);
		}
	}
	for(int i=1; i<=n; i++){
		dist[i]=1e9;
		dist2[i]=1e9;
	}
	for(int i=1; i<=n; i++)dist[i]=1e18;
	for(int i=1; i<n; i++){
		deb(tab[i]);
		if(i>1 && (i==2 || tab[i]-tab[i-1]!=tab[i-1]-tab[i-2])){
			solve(n, X[0], tab[i]-tab[i-1], i==2);
			ll cost=tab[i]-i*(tab[i]-tab[i-1]);
			deb(i, dist[1], cost, tab[i]-tab[i-1]);
			for(int j=1; j<=n; j++){
				if(licz[j]-typ[j]>=i-1)
					ans[j]=min(ans[j], dist[j]+cost-typ[j]*(tab[i]-tab[i-1]));
				dist[j]-=licz[j]*(tab[i]-tab[i-1]);
			}
		}
	}
	ans[X[0]]=0;
	for(int i=1; i<=n; i++){
		cout<<ans[i]<<"\n";
	}
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:114:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |  for(int i=0; i<V.size(); i++){
      |               ~^~~~~~~~~
Main.cpp:128:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |  for(int i=0; i<V.size(); i++){
      |               ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10072 KB Output is correct
2 Correct 21 ms 14044 KB Output is correct
3 Correct 31 ms 16844 KB Output is correct
4 Correct 27 ms 16152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10076 KB Output is correct
2 Correct 23 ms 13908 KB Output is correct
3 Correct 30 ms 16060 KB Output is correct
4 Correct 30 ms 16536 KB Output is correct
5 Correct 32 ms 16308 KB Output is correct
6 Correct 30 ms 16140 KB Output is correct
7 Correct 28 ms 15140 KB Output is correct
8 Correct 31 ms 15788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10328 KB Output is correct
2 Correct 21 ms 13796 KB Output is correct
3 Correct 30 ms 16148 KB Output is correct
4 Correct 31 ms 16584 KB Output is correct
5 Correct 32 ms 16808 KB Output is correct
6 Correct 30 ms 16040 KB Output is correct
7 Correct 31 ms 16140 KB Output is correct
8 Correct 32 ms 16600 KB Output is correct
9 Correct 16 ms 12124 KB Output is correct
10 Correct 16 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10076 KB Output is correct
2 Correct 8 ms 10332 KB Output is correct
3 Correct 6 ms 10220 KB Output is correct
4 Correct 7 ms 10328 KB Output is correct
5 Correct 7 ms 10076 KB Output is correct
6 Correct 6 ms 10072 KB Output is correct
7 Correct 7 ms 10076 KB Output is correct
8 Correct 6 ms 10072 KB Output is correct
9 Correct 5 ms 10076 KB Output is correct
10 Correct 6 ms 10076 KB Output is correct
11 Correct 5 ms 10076 KB Output is correct
12 Correct 8 ms 10088 KB Output is correct
13 Correct 5 ms 10328 KB Output is correct
14 Correct 7 ms 10332 KB Output is correct
15 Correct 19 ms 10396 KB Output is correct
16 Correct 31 ms 10332 KB Output is correct
17 Correct 68 ms 10396 KB Output is correct
18 Correct 6 ms 10332 KB Output is correct
19 Correct 8 ms 10332 KB Output is correct
20 Correct 9 ms 10240 KB Output is correct
21 Correct 5 ms 10076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 16056 KB Output is correct
2 Correct 35 ms 16532 KB Output is correct
3 Correct 26 ms 15572 KB Output is correct
4 Correct 38 ms 15572 KB Output is correct
5 Correct 43 ms 16844 KB Output is correct
6 Correct 27 ms 14616 KB Output is correct
7 Correct 18 ms 12304 KB Output is correct
8 Correct 18 ms 13368 KB Output is correct
9 Correct 21 ms 13336 KB Output is correct
10 Correct 33 ms 15372 KB Output is correct
11 Correct 43 ms 15220 KB Output is correct
12 Correct 34 ms 17360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 16056 KB Output is correct
2 Correct 35 ms 16532 KB Output is correct
3 Correct 26 ms 15572 KB Output is correct
4 Correct 38 ms 15572 KB Output is correct
5 Correct 43 ms 16844 KB Output is correct
6 Correct 27 ms 14616 KB Output is correct
7 Correct 18 ms 12304 KB Output is correct
8 Correct 18 ms 13368 KB Output is correct
9 Correct 21 ms 13336 KB Output is correct
10 Correct 33 ms 15372 KB Output is correct
11 Correct 43 ms 15220 KB Output is correct
12 Correct 34 ms 17360 KB Output is correct
13 Correct 30 ms 16136 KB Output is correct
14 Correct 55 ms 16520 KB Output is correct
15 Correct 202 ms 17128 KB Output is correct
16 Correct 198 ms 17052 KB Output is correct
17 Correct 21 ms 14772 KB Output is correct
18 Correct 82 ms 16452 KB Output is correct
19 Correct 75 ms 15888 KB Output is correct
20 Correct 51 ms 17056 KB Output is correct
21 Correct 22 ms 13528 KB Output is correct
22 Correct 20 ms 14056 KB Output is correct
23 Correct 23 ms 14056 KB Output is correct
24 Correct 31 ms 15684 KB Output is correct
25 Correct 28 ms 17136 KB Output is correct
26 Correct 29 ms 16636 KB Output is correct
27 Correct 37 ms 15152 KB Output is correct
28 Correct 222 ms 15152 KB Output is correct
29 Correct 417 ms 15148 KB Output is correct
30 Correct 43 ms 18060 KB Output is correct
31 Correct 421 ms 18848 KB Output is correct
32 Correct 770 ms 18968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10076 KB Output is correct
2 Correct 8 ms 10332 KB Output is correct
3 Correct 6 ms 10220 KB Output is correct
4 Correct 7 ms 10328 KB Output is correct
5 Correct 7 ms 10076 KB Output is correct
6 Correct 6 ms 10072 KB Output is correct
7 Correct 7 ms 10076 KB Output is correct
8 Correct 6 ms 10072 KB Output is correct
9 Correct 5 ms 10076 KB Output is correct
10 Correct 6 ms 10076 KB Output is correct
11 Correct 5 ms 10076 KB Output is correct
12 Correct 8 ms 10088 KB Output is correct
13 Correct 5 ms 10328 KB Output is correct
14 Correct 7 ms 10332 KB Output is correct
15 Correct 19 ms 10396 KB Output is correct
16 Correct 31 ms 10332 KB Output is correct
17 Correct 68 ms 10396 KB Output is correct
18 Correct 6 ms 10332 KB Output is correct
19 Correct 8 ms 10332 KB Output is correct
20 Correct 9 ms 10240 KB Output is correct
21 Correct 5 ms 10076 KB Output is correct
22 Correct 63 ms 14100 KB Output is correct
23 Correct 124 ms 14120 KB Output is correct
24 Correct 25 ms 13784 KB Output is correct
25 Correct 720 ms 14128 KB Output is correct
26 Correct 78 ms 14100 KB Output is correct
27 Correct 96 ms 14248 KB Output is correct
28 Correct 20 ms 13692 KB Output is correct
29 Correct 23 ms 13924 KB Output is correct
30 Correct 30 ms 14464 KB Output is correct
31 Correct 71 ms 14336 KB Output is correct
32 Correct 16 ms 12664 KB Output is correct
33 Correct 17 ms 12556 KB Output is correct
34 Correct 22 ms 14312 KB Output is correct
35 Correct 18 ms 14052 KB Output is correct
36 Correct 18 ms 14136 KB Output is correct
37 Correct 17 ms 14064 KB Output is correct
38 Correct 18 ms 14224 KB Output is correct
39 Correct 560 ms 15236 KB Output is correct
40 Correct 723 ms 15188 KB Output is correct
41 Correct 1083 ms 15392 KB Output is correct
42 Correct 1791 ms 15104 KB Output is correct
43 Correct 2509 ms 15144 KB Output is correct
44 Correct 3565 ms 15112 KB Output is correct
45 Execution timed out 4006 ms 15032 KB Time limit exceeded
46 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10072 KB Output is correct
2 Correct 21 ms 14044 KB Output is correct
3 Correct 31 ms 16844 KB Output is correct
4 Correct 27 ms 16152 KB Output is correct
5 Correct 6 ms 10076 KB Output is correct
6 Correct 23 ms 13908 KB Output is correct
7 Correct 30 ms 16060 KB Output is correct
8 Correct 30 ms 16536 KB Output is correct
9 Correct 32 ms 16308 KB Output is correct
10 Correct 30 ms 16140 KB Output is correct
11 Correct 28 ms 15140 KB Output is correct
12 Correct 31 ms 15788 KB Output is correct
13 Correct 6 ms 10328 KB Output is correct
14 Correct 21 ms 13796 KB Output is correct
15 Correct 30 ms 16148 KB Output is correct
16 Correct 31 ms 16584 KB Output is correct
17 Correct 32 ms 16808 KB Output is correct
18 Correct 30 ms 16040 KB Output is correct
19 Correct 31 ms 16140 KB Output is correct
20 Correct 32 ms 16600 KB Output is correct
21 Correct 16 ms 12124 KB Output is correct
22 Correct 16 ms 12124 KB Output is correct
23 Correct 6 ms 10076 KB Output is correct
24 Correct 8 ms 10332 KB Output is correct
25 Correct 6 ms 10220 KB Output is correct
26 Correct 7 ms 10328 KB Output is correct
27 Correct 7 ms 10076 KB Output is correct
28 Correct 6 ms 10072 KB Output is correct
29 Correct 7 ms 10076 KB Output is correct
30 Correct 6 ms 10072 KB Output is correct
31 Correct 5 ms 10076 KB Output is correct
32 Correct 6 ms 10076 KB Output is correct
33 Correct 5 ms 10076 KB Output is correct
34 Correct 8 ms 10088 KB Output is correct
35 Correct 5 ms 10328 KB Output is correct
36 Correct 7 ms 10332 KB Output is correct
37 Correct 19 ms 10396 KB Output is correct
38 Correct 31 ms 10332 KB Output is correct
39 Correct 68 ms 10396 KB Output is correct
40 Correct 6 ms 10332 KB Output is correct
41 Correct 8 ms 10332 KB Output is correct
42 Correct 9 ms 10240 KB Output is correct
43 Correct 5 ms 10076 KB Output is correct
44 Correct 30 ms 16056 KB Output is correct
45 Correct 35 ms 16532 KB Output is correct
46 Correct 26 ms 15572 KB Output is correct
47 Correct 38 ms 15572 KB Output is correct
48 Correct 43 ms 16844 KB Output is correct
49 Correct 27 ms 14616 KB Output is correct
50 Correct 18 ms 12304 KB Output is correct
51 Correct 18 ms 13368 KB Output is correct
52 Correct 21 ms 13336 KB Output is correct
53 Correct 33 ms 15372 KB Output is correct
54 Correct 43 ms 15220 KB Output is correct
55 Correct 34 ms 17360 KB Output is correct
56 Correct 30 ms 16136 KB Output is correct
57 Correct 55 ms 16520 KB Output is correct
58 Correct 202 ms 17128 KB Output is correct
59 Correct 198 ms 17052 KB Output is correct
60 Correct 21 ms 14772 KB Output is correct
61 Correct 82 ms 16452 KB Output is correct
62 Correct 75 ms 15888 KB Output is correct
63 Correct 51 ms 17056 KB Output is correct
64 Correct 22 ms 13528 KB Output is correct
65 Correct 20 ms 14056 KB Output is correct
66 Correct 23 ms 14056 KB Output is correct
67 Correct 31 ms 15684 KB Output is correct
68 Correct 28 ms 17136 KB Output is correct
69 Correct 29 ms 16636 KB Output is correct
70 Correct 37 ms 15152 KB Output is correct
71 Correct 222 ms 15152 KB Output is correct
72 Correct 417 ms 15148 KB Output is correct
73 Correct 43 ms 18060 KB Output is correct
74 Correct 421 ms 18848 KB Output is correct
75 Correct 770 ms 18968 KB Output is correct
76 Correct 63 ms 14100 KB Output is correct
77 Correct 124 ms 14120 KB Output is correct
78 Correct 25 ms 13784 KB Output is correct
79 Correct 720 ms 14128 KB Output is correct
80 Correct 78 ms 14100 KB Output is correct
81 Correct 96 ms 14248 KB Output is correct
82 Correct 20 ms 13692 KB Output is correct
83 Correct 23 ms 13924 KB Output is correct
84 Correct 30 ms 14464 KB Output is correct
85 Correct 71 ms 14336 KB Output is correct
86 Correct 16 ms 12664 KB Output is correct
87 Correct 17 ms 12556 KB Output is correct
88 Correct 22 ms 14312 KB Output is correct
89 Correct 18 ms 14052 KB Output is correct
90 Correct 18 ms 14136 KB Output is correct
91 Correct 17 ms 14064 KB Output is correct
92 Correct 18 ms 14224 KB Output is correct
93 Correct 560 ms 15236 KB Output is correct
94 Correct 723 ms 15188 KB Output is correct
95 Correct 1083 ms 15392 KB Output is correct
96 Correct 1791 ms 15104 KB Output is correct
97 Correct 2509 ms 15144 KB Output is correct
98 Correct 3565 ms 15112 KB Output is correct
99 Execution timed out 4006 ms 15032 KB Time limit exceeded
100 Halted 0 ms 0 KB -