Boat fare issues

Source: Internet
Author: User

Problem description

A tourist city has opened a number of tourist attractions along the Changjiang River. A boat club is set up at these attractions with a yacht rental station. Guests can rent a boat at these boat rental stations and return the boat at any of the downstream boat rental stations, from a boat rental station to the rental price of a downstream boat rental station. Your task is to calculate the minimum charter cost for the visitor from the starting point to the terminal.

Input

The input file has several sets of data, with an integer n (1<=n<=100) on the first line of each set of test data, indicating an upstream starting point 0 to a downstream N-boat rental Station

1, 2, ... N. Then there are n rows, and the 1th line in the n row has n integers, representing the No. 0 station to the first ... , n-station boat rental; the 2nd line has n-1 integers, representing the 1th station to the 2,3,4, ... The rent of the boat between stations N. The n-1 line has 2 integers representing the rental of the cruise ship between the N-2 station and the N-1 and N stations, and the nth line has 1 integers representing the cruise rent between the N-1 station and the nth station. Two integers on a line are separated by a space. There are no blank lines between the two sets of test data.

Output

For each set of test data in the input file, first output "Case #:" On one line, where the "#" test data is numbered (starting at 1). Then output a row, the content is the visitor from starting point to the terminal in this case the minimum charter fee.

Input sample

3

2 3 6

1 3

2

3

4 7 9

4 5

6

Output sample

Case 1:

5

Case 2:

9

Idea one, using f[i][j] to represent a charter fee from I to J, A[i][j] represents the minimum cost of a boat trip from I to J, and then from station I to J station either directly or through a transit point k from I to K and from K to J.

The equation of state transfer is obtained

A[i][j] = min ((a[i][k]+a[k][j]), f[i][j]);

#include <iostream> #include <stdio.h> #include <algorithm> #define MAXN 100using namespace Std;int f[ MAXN][MAXN],A[MAXN][MAXN];//F[I][J] represents a charter fee from I to J, A[i][j] represents the minimum cruise cost for I to J int main (void) {    int n;    int i,j,k;    int num = 1;    while (Cin>>n)    {        //int num = 1;        for (i = 0, i < n; i++)        {for          (j = i+1; J <= N; j + +)          {              cin>>f[i][j];              A[I][J] = f[i][j];//by way of initializing A[i][j] array.          }        }        for (i = 0; l < n; i++)        {for            (k = i+1; k < n-1; k++) for            (j = k+1; J <= N; j + +)            {                a[i][j] = Min ((f[i][k]+f[k][j]), f[i][j]);            }        }        cout<< "Case" <<num<< ":" <<endl;        num++;        cout<<a[0][n]<<endl;    }    cout << "Hello world!" << Endl;    return 0;}
We can see the middle because of the addition of a transit station more to be a Triple loop , less efficient and 1 million of the data is likely to time out!

So we've got another way.

Idea Two,
Use an array of m[i] to represent the minimum boat rental for starting point to Station I. It either arrives directly or needs to pass through the transit station K;
Get the state transition equation to M[i] = min (m[i],m[k]+f[k][i]);
A double loop to get the results
The code is as follows
for (i = 0; i < n; i++)
{
M[i] = F[0][i];
for (j = n-i; j > 0; j--)
M[i] = min (m[i],m[j]+f[j][i]);
}

Boat fare issues

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.