Submission #849744

# Submission time Handle Problem Language Result Execution time Memory
849744 2023-09-15T08:59:28 Z Lib Closing Time (IOI23_closing) C++17
Compilation error
0 ms 0 KB
#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
/*
Mieu ta thuat toan:
B1) BFS de tinh cac gia tri Cost1[i] va Cost2[i] tuong ung voi moi dinh i
B2) Phan loai cac dinh dua theo Cost1[i] va Cost2[i]:
- Type[i]=0 neu i la 1 dinh binh thuong (0 thuoc 2 loai con lai)
- Type[i]=1 neu i la 1 dinh dac biet (Co Cost1[i]>Cost2[i] va Cost2[i]<DistX[Y])
- Type[i]=2 neu i la 1 dinh thuoc duong di tu X den Y
B3) Xu li 2 truong hop:
- P1: Tat ca cac dinh deu chi duoc nang cap len Lv1, 2 cay khong giao nhau: Bu code subtask 1
- P2: Ton tai it nhat 1 dinh loai 2: Tao ra 3 cai priority queue:
+ pq1: Dung de luu cac gia tri loai 0 va cac lan nang cap tu lv1 len lv2 cua cac dinh loai 2
+ pq2: Dung de luu cac gia tri loai 1: Day nguyen ca cap -(Cost1[i]+Cost2[i]) vao trong pq
+ pq3: Dung de luu phan tu duy nhat duoc nang cap len Lv1 thuoc loai 1 (neu co)
+ pq4 ton tai de do phai pop sau khi ket thuc giai doan 1 (cam on BTC vi da cho code nhin thoang hon
+ Tham lam tren phan tu top cua pq1 va pq2: So sanh theo Cost Efficientcy
*/
long long Type[200003];
long long Check[200003];
long long BFSCheck[200003];
long long Cost1[200003];
long long Cost2[200003];
long long CPrev[200003];
long long DistX[200003];
long long DistY[200003];
long long check[200003];
long long N,X,Y,K,n,ans1,ans2;
struct edge{
	int first;
	long long second;
};
vector <edge> VTemp;
vector <vector <edge> > adj;
vector <int> XYPath;
priority_queue <pair <long long,int> > pq1,pq2,pq3;
priority_queue <long long> pq4;
int max_score(int N, int X, int Y, long long K, std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
	int n=N;
	int x=X;
	int y=Y;
	long long k=K;
	int q,s,e;
	long long tans,Budget,len;
	vector <int> CList;
	int cur,cur2;
	int t;
	long long t2;
	pair <long long,int> tpos;
	cin>>q;
	for(int whatever=1;whatever<=q;whatever++){
		//initialize cho moi query
		while(!pq1.empty()){
			pq1.pop();
		}
		while(!pq2.empty()){
			pq2.pop();
		}
		while(!pq3.empty()){
			pq3.pop();
		}
		while(!pq4.empty()){
			pq4.pop();
		}
		XYPath.clear();
		cin>>N>>X>>Y>>K;
		adj.clear();
		n=N;
		for(int i=0;i<=n+1;i++){
			adj.push_back(VTemp);
			check[i]=0;
			Cost1[i]=0;
			Cost2[i]=0;
			DistX[i]=0;
			DistY[i]=0;
			Type[i]=0;
		}
		ans1=0;
		ans2=0;
		for(int i=1;i<n;i++){
			s=U[i-1];
			e=V[i-1];
			len=W[i-1];
			adj[s].push_back({e,len});
			adj[e].push_back({s,len});
		}
		//BFS tu X
		CList.clear();
		cur=0;
		cur2=0;
		for(int i=0;i<=n+1;i++){
			BFSCheck[i]=0;
			CPrev[i]=0;
		}
		CList.push_back(X);
		BFSCheck[X]=1;
		DistX[X]=0;
		CPrev[X]=-1;
		while(cur<CList.size()){
			t=CList[cur];
			for(int i=0;i<adj[t].size();i++){
				if(BFSCheck[adj[t][i].first]==0){
					BFSCheck[adj[t][i].first]=1;
					DistX[adj[t][i].first]=DistX[t]+adj[t][i].second;
					CPrev[adj[t][i].first]=t;
					CList.push_back(adj[t][i].first);
					if(adj[t][i].first==Y){
					XYPath.push_back(Y);
					cur2=Y;
					while(CPrev[cur2]>=0){
						cur2=CPrev[cur2];
						XYPath.push_back(cur2);
					}
				}
				}
			}
			cur++;
		}
		//BFS tu Y
		CList.clear();
		cur=0;
		cur2=0;
		for(int i=0;i<=n+1;i++){
			BFSCheck[i]=0;
			CPrev[i]=0;
		}
		CList.push_back(Y);
		BFSCheck[Y]=1;
		DistY[Y]=0;
		while(cur<CList.size()){
			t=CList[cur];
			for(int i=0;i<adj[t].size();i++){
				if(BFSCheck[adj[t][i].first]==0){
					BFSCheck[adj[t][i].first]=1;
					DistY[adj[t][i].first]=DistY[t]+adj[t][i].second;
					CList.push_back(adj[t][i].first);
				}
			}
			cur++;
		}
		for(int i=0;i<n;i++){
			Cost1[i]=min(DistX[i],DistY[i]);
			Cost2[i]=max(DistX[i],DistY[i])-Cost1[i];
			if(Cost2[i]<DistX[Y]){
			 Type[i]=1;
			}
		}
		for(int i=0;i<XYPath.size();i++){
			Type[XYPath[i]]=2;
		}
		//Phase 1: Chi nang cap len loai 1
		for(int i=0;i<n;i++){
			pq4.push(-Cost1[i]);
			//cout<<Cost1[i]<<" ";
		//	cout<<Type[i]<<" ";
		}
		Budget=K;
		while(Budget>0&&!pq4.empty()){
			Budget+=pq4.top();
			if(Budget>=0){
				ans1++;
				pq4.pop();
			}
		}
		//Phase 2: Ton tai it nhat 1 dinh duoc nang cap len loai 2
		Budget=K;
	//	cout<<Budget<<" ";
		for(int i=0;i<XYPath.size();i++){
			Budget-=Cost1[XYPath[i]];
			check[XYPath[i]]=1;
			ans2++;
		}
		tans=ans1;
		//cout<<Budget<<" ";
		if(Budget>0){
			for(int i=0;i<n;i++){
				if(Type[i]==0){
					pq1.push({-Cost1[i],i});
				}else if(Type[i]==1){
					pq2.push({-(Cost1[i]+Cost2[i]),i});
					pq3.push({-Cost1[i],i});
				}else{
					pq1.push({-Cost2[i],i});
				}
			}
			while(Budget>0&&(!pq1.empty()||!pq2.empty())){
				tpos=pq1.top();
				pq1.pop();
				if(!pq1.empty()){
					t2=pq1.top().first+tpos.first;
				}else{
					t2=tpos.first;
				}
				pq1.push(tpos);
				if(!pq2.empty()&&Budget+pq2.top().first>=0&&pq2.top().first>=t2){
					ans2+=2;
					tpos=pq2.top();
					pq2.pop();
					Budget+=tpos.first;
					check[tpos.second]=2;
				}else if(!pq1.empty()&&Budget+pq1.top().first>=0){
					ans2+=1;
					tpos=pq1.top();
					pq1.pop();
					Budget+=tpos.first;
					check[tpos.first]++;
					if(check[tpos.first]==1){
						pq1.push({-Cost2[tpos.first],tpos.first});
					}
				}else{
					while(!pq3.empty()&&Budget+pq3.top().first<0&&check[pq3.top().first]==0){
						pq3.pop();
					}
					if(!pq3.empty()){
						ans2++;
						Budget+=pq3.top().first;
						break;
					}
				}
			}
			tans=max(ans1,ans2);
		}
		return tans;
}

Compilation message

closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:101:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |   while(cur<CList.size()){
      |         ~~~^~~~~~~~~~~~~
closing.cpp:103:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |    for(int i=0;i<adj[t].size();i++){
      |                ~^~~~~~~~~~~~~~
closing.cpp:132:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |   while(cur<CList.size()){
      |         ~~~^~~~~~~~~~~~~
closing.cpp:134:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |    for(int i=0;i<adj[t].size();i++){
      |                ~^~~~~~~~~~~~~~
closing.cpp:150:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |   for(int i=0;i<XYPath.size();i++){
      |               ~^~~~~~~~~~~~~~
closing.cpp:170:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |   for(int i=0;i<XYPath.size();i++){
      |               ~^~~~~~~~~~~~~~
closing.cpp:42:6: warning: unused variable 'x' [-Wunused-variable]
   42 |  int x=X;
      |      ^
closing.cpp:43:6: warning: unused variable 'y' [-Wunused-variable]
   43 |  int y=Y;
      |      ^
closing.cpp:44:12: warning: unused variable 'k' [-Wunused-variable]
   44 |  long long k=K;
      |            ^
closing.cpp:226:1: error: expected '}' at end of input
  226 | }
      | ^
closing.cpp:40:1: note: to match this '{'
   40 | {
      | ^
closing.cpp:47:15: warning: control reaches end of non-void function [-Wreturn-type]
   47 |  vector <int> CList;
      |               ^~~~~