Swun 1454-hotel booking

Source: Internet
Author: User

 

Hotel BookingTime limit (Common/Java): 4000 MS/10000 MS running memory limit: 65536 Kbyte
Total submission: 1 pass the test: 1

Description

A transport company often needs to deliver goods from one city to another city. the transport company has made a special deal with a hotel chain which allows its drivers to stay in the hotels of this chain for free.
Drivers are only allowed to drive up to 10 hours a day. the transport company wants to find a route from the starting city to the destination city such that a driver can always spend the night in one of the hotels of the hotel chain, and that he needs to drive
At most 10 hours from one hotel to the next hotel (or the destination). Of course, the number of days needed to deliver the goods shoshould also be minimized.

Input

The input file contains several test cases. Each test case starts with a line containing an integerN,
(2 ≤ n ≤ 10000), The number of cities to be considered when planning the route. For simplicity, cities are numbered from 1N,
Where 1 is the starting city, andNIs the destination city. The next line contains an integerHFollowed
By the numbersC1, C2,..., CHIndicating the numbers of the cities where
Hotels of the hotel chain are located. You may assume that0 ≤ h ≤ min (n, 100). The third line of each test case contains an integerM(1
≤ MB ≤ 105
), The number of roads to be considered for planning the route. The followingMLines
Describe the roads. Each road is described by a line containing 3 IntegersA, B, T(1 ≤ a, B ≤ nAnd1
≤ T ≤ 600
), WhereA, BAre the two cities connected by the road,
AndTIs the time in minutes needed by the driver to drive from one end of the road to the other. input is
TerminatedN = 0.

Output

For each test case, print one line containing the minimum number of hotels the transport company has to book for a delivery from City 1 to CityN.
If it is impossible to find a route such that the driver has to drive at most 10 hours per day, print-1Instead.

Sample Input

6
3 2 5 3
8
1 2 400
3 2 80
3 4 301
4 5 290
5 6 139
1 3 375
2 5 462
4 6 300
3
0
2
1 2 371
2 3 230
0

Sample output

2
-1

Prompt

 

Question Source

University of Ulm

 

Question address:

Http: // 218.194.91.48/acmhome/problemdetail. do? & Method = showdetail & id = 1454

 

Two-dimensional spfa, priority queue optimization. (Actually, I do it as BFS, but it is similar to spfa)

 

 

# Include <iostream> # include <cstdio> # include <queue> # include <cstring> using namespace STD; struct edge {int NX, cost, V;} edge [210000]; int EF [10010], Tol = 0; void add_edge (int A, int B, int cost) {edge [tol]. V = B; edge [tol]. cost = cost; edge [tol]. nx = EF [a]; EF [a] = tol ++;} struct node {int U, minute, VIS; node () {}; node (INT Uu, int mm, int vv) {u = Uu; // The current location minute = mm; // The record starts from the hotel/start point, the maximum time required to reach the U location. vis = vv; // record the number of check-in times} bool friend operat Or <(node A, Node B) {// The priority is: the first place to visit a hotel with a small number of visits. If the number of visits is the same, the first one to return a. vis! = B. vis? A. vis> B. VIS:. minute> B. minute ;}}; priority_queue <node> que; int N; int has [10010]; int time [10010]; // time [v] records the shortest distance between [V location] and [start point/nearest hotel]. Int count [10010]; // count records the number of currently checked in int spfa () {int K, V, TMP; node now, nx; que. push (node (1, 0, 0); While (! Que. empty () {now = que. top (); que. pop (); If (now. U = N) return now. vis; For (k = EF [now. u]; k! =-1; k = edge [K]. NX) {v = edge [K]. v; If (count [v] <now. vis) {// when the number of visits to a hotel increases, the time recorded earlier will expire. Count [v] = now. vis; time [v] = 601; // record time is 601 minutes} TMP = now. minute + edge [K]. cost; // The time spent on arriving at V from the [start/Hotel] If (TMP> = time [v] | has [v] =-1) continue; // time is higher than the recorded time [v], so time [v] = TMP; que is not accessed. push (node (v, TMP, now. vis); If (has [v] = 1) {// before the sentence is written in continue, the sample has not passed. Then has [v] =-1; // mark the hotel that you have stayed in as no longer resident (why? Since we had a better way to stay first, why should we stay again now. You can think about it carefully.) que. push (node (v, 0, now. vis + 1) ;}}} return-1;} void Init () {int I; for (I = 1; I <= N; I ++) {has [I] = 0; EF [I] = count [I] =-1;} Tol = 0; while (! Que. Empty () que. Pop ();} int main () {int m, a, B, Len; while (~ Scanf ("% d", & N) {If (! N) break; Init (); scanf ("% d", & M); While (M --) {scanf ("% d", & ); has [a] = 1;} scanf ("% d", & M); While (M --) {scanf ("% d", &, & B, & Len); add_edge (a, B, Len); add_edge (B, A, Len);} printf ("% d \ n ", spfa ();} return 0 ;}

 

Related Article

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.