Grouping Boats, using ingboats

Source: Internet
Author: User

Grouping Boats, using ingboats

Description

The Yangtze River Yacht Club has set up n yacht rental stations on the Yangtze River 1, 2 ,..., N. Visitors can rent a yacht at these yacht rental stations and return the yacht at any of the downstream yacht rental stations. The rent between the yacht rental station I and the yacht rental station j is r (I, j), 1 <= I <j <= n. Design an algorithm to calculate the minimum rent required from yacht rental station 1 to yacht rental Station n.

 

Input

There is a positive integer n (n <= 1st) in row 200, indicating that there are n yacht rental stations. The following n-1 rows are r (I, j) and 1 <= I <j <= n.

 

Output

Minimum rent required from yacht rental station 1 to yacht rental Station n

 

Sample Input

3

5 15

7

 

Sample Output

12

 

This topic is about dynamic planning. Using the floyd algorithm and other people's code, I am too lazy to think about it -.-

 

 1 #include<stdio.h>
2 int f[201][201],n,i,j,k,p,tmp; 3 void solve() 4 { 5 for(k=2;k<n;k++) 6 for(i=0;i<n-k;i++) 7 { 8 j=i+k; 9 for(p=i+1;p<j;p++) 10 { 11 tmp=f[i][p]+f[p][j]; 12 if(f[i][j]>tmp) 13 f[i][j]=tmp; 14 } 15 } 16 } 17 18 int main() 19 { 20 while(scanf("%d",&n)!=EOF) 21 { 22 for(i=0;i<n;i++) 23 {24 for(j=i+1;j<n;j++) 25 scanf("%d",&f[i][j]); 26 }27 solve(); 28 printf("%d\n",f[0][n-1]); 29 } 30 return 0;31 }

 

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.