hugo_DITAU

 avatar
duyvan
plain_text
6 months ago
1.8 kB
15
Indexable
Never
//hugo_ditau 3 cua

#include <iostream>
using namespace std;
int N;
int dis[65], visit[65];
int gate[3], person[3];
int mindis;

void nhap(){
	cin >> N;
	for(int i = 0; i<3; i++){
		cin >> gate[i] >> person[i];
	}
	for(int i = 0; i<=N; i++){
		visit[i] = 0;
		dis[i] = 0;
	}
}
int sum(){
	int ans = 0;
	for(int i=1; i<=N; i++){
		ans += dis[i];
	}
	return ans;
}
void OpenGate(int x){
	int pos0 = gate[x];
	int temp = person[x];
	gate[x] = 0;
	for(int i=0; i<=N; i++){
		int available = 0;
		if(dis[pos0+i]==0 && pos0+i<=N){
			dis[pos0+i] = i+1;
			visit[pos0+i] = x+1;
			available++;
		}
		if(dis[pos0-i]==0 && pos0-i>0){
			dis[pos0-i] = i+1;
			visit[pos0-i] = x+1;
			if( i!=0) available++;
		}
		if(available<=person[x]){
			person[x] -= available;
		}
		else if(available>person[x]){
			person[x]--;
			dis[pos0-i]=0;
			visit[pos0-i]=0;
			int flag = 1;
			for(int j=0; j<3; j++){
				if(gate[j] != 0){
					flag = 0;
					OpenGate(j);
				}
			}
			if(flag){
				mindis = mindis>sum()?sum():mindis;
			}
			dis[pos0-i] = i+1;
			visit[pos0-i] = x+1;
			dis[pos0+i] = 0;
			visit[pos0+i] = 0;
			flag = 1;
			for(int j=0; j<3; j++){
				if(gate[j]!=0){
					flag = 0;
					OpenGate(j);
				}
			}
			if(flag){
				mindis = mindis>sum()?sum():mindis;
			}
			break;
		}
		if(person[x]==0){
			int flag = 1;
			for(int j=0; j<3; j++){
				if(gate[j] != 0){
					flag = 0;
					OpenGate(j);
				}
			}
			if(flag){
				mindis = mindis>sum()?sum():mindis;
			}
			break;
		}
	}
	for(int i=1; i<=N; i++){
		if(visit[i] == x+1){
			dis[i] = 0;
		}
	}
	gate[x]  = pos0;
	person[x] = temp;
}
int main(){
	int T; cin >> T;
	for (int tc = 1; tc <=T; tc++)
	{
		nhap();
		mindis = 1000000;
		for(int i = 0; i<3; i++){
			OpenGate(i);
		}
		cout << "Case #" << tc << endl << mindis << endl;
	}
	return 0;
}
Leave a Comment