Poj 1700 Crossing River & nyoj 47 cross-river

Source: Internet
Author: User

Poj: http://poj.org/problem? Id = 1700

The simplest farmer is greedy.

Online Search Ideas:

Http://www.cnblogs.com/drizzlecrj/archive/2007/10/20/931011.html

The following is the best solution for constructing n people (n> = 1) Bridge

1) if n = 1 or n = 2, the owner crosses the bridge directly.

2) if n = 3, the fastest person can send the other two to cross the river at a time.

3) if n> = 4, set a and B as the fastest and fast travelers, the time required for crossing the bridge is a, B, and Z, Y is the slowest traveler and slow traveler. The time required for crossing the bridge is Z and Y, respectively. So

There are two ways to use the smallest two to send the largest two to the past (the last state is only the largest to send the past, the smallest to stay here ):

1) Use the smallest A to send Y and Z:Z + A + Y +;

2) first get the smallest two (B), then the smallest a returns (A), then the two largest (z), and then the second smallest B returns (B ),B + A + Z + B;

The time difference between the two methods is 2b-a-y.

When 2B> A + Y, use mode 1) Move Z and y across the bridge

When 2B <A + Y, use mode 2) Move Z and y across the bridge

When 2b = a + Y, use mode 1 or Mode 2 to move Z and y across the bridge.

In this way the problem becomes the case of N-2 travelers, recursive solution can be done.

ACCode

 # include 
  
    # include 
   
     # include 
    
      using namespace STD; int s [1010]; int time (int * s, int N) {If (n = 1) return s [0]; If (n = 2) return s [1]; if (n = 3) return s [0] + s [1] + s [2]; If (n> = 4) {If (s [1] * 2> = s [0] + s [N-2]) return 2 * s [0] + s [N-1] + s [N-2] + time (S, N-2 ); else if (s [1] * 2 
      T; while (t --) {int N; CIN> N; memset (S, 0, sizeof (s); For (INT I = 0; I 
      
        S [I]; sort (S, S + n); cout 
       
      
    
   
  

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.