POJ 3404&&poj1700 (greedy)

Source: Internet
Author: User

Bridge over a rough river
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4143 Accepted: 1703

Description

A Group of N travelers (1≤ n ≤50) have approached an, and shabby bridge and wishes to cross the Rive R as soon as possible. However, there can be is no more than and persons on the bridge at a time. Besides it's necessary to light the same as a torch for safe crossing but the group have only one torch.

Each traveler needs ti seconds to cross the river on the bridge; i=1, ..., N (ti is integers from 1 to 100). If The travelers is crossing together their crossing time is the time of the slowest traveler.

The task is to determine minimal crossing time for the whole group.

Input

The input consists of lines:the first line contains the value of N and the second one contains the values of ti (separated by one or several spaces).

Output

The output contains one line with the result.

Sample Input

46 7 6 5

Sample Output

29

Source

Northeastern Europe 2001, Western subregion Test instructions: n individuals across the river, each time to the past two people, the two people with a flashlight, after the river need to have a person to send back, ask everyone to pass the least time. Solution: If you want to be the shortest time for everyone to send back, and then take the shortest and time-consuming to go back together that is wrong, for example: Four people spend time on the Bridge 1 2 5 10, according to the above idea answer is 19, but the actual answer should be 17.       The concrete steps are this:       The first step: 1 and 2 past, takes time 2, then 1 comes back (spends time 1);       Second walk: 3 and 4 past, take time 10, then 2 come back (spend time 2); third: 1 and 2 past, spending time 2, total time consuming 17 greedy ideas: For the first person, 1. If there are already i-1 individuals have passed the river, then must be sent the shortest time to come to meet him: dp[i] = dp[i-1] +a[1]+a[i];2. If there are already i-2 individuals have passed the river, then the first step, first send time-consuming shortest to pick him up, and then give the flashlight to a[i] and a[i-1], and then send time-consuming small people come over, time-consuming a[2], and then go back together time a[2] dp[i] = dp[i-2] +A[I]+A[1]+2*A[2];DP [i] = max
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;inta[Wuyi],dp[Wuyi];intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {         for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Memset (DP,0,sizeof(DP)); Sort (a+1, a+n+1); dp[1] = a[1]; dp[2] = a[2];  for(intI=3; i<=n;i++) {Dp[i]= Min (dp[i-1]+a[i]+a[1],dp[i-2]+a[i]+2*a[2]+a[1]); } printf ("%d\n", Dp[n]); }}

POJ 1700 (Java Big Data Edition): Pay attention to the special case of handling n=1

import Java.math.biginteger;import java.util.arrays;import java.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner sc=NewScanner (System.inch); intTcase=Sc.nextint ();  while(tcase-->0){            intn =Sc.nextint (); if(n==1) {                intv =Sc.nextint (); System. out. println (v); Continue; } BigInteger [] a=Newbiginteger[n+1]; BigInteger [] DP=Newbiginteger[n+1];  for(intI=1; i<=n;i++){                intv =Sc.nextint (); A[i]=biginteger.valueof (v); } arrays.sort (A,1, n+1); //for (int i=1;i<=n;i++) System.out.println (A[i]);dp[1] = a[1]; dp[2] = a[2];  for(intI=3; i<=n;i++) {BigInteger C1= dp[i-1].add (A[i]). Add (a[1]); BigInteger C2= dp[i-2].add (a[1]). Add (a[2].multiply (Biginteger.valueof (2)) . Add (A[i]); if(C1.compareto (C2) >0) Dp[i] =C2; ElseDp[i] =C1; } System. out. println (Dp[n]); }    }     }

POJ 3404&&poj1700 (greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.