ZOJ-1877 Bridge

Bridge

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

n people wish to cross a bridge at night. A group of at most two people may cross at any time, and each group must have a flashlight. Only one flashlight is available among the n people, so some sort of shuttle arrangement must be arranged in order to return the flashlight so that more people may cross. 

Each person has a different crossing speed; the speed of a group is determined by the speed of the slower member. Your job is to determine a strategy that gets all n people across the bridge in the minimum time.


Input

The first line of input contains n, followed by n lines giving the crossing times for each of the people. There are not more than 1000 people and nobody takes more than 100 seconds to cross the bridge.

Input contains multiple test cases. Process to the end of file.


Output

The first line of output must contain the total number of seconds required for all n people to cross the bridge. The following lines give a strategy for achieving this time. Each line contains either one or two integers, indicating which person or people form the next group to cross. (Each person is indicated by the crossing time specified in the input. Although many people may have the same crossing time the ambiguity is of no consequence.) Note that the crossings alternate directions, as it is necessary to return the flashlight so that more may cross. If more than one strategy yields the minimal time, any one will do.


Sample Input

4
1
2
5
10


Sample Output

17
1 2
1
5 10
2
1 2


對於過橋問題

對於人數n

n == 1直接過橋

n == 2 取決慢的那個人的速度

n == 3 由快的的那個人來回將每個人帶回去

n >= 4 對於這種情況,需要分兩種情況判斷那種更快

先討論4個人的情況,假定人的時間爲a<b<c<d

第一種:

由最快的人先把最後2個人帶過去

時間爲:

T=d+a+c+a;

第二種:

由最快的兩個人將滿的兩個人帶過去

時間爲:

T=b+a+d+b;

n>4時,可以將每次帶2個人,直到剩下<4個人的情況。

然後按照前面<4個人的情況討論即可。


#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a, const void *b)
{
	return *(int *)a - *(int *)b;
}

int pretend_1(int *, int);
int pretend_2(int *, int);
void print_1(int *, int);
void print_2(int *, int);

int main()
{
	int n, i, temp, total, t1, t2;
	int save[1000][2], t;
	int p[1000];
	while(scanf("%d\n", &n) != EOF) {
		for(i = 0; i < n; i++) {
			scanf("%d", p+i);
		}
		qsort(p, n, sizeof(int), cmp);
		/*for(i = 0; i < n; i++) {
			printf("%d ", p[i]);
		}
		printf("\n");*/
		if(n == 1) {
			printf("%d\n", p[0]);
			printf("%d\n", p[0]);
		}
		else if(n == 2) {
			printf("%d\n", p[1]);
			printf("%d %d\n", p[0], p[1]);
		}
		else if(n == 3) {
			printf("%d\n", p[2] + p[0] + p[1]);
			printf("%d %d\n", p[0], p[2]);
			printf("%d\n", p[0]);
			printf("%d %d\n", p[0], p[1]);
		} else {
			temp = n;
			total = 0;
			t = 0;
			while(temp >= 4) {
				if((t1 = pretend_1(p, temp)) > (t2 = pretend_2(p, temp))) {
					total += t2;
					save[t][0] = temp;
					save[t++][1] = 2;
				} else {
					total += t1;
					save[t][0] = temp;
					save[t++][1] = 1;
				}
				temp -= 2;
			}
			if(temp == 3) {
				total += p[2] + p[1] + p[0];
			}
			else if(temp == 2) {
				total += p[1];
			}
			else if(temp == 1) {
				total += p[0];
			}
			printf("%d\n", total);
			for(i = 0; i < t; i++) {
				if(save[i][1] == 1) {
					print_1(p, save[i][0]);
				} else {
					print_2(p, save[i][0]);
				}
			}
			if(temp == 3) {
				printf("%d %d\n", p[0], p[2]);
				printf("%d\n", p[0]);
				printf("%d %d\n", p[0], p[1]);
			}
			else if(temp == 2) {
				printf("%d %d\n", p[0], p[1]);
			}
		} 
	}
	return 0;
}

int pretend_1(int *p, int n)
{
	return p[1] + p[0] + p[n-1] + p[1];
}

int pretend_2(int *p, int n)
{
	return p[n-1] + p[0] + p[n-2] + p[0];
}

void print_1(int *p, int n)
{
	printf("%d %d\n", p[0], p[1]);
	printf("%d\n", p[0]);
	printf("%d %d\n", p[n-2], p[n-1]);
	printf("%d\n", p[1]);
}

void print_2(int *p, int n)
{
	printf("%d %d\n", p[0], p[n-1]);
	printf("%d\n", p[0]);
	printf("%d %d\n", p[0], p[n-2]);
	printf("%d\n", p[0]);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章