POJ 2991 Traveling by Stagecoach

Source: Internet
Author: User

Title:
Description

Once upon a time, there was a traveler.

He plans to travel using stagecoaches (horse wagons). His starting point and destination is fixed, but he cannot determine his route. Your job in this problem are to write a program which determines the route for him.

There is several cities in the country, and a road network connecting them. If there is a road between-cities, one can travel by a stagecoach from one of the them to the other. A coach ticket is needed for a coach ride. The number of horses is specified in each of the tickets. Of course, with more horses, the coach runs faster.

At the starting point, the traveler has a number of coach tickets. By considering these tickets and the information on the road network, you should find the best possible route that takes H Im to the destination in the shortest time. The usage of coach tickets should be taken to account.

The following conditions is assumed.
A coach ride takes the traveler from one city to another directly connected by a road. In all words, on arrival to a, he must the coach.
Only one ticket can is used for a coach ride between the cities directly connected by a road.
Each ticket can is used only once.
The time needed for a coach ride is the distance between and the cities divided by the number of horses.
The time needed for the coach change should is ignored.

Input

The input consists of multiple datasets, each in the following format. The last dataset was followed by a line containing five zeros (separated by a space).

N M p a b
T1 T2 ... tn
X1 Y1 Z1
X2 Y2 Z2
...
XP YP ZP

Every input item in a dataset is a non-negative integer. If a line contains the or more input items, they is separated by a space.

n is the number of coach tickets. You can assume this number of tickets is between 1 and 8. M is the number of cities in the network. You can assume this number of cities is between 2 and 30. P is the number of roads between cities and which may be zero.

A is the city index of the starting city. B is the city index of the destination city. A is not equal to B. You can assume this all city indices in a dataset (including the above) is between 1 and M.

The second line of a dataset gives the details of the coach tickets. TI is the number of horses specified in the I-th coach ticket (1<=i<=n). You can assume this number of horses is between 1 and 10.

The following p lines give the details of the roads between cities. The I-th road connects, cities with City indices Xi and Yi, and have a distance zi (1<=i<=p). You can assume this distance is between 1 and 100.

No. Roads connect the same pair of cities. A road never connects a city with itself. Each road can is traveled in both directions.
Output

For each dataset in the input, one line should is output as specified below. An output line should not contain extra characters such as spaces.

If The traveler can reach the destination, the time needed for the best route (a route with the shortest time) should be p Rinted. The answer should not has an error greater than 0.001. You could output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.

If The traveler cannot reach the destination, the string "Impossible" should be printed. One cannot reach the destination either when there is no routes leading to the destination, or when the number of tickets is not sufficient. Note that the first letter of "impossible" was in uppercase, while the other letters was in lowercase.
Sample Input

3 4 3) 1 4
3 1 2
1 2 10
2 3 30
3 4 20
2 4 4) 2 1
3 1
2 3 3
1 3 3
4 1 2
4 2 5
2 4 3) 4 1
5 5
1 2 10
2 3 10
3 4 10
1 2 0) 1 2
1
8 5 10) 1 5
2 7 1 8 4 5 6 3
1 2 5
2 3 4
3 4 7
4 5 3
1 3 25
2 4 23
3 5 22
1 4 45
2 5 51
1 5 99
0 0 0) 0 0
Sample Output

30.000
3.667
Impossible
Impossible
2.856
Hint

Since the number of digits after the decimal point was not specified, the above result was not the only solution. For example, the following result is also acceptable.

30.0

3.66667

Impossible

Impossible

2.85595
Source

Japan 2005 Domestic
Chinese:
(the traveller plans to travel by wagon.) His country has m cities, and there are a number of roads connected between the cities. It takes a horse-drawn carriage to travel from one city to another in a neighboring city. The carriage requires the use of a ticket and only one route per ticket. The number of horses on each ticket, from one city to another city, is equal to the length of the road divided by the number of horses. Now the traveler has n tickets, and the number of horses on the first ticket is TI. One ticket can only be used once and the transfer time is ignored. Ask the shortest amount of time from city A to City B, if the output impossible cannot be reached.

#include <bits/stdc++.h> #include <iostream> #include <cstring> #include <algorithm> #
Include<iomanip> using namespace std;
const int inf=9999999;
int n,m,p,a,b;
Double t[11];
Double d[31][31];
Double dp[1025][31];
    void Solve () {for (int i=0;i< (1<<n); i++) fill (dp[i],dp[i]+m,inf);
    dp[(1<<n) -1][a-1]=0;
Double Res=inf; for (int s=0; S<= (1<<n)-1; s++) for (int s= (1<<n)-1; s>=0; s--) {for (int v=0;v<m;v++) {for (int i=0;i<n;i++) {if ( !
                (s>>i&1))
                            {for (int u=0;u<m;u++) {if (d[v][u]>=0) Dp[s][u]=min (dp[s][u],dp[s| (
                        1<<i)][u]+d[v][u]/t[i]);
                    dp[s&~ (1<<i)][u]=min (dp[s&~ (1<<i)][u],dp[s][v]+ (double) d[v][u]/t[i]);
 }
                }
            }
        }       Res=min (Res,dp[s][b-1]);
    } if (Res==inf) cout<< "Impossible" <<endl;
else Cout<<fixed<<setprecision (3) <<res<<endl;
    } int main () {Ios::sync_with_stdio (false);
        while (cin>>n>>m>>p>>a>>b) {if (n+m+p+a+b==0) break;
        for (int i=0;i<n;i++) cin>>t[i];
        Memset (d,-1,sizeof (d));
            for (int i=0;i<p;i++) {int x, y, Z;
            cin>>x>>y>>z;
        D[x-1][y-1]=d[y-1][x-1]=z;
    } solve ();
} return 0;
 }

Answer:
Challenge Program Design contest the second version of the state compression DP example, I think out is the state transfer equation is
Dp[s][u]=min (dp[s][u],dp[s| ( 1<< i)][v]+d[v][u]/t[i])
indicates that the state of the table on the body is the minimum time to reach the city U (s) without a horse ticket i) can be s| (1<< i) by the state of the previous horse ticket I and in the current city for V to reach the state, By the carriage Walk D[v][u] This road is transferred over.
Unfortunately, I wrote the code to change the half-day has not changed, the reference book code, to write out.

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.