UVA 590-always on the run__ Dynamic planning (Liu Rugia Nineth)

Source: Internet
Author: User
Tags cas

1, http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 531

2, the subject of the main effect:

Given two number of n,k,n cities, K flights, and then give N (n-1) flight records, the former (n-1) line represents the first city flights to other cities, and if consumption is 0, there are no flights, such as the first example, the first two rows of the table to the second city and all flights in three cities. , the next (n-1) line, representing the second city to the first city and the third city, now asks for the minimum cost of getting to the nth city from the first city, through K flights.

State transition equation: Dp[i][j] represents the minimum cost of arriving at the city I through the J flight

DP[I][J] initially as -1.dp[1][0]=0;

Dp[i][j]=max (Dp[i-1][j]+cost[i][j][c],dp[i][j]);

3. Title:



Always on the Run

screeching tires. Searching lights. Wailing sirens. Police cars everywhere. Trisha Quickfinger did it again! Stealing the ' Mona Lisa ' had been more difficult than planned, but being the world ' s best art thief means expecting the UN Expected. So we are, the wrapped frame tucked firmly under her arm, running to catch the northbound Metro to Charles-de-gaulle Airport.

But even more important than actually stealing the painting was to shake off the police that would soon be following. Trisha ' s plan are simple:for several days she'll be flying from one city to another, making one flight each day. When she's reasonably sure that police has lost her trail, she'll fly to Atlanta and meet her ' customer ' (known onl Y as Mr. P.) to deliver the painting.

Her plan are complicated by the fact this nowadays, even when you are stealing expensive art, and have to watch your NG budget. Trisha therefore wants to spend the least money possible on her escape flights. This isn't easy, since airlines prices and flight availability vary from day to day. The price and availability of a airline connection depends on the two cities and involved. Every pair of cities has a ' flight schedule ' which repeats-Every few days. The length of the period may is different for each pair of cities and for each direction.

Although Trisha is a good in stealing paintings, she easily gets confused when booking airline. This is where your come in.

Input The input file contains the descriptions of several scenarios in which Trisha to escape.  Every description starts with a line containing two integers n and K. n is the number of cities through which Trisha ' s escape could take her, and K is the number of flights she would take. The cities are numbered, where 1 is Paris, her starting point, and N is Atlanta, her final destination. The numbers would satisfy and.

Next you are given n (n-1) flight schedules, one/line, describing the connection between-every possible pair of citie S. The n-1 flight schedules correspond to the flights from City 1 to all other cities (), the next n-1 lines to Those from the City 2 to all others (), and.

The description of the flight schedule itself starts with a integer d, the length of the period in, with. Following this are d non-negative integers and representing the cost of the flight between the two. A cost of 0 means this there is no flight between the two of

So, for example, the flight schedule ' 3 0 ' means ', ' and ' on the ' the ' flight costs. E is no flight in the third day it costs, and then the cycle Repeats:on the fourth day the flight costs No flight on the fifth day, etc.

The input is terminated by a scenario has n = k = 0.

Output For each scenario in the input, the scenario of the the number of the, as shown in the sample output. If It is possible for Trisha to travel K days, starting in City 1, each day flying to a different city than the day befo Re, and finally, arriving in city N, then print ' The best flight costs X. ', where x is the least Amount that's K flights can cost.

If It isn't possible to travel in such a way, print ' No flight possible.

Print a blank line after each scenario.

Sample Input

3 6 2 130, 3 0 7, 0 0, 4 3, 0,
135 140
2
, 2 3 2 0
1
0 0

Sample Output

Scenario #1 the best
flight costs 460.

Scenario #2
No flight possible.


Miguel A. Revilla 
1998-03-104, AC code;

#include <stdio.h> #include <string.h> #define N #define K 1005 int day[n][k];
int cost[n][n][35];
int dp[n][k];
int n,k;
    void DP () {memset (dp,-1,sizeof (DP));
   int min;
    dp[1][0]=0;
            for (int j=1;j<=k;j++) {for (int i=1;i<=n;i++) {min=-1;
                for (int c=1;c<=n;c++) {if (I==c | | dp[c][j-1]<0) continue;
                int t= (j-1)%day[c][i]; if (cost[c][i][t]==0) continue;//no flight if (min<0) min=dp[c][j-1]+cost[c][i
                ][T];
            else if (min>dp[c][j-1]+cost[c][i][t]) min=dp[c][j-1]+cost[c][i][t];
        } dp[i][j]=min;
    } if (dp[n][k]<0) printf ("No flight possible.\n\n");
else printf ("The best flight costs%d.\n\n", dp[n][k]);
    int main () {int cas=0;
        while (scanf ("%d%d", &n,&k)!=eof) {cas++; if (n==0 && k==0) break;
                for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (i!=j)

                    {scanf ("%d", &day[i][j]);
                    for (int c=0;c<day[i][j];c++) {scanf ("%d", &cost[i][j][c]);
        ("Scenario #%d\n", CAs);
    DP ();
return 0;
 }


Always on the Run

screeching tires. Searching lights. Wailing sirens. Police cars everywhere. Trisha Quickfinger did it again! Stealing the ' Mona Lisa ' had been more difficult than planned, but being the world ' s best art thief means expecting the UN Expected. So we are, the wrapped frame tucked firmly under her arm, running to catch the northbound Metro to Charles-de-gaulle Airport.

But even more important than actually stealing the painting was to shake off the police that would soon be following. Trisha ' s plan are simple:for several days she'll be flying from one city to another, making one flight each day. When she's reasonably sure that police has lost her trail, she'll fly to Atlanta and meet her ' customer ' (known onl Y as Mr. P.) to deliver the painting.

Her plan are complicated by the fact this nowadays, even when you are stealing expensive art, and have to watch your NG budget. Trisha therefore wants to spend the least money possible on her escape flights. This isn't easy, since airlines prices and flight availability vary from day to day. The price and availability of a airline connection depends on the two cities and involved. Every pair of cities has a ' flight schedule ' which repeats-Every few days. The length of the period may is different for each pair of cities and for each direction.

Although Trisha is a good in stealing paintings, she easily gets confused when booking airline. This is where your come in.

Input The input file contains the descriptions of several scenarios in which Trisha to escape.  Every description starts with a line containing two integers n and K. n is the number of cities through which Trisha ' s escape could take her, and K is the number of flights she would take. The cities are numbered, where 1 is Paris, her starting point, and N is Atlanta, her final destination. The numbers would satisfy and.

Next you are given n (n-1) flight schedules, one/line, describing the connection between-every possible pair of citie S. The n-1 flight schedules correspond to the flights from City 1 to all other cities (), the next n-1 lines to Those from the City 2 to all others (), and.

The description of the flight schedule itself starts with a integer d, the length of the period in, with. Following this are d non-negative integers and representing the cost of the flight between the two. A cost of 0 means this there is no flight between the two of

So, for example, the flight schedule ' 3 0 ' means ', ' and ' on the ' the ' flight costs. E is no flight in the third day it costs, and then the cycle Repeats:on the fourth day the flight costs No flight on the fifth day, etc.

The input is terminated by a scenario has n = k = 0.

output for each scenario in the input, the number of the scenario, as shown In the sample output. If It is possible for Trisha to travel K days, starting in City 1, each day flying to a different city than the day befo Re, and finally, arriving in city N, then print ' The best flight costs X. ', where x is the least Amount that's K flights can cost.

If It isn't possible to travel in such a way, print ' No flight possible.

Print a blank line after each scenario.

Sample Input

3 6 2 130 3 0 7, 0 0, 4 3, 0
135 140
2 2
3
   2 0
1
0 0

Sample Output

Scenario #1 the best
flight costs 460.

Scenario #2
No flight possible.


Miguel A. Revilla 
1998-03-10

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.