Poj 1700 cross-river

Source: Internet
Author: User

Reference blog: http://www.cnblogs.com/steady/archive/2011/01/23/1942555.html

Http://blog.csdn.net/wuzhekai1985/article/details/6846934

The above two blogs show how to do this, which is very detailed, but not why.

I tried to prove that I felt that the strategy for this question was to send the slowest two people to the past for the shortest time. The final result of this question will be optimal.

The minimum time required to cross the river is proved. Then, every two messages are sent, and recursion is formed. But when two people are sent, is the time always the best? Why can't I send three people?

I don't know. I hope you can give me some advice.

#include<cstdio>#include<algorithm>using namespace std;int sum = 0;int Cross_river(int *input,int num){    if(num <= 2) return input[num-1];    else if(num == 3)return input[0]+input[1]+input[2];    else{        int t1=input[0]+input[1]*2+input[num-1];        int t2=input[0]*2+input[num-1]+input[num-2];        int tmp=0;        tmp = t1>t2?t2:t1;        return Cross_river(input,num-2)+tmp;    }}int main(){    int T_c,num,input[1005];    scanf("%d",&T_c);    while(T_c--)    {        scanf("%d",&num);        for(int i = 0;i < num;++i)        {            scanf("%d",&input[i]);        }        sort(input,input+num);        sum = Cross_river(input,num);        printf("%d\n",sum);    }    return 0;}

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.