ZOJ Problem Set - 1877

Posted hello-liuliu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ Problem Set - 1877相关的知识,希望对你有一定的参考价值。

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


Source: University of Waterloo Local Contest 2000.09.
题目分析:
  有n个人通过一座桥到桥的另一头,每次只能通过两个人并且需要一把手电,不同的人过桥的速度不同,每次通过的时间由速度最慢的那个人决定,过桥后必须有一个人把手电带回去。
求所有人都到桥的另外一头所需的最少时间。
输出最少时间和每一次过桥的人。
 
因为每次过桥后,都有一个人会回来,那么使用贪心的思想,每次都由速度最快的人带一个慢的人过桥,然后快的人再回来。
设花费的时间为time;
考虑只有a一个人的情况:
  直接过桥。
  time=a;
考虑只有a,b两个人的情况:
  直接过桥。
  time=max(a,b);
考虑只有a,b,c三个人的情况:
  假设a<b<c.
  a b 过桥,a回来,a c过桥
  time=b+a+c;
考虑只有a,b,c,d 4个人的情况:
  假设a<b<c<d.
  使用贪心思想,会有两种情况:
  1)、a b过桥,a回来,c d过桥,b回来,a b过桥;
  time=b+a+d+b+b;
  2)、a c过桥,a回来,b d过桥 ,b回来,a b过桥;
  time=c+a+d+b+b;
  结果去两种情况的最小值;
  
当n>4时:
  S={a,b,c,d,e,f....};
将所有人按照速度排序,最慢的人为j,只需考虑S0,S1,Sj-1,Sj的过桥情况。这样不断的将最慢的人过桥,就把n的情况转换为前面的三种平凡的情况。
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <cstring>
 7 #include <map>
 8 using namespace std;
 9 const int N=1009;
10 int s[N];
11 int n;
12 
13 int main()
14 {
15     int time=0;
16     int count=0;
17     int j;
18     while(scanf("%d",&n)!=EOF)
19     {
20         time=0;
21         count =0;
22 
23         for(int i=0;i<n;i++)
24         {
25             scanf("%d",&s[i]);
26         }
27         sort(s,s+n);
28         j=n-1;//最慢的人
29         while(count<=n-4)//4个人的情况
30         {
31             if(2*s[1]<s[0]+s[j-1])
32             {
33                 time+=s[1]+s[1]+s[j]+s[0];
34             }
35             else
36             {
37                 time+=s[j]+s[0]+s[0]+s[j-1];
38             }
39             count+=2;//有2个人过桥
40             j-=2;//规模减少2
41         }
      //剩下的3种情况
42 switch(n-count) 43 { 44 case 1: 45 time+=s[0]; 46 break; 47 case 2: 48 time+=s[1]; 49 break; 50 case 3: 51 time+=s[0]+s[1]+s[2]; 52 break; 53 } 54 printf("%d\n",time);//输出总时间 55 j=n-1; 56 count =0;
      //输出过桥的方法
57 while(count<=n-4) 58 { 59 if(2*s[1]<s[0]+s[j-1]) 60 { 61 printf("%d %d\n%d\n",s[0],s[1],s[0]); 62 printf("%d %d\n%d\n",s[j-1],s[j],s[1]); 63 } 64 else 65 { 66 printf("%d %d\n%d\n",s[0],s[j-1],s[0]); 67 printf("%d %d\n%d\n",s[0],s[j],s[0]); 68 } 69 count+=2; 70 j-=2; 71 72 } 73 switch(n-count) 74 { 75 case 1: 76 printf("%d\n",s[0]); 77 break; 78 case 2: 79 printf("%d %d\n",s[0],s[1]); 80 break; 81 case 3: 82 printf("%d %d\n%d\n",s[0],s[1],s[0]); 83 printf("%d %d\n",s[0],s[2]); 84 break; 85 } 86 } 87 return 0; 88 }

 

以上是关于ZOJ Problem Set - 1877的主要内容,如果未能解决你的问题,请参考以下文章

ZOJ Problem Set - 1025解题报告

ZOJ Problem Set - 1013 Great Equipment ()

ZOJ Problem Set - 1004 Anagrams by Stack (回溯法)

ZOJ 3329 Problem Set (期望dp)

ZOJ Problem Set - 2480 Simplest Task in Windows

ZOJ Problem Set - 1008 Gnome Tetravex (DFS+剪枝)