poj2686 (traveling by Stagecoach) state compression DP

Source: Internet
Author: User
Tags x2 y2

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 43 1 21 2 102 3 303 4 202 4 4 2 13 12 3 31 3 34 1 24 2 52 4 3 4 15 51 2 102 3 103 4 101 2 0 1 218 5 10 1 52 7 1 8 4 5 6 31 2 52 3 43 4 74 5 31 3 252 4 233 5 221 4 452 5 511 5 990 0 0 0 0

Sample Output

30.0003.667impossibleimpossible2.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


Test instructions: M City, P Road, tell you the length of each road, give you n tickets, each ticket has a speed. One way must be to use a ticket, time is distance/speed. The shortest time required from a to B.


Puzzle: State compression dp,dp[s][v] means that the remaining ticket set is S, the shortest time in V City, then

DP[S][V] = min{Dp[s-{i}][u] + d (V, u)}


#include <iostream> #include <sstream> #include <fstream> #include <string> #include <map > #include <vector> #include <list> #include <set> #include <stack> #include <queue># Include <deque> #include <algorithm> #include <functional> #include <iomanip> #include < limits> #include <new> #include <utility> #include <iterator> #include <cstdio> #include < cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime>using namespace Std;const int INF = 0x3f3f3f3f;const int MAXN = 10;const int maxm = 35;double dp[(1&LT;&LT;MAXN) -1][maxm];int d[    Maxm][maxm];int t[maxn];int vis[maxm];int N, M, p, a, b;double rec (int s, int v) {Vis[v] = 1;    if (Dp[s][v] >= 0) return dp[s][v];    if (v = = b) return dp[s][v] = 0;    Double res = INF; for (int u = 0, u < m; ++u) for (int i = 0; i < n; ++i) if (u! = v &&!vis[u] && (1<<i) &s && D[u][v] < INF) {Vis[u] = 1;                res = min (res, rec ((1<<i) ^s, u) +d[v][u]*1.0/t[i]);            Vis[u] = 0; } return Dp[s][v] = res;}        int main () {while (CIN >> n >> m >> p >> a >> b && (n | | m | | p | | a | | b)) {        a--;        b--;        for (int i = 0; i < n; ++i) scanf ("%d", &t[i]);        Fill (d[0], d[m], INF);        Fill (Vis, vis+m, 0);        Fill (dp[0], dp[1<<n],-1);            while (p--) {int x, y, dis;            scanf ("%d%d%d", &x, &y, &dis);        D[X-1][Y-1] = d[y-1][x-1] = dis;        } Double ans = rec ((1<<n)-1, a);        if (Fabs (Ans-inf) < 1e-8) printf ("impossible\n");    else printf ("%.3f\n", ans); } return 0;}


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

poj2686 (traveling by Stagecoach) state compression DP

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.