Crossing River poj1700 Greedy

Source: Internet
Author: User

Title Description: n Individuals across the river, there is only one boat, up to two people rowing, each rowing speed is different, the speed of the boat speed of the slowest person. Input T is the number of case, each case input n is the number of people, the next line of input is the time each person crosses the river, are not the same. The time required to output n people all across the river

Algorithm thought: Adopt the Greedy method. There are two ways of rowing, one is the fastest + slowest, the fastest back, the fastest + times slow, the fastest back, cycle down, the second is the fastest + times faster, faster back, the slowest + times slow, the fastest back, cycle down. So when the rest of the population n>3, compare the first and second time, using a short-time method to cross the river. n=1,2,3 the time to discuss it alone.

#include <iostream>#include<stdio.h>#include<algorithm>#include<string.h>using namespacestd;intpt[1010];//Store time of peopleintMain () {intT; scanf ("%d",&T);  while(t--){        intN; scanf ("%d",&N); Memset (PT,0,sizeof(PT));  for(intI=0; i<n;i++) {scanf ("%d", pt+i); } sort (Pt,pt+N); intt=0;  while(n>3){            if(2*pt[1]+pt[0]>2*pt[0]+pt[n-2]) {T+=2*pt[0]+pt[n-1]+pt[n-2]; }            Else{T+=2*pt[1]+pt[n-1]+pt[0]; } N-=2; }        if(n==3) {T+=pt[2]+pt[0]+pt[1]; }        Else if(n==2) {T+=pt[1]; }        Else{T+=pt[0]; } printf ("%d\n", T); }    return 0;}

Crossing River poj1700 Greedy

Related Article

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.