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 }