Cod sursa(job #721078)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 23 martie 2012 11:39:33
Problema Wanted Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
#include <algorithm>
#define NMAx 220
#define KMAx 2
#define oo (1<<30)
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)<0?-(a):(a))
#define Dist(a,b) (abs(V[b].x-V[a].x)+V[a].y+V[b].y)
using namespace std;

struct Punct{int x,y;}V[NMAx];
int N,Sol,A[NMAx][NMAx][KMAx];

int DEI(int Left,int Right,int K) {
	
	if(Left>Right)
		return 0;
	else
	if(A[Left][Right][K]<oo)
		return A[Left][Right][K];
	
	for(int Mid=Left;Mid<=Right;Mid++) {
		
		if(K==0)
			A[Left][Right][K]=min( A[Left][Right][K], max( DEI(Left,Mid-1,0), DEI(Mid+1,Right,1))+Dist(Mid,Right+1));
		else
			A[Left][Right][K]=min( A[Left][Right][K], max( DEI(Left,Mid-1,0), DEI(Mid+1,Right,1))+Dist(Left-1,Mid));
		
		}
	
	return A[Left][Right][K];
	
}
bool cmp(Punct A,Punct B) {
	return A.x<B.x;
}
void init() {
	
	int i,j;
	
	for(i=1;i<=N;i++)
		for(j=1;j<=N;j++)
			A[i][j][0]=A[i][j][1]=oo;
	
}
void citire() {
	
	ifstream in("wanted.in");
	in>>N;
	
	for(int i=1;i<=N;i++)
		in>>V[i].x>>V[i].y;
	
	in.close();
	
}
void afis() {
	
	ofstream out("wanted.out");
	out<<Sol<<'\n';
	out.close();
	
}
int main() {
	
	citire();
	
	init();
	sort(V+1,V+N+1,cmp);
	Sol=DEI(1,N,0);
	
	afis();
	
	return 0;
	
}