Crossing River
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9919   Accepted: 3752

Description

A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.

Output

For each test case, print a line containing the total number of seconds required for all the N people to cross the river.

Sample Input

  1. 1
  2. 4
  3. 1 2 5 10

Sample Output

  1. 17
  2.  
  3. pshttp://poj.org/problem?id=1700

/* 此题讲的是N个人过河,每个人都有自己的过河时间,一条船只能承受2个人,所用时间为其中过河时间最多的,

所以呢,想到有两种情况,第一种:过河时间最少的人来回接送其他人,第二种:过河时间最少和次少的人来回接送其他人,

刚开始就觉得第一种时间必然是最少的,但是仔细想想,不然。因为第一种情况虽然单次过河时间少,但送人的次数要多,

如第1个人(过河时间最少)接送第i人 dp[i]=dp[i-1]+time[0]+time[i]; 而第二种情况呢,虽然来回次数多,但是接送的人要多,

如第1个人接第i-1个和第i个人,然后让第i-1个和第i个人一块过河,第2个人再接送第1个人。

dp[i]=dp[i-2]+time[0]+time[i]+time[1]*2。所以每次计算出这两个值都应该进行比较,从而AC!*/

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define N 1010
  4.  
  5. int cmp(const void *a,const void *b)
  6. {
  7. return *(int *)a-*(int *)b;
  8. }
  9.  
  10. int a[N];
  11. int dp[N];
  12. int n;
  13.  
  14. int main()
  15. {
  16. int i,j,t,k;
  17. int min1,min2;
  18. scanf("%d",&t);
  19. while(t--)
  20. {
  21. scanf("%d",&n);
  22. for(i=;i<n;i++)
  23. scanf("%d",&a[i]);
  24. qsort(a,n,sizeof(a[]),cmp);
  25. dp[]=a[];
  26. dp[]=a[];
  27.  
  28. for(i=;i<n;i++)
  29. {
  30. min1=dp[i-]+a[i]+a[];
  31. min2=dp[i-]+a[]+a[]+a[i]+a[];
  32. dp[i]=min1;
  33. if(min1>min2)//中间是嵌套的,哪种快选哪种
  34. dp[i]=min2;
  35. }
  36. printf("%d\n",dp[n-]);
  37. }
  38. return ;
  39. }
  40.  
  41. /*
  42. 3
  43. 6
  44. 1 2 5 10 15 20
  45. 5
  46. 1 2 5 10 15
  47. 4
  48. 1 2 5 10
  49. */

Crossing River(1700poj)的更多相关文章

  1. 702:Crossing River (贪心)

    [题目描述] N个人过河,一次过去2个回来一个,给出每个人所需时间,问最小过河时间. [题目链接] http://noi.openjudge.cn/ch0406/702/ [算法] 一开始想样例是怎么 ...

  2. ACM学习历程——POJ 1700 Crossing River(贪心)

    Description A group of N people wishes to go across a river with only one boat, which can at most ca ...

  3. POJ 1700 cross river (数学模拟)

                                                                                                       ...

  4. poj-1700 crossing river(贪心题)

    题目描述: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...

  5. bzoj1143 祭祀river(最大独立集)

    [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2175  Solved: 1098[Submit][Status] ...

  6. bzoj1143/2718 祭祀river(最大独立集)

    [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2175  Solved: 1098[Submit][Status] ...

  7. BZOJ 1143: [CTSC2008]祭祀river(最大独立集)

    题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1143 一句话题意:给一个DAG(有向无环图),求选出尽量多的点使这些点两两不可达,输出点个 ...

  8. UVA - 12230 Crossing Rivers (期望)

    Description You live in a village but work in another village. You decided to follow the straight pa ...

  9. 【ZJOI2017 Round1练习】D2T1 river(二分图)

    题意: 思路:这道题并没有官方题解 没有羊驼在所有三元组中出现就是NO 现在考虑不少于1只的情况 删去其中一只,我们得到了两组点和一些边 我们只要判断这是否为一张二分图,使用暴力染色的方法就有60分了 ...

随机推荐

  1. 阻止事件冒泡,阻止默认事件,event.stopPropagation()和event.preventDefault(),return false的区别

    1.event.stopPropagation()方法 这是阻止事件的冒泡方法,不让事件向documen上蔓延,但是默认事件任然会执行,当你掉用这个方法的时候,如果点击一个连接,这个连接仍然会被打开, ...

  2. Windows打开应用,提示“此程序被组策略阻止”

    Windows打开应用,提示"此程序被组策略阻止",该问题为组策略限制了用户使用某个应用程序,一般可以在 1 控制面板--->管理工具--->本地安全策略-->软 ...

  3. django~项目的文件位置的重要性

    前几天我犯了个很低级的错误 就是把文件的地址放错地方了~~ 我把templates文件放进mysite文件里面了 和templatetags文件同级了  所以一直报错  说找不到模板的文件 实际上te ...

  4. mysql之视图,存储过程,触发器,事务

    视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时 ...

  5. odoo开发笔记 -- 异常信息处理汇总

    1 Traceback (most recent call last): File , in _handle_exception return super(JsonRequest, self)._ha ...

  6. PyCharm下载与激活

    1.集成开发环境(IDE:Integrated Development Environment)PyCharm下载地址:https://www.jetbrains.com/pycharm/downlo ...

  7. Java代码调用Oracle的存储过程,存储函数和包

    Java代码调用存储过程和存储函数要使用CallableStatement接口 查看API文档: 上代码: java代码调用如下的存储过程和函数: 查询某个员工的姓名  月薪 职位 create or ...

  8. Centos 7 安装 Redis 3.2

    环境: Centos 7 GCC            #未安装,使用yum install gcc安装 1.下载redis 官方下载网站:https://redis.io/download.请在页面 ...

  9. 制作windows服务

    1.下载winsw-1.8-bin.exe并更名: 2.配置winsw-1.8-bin.exe同上名<?xml version="1.0" encoding="UT ...

  10. 【转】28个Unix/Linux的命令行神器

    下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具(原文链接),有一些是大家熟悉的,有一些是非常有用的,有一些是不为人知的.这些工具都非常不错,希望每个人都知道 ...