Cod sursa(job #163586)

Utilizator Adriana_SAdriana Sperlea Adriana_S Data 22 martie 2008 14:48:16
Problema Wanted Scor 20
Compilator cpp Status done
Runda preONI 2008, Runda Finala, Clasa a 10-a Marime 1.35 kb
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

const int N_MAX = 210;
const int INF = 2000000000;

pair <int, int> v[N_MAX];

int mabs(int a)
{
	return (a < 0 ? -a : a);
}

int N, used[N_MAX];

int cost(int st, int dr, int poz, int sum)
{
	if (st == dr) return sum;
	else {
		int MIN = INF, dist, unu, doi, g = 0;
	
		for (int i = st; i <= dr; i ++) {

			if (!used[i]) {
				g = 1;
				dist = v[poz].second + mabs(v[poz].first - v[i].first) + v[i].second;
		
				used[i] = 1;
				unu = cost(st, i, i, sum + dist);
				doi = cost(i, dr, i, sum + dist);
				used[i] = 0;
		
				if (unu > doi) {
					if (unu < MIN) MIN = unu;
				} else {
					if (doi < MIN) MIN = doi;
				}
			}
		}

		if (g) return MIN;
		else return sum;
	}
}

int main()
{
	freopen("wanted.in", "r", stdin);
//#ifndef _SCREEN_
	freopen("wanted.out", "w", stdout);
//#endif

	int x, y;

	scanf("%d\n", &N);
	for (int i = 1; i <= N; i ++) {
		scanf("%d %d\n", &x, &y);
		v[i] = make_pair(x, y);
	}

	sort(v + 1, v + N + 1);

	int MINN = INF, care = 0;

	for (int i = 1; i <= N; i ++) {
		used[i] = 1;

		int bla1 = cost(1, i, i, mabs(v[i].first) + v[i].second);
		int bla2 = cost(i, N, i, mabs(v[i].first) + v[i].second);

		if (bla1 > bla2) {
			if (bla1 < MINN) MINN = bla1, care = i;
		} 
		else MINN = bla2, care = i;

		used[i] = 0;
	}

	printf("%d\n", MINN);

	return 0;
}