HDU 2066 traveling by a person-from lanshui_Yang

Source: Internet
Author: User

Problem Description
Although caoer is a luchi (that is, a person who has been in Hangzhou for more than a year will still be lost on campus, Khan ~), However, caoer still enjoys traveling, because he will meet many people (Prince Charming, ^ 0 ^) on the road. Many things can enrich his experience, you can also see beautiful scenery ...... Caoer wants to go to many places. She wants to go to the Tokyo Tower to see the night view, go to Venice to see the movie, go to Yangmingshan to see the taro, go to New York to see the pure snow scene, go to Paris to drink coffee and write, visit Meng jiangnv in Beijing ...... The winter vacation is approaching. You can't waste it for such a long period of time. You must give yourself a good vacation, but you can't waste your training, therefore, caoer decided to go to a desired place in the shortest time! Because caoer's home is in a small town without passing by train, she can only take a train to the neighboring city ~).


Input
There are multiple groups of input data. The first row of each group is three integers T, S, and D, indicating that there are T routes and S are adjacent cities in cao'er's home, there are D places to go;
Then there are T rows. Each row has three integers a, B, and time, indicating that the driving distance between cities a and B is time hour. (1 = <(a, B) <= 1000; there may be multiple routes between a and B)
The next line T + 1 contains the number of S, indicating the city connected to cao'er's home;
The next line T + 2 has the number D, which indicates that the grass wants to go to the place.


Output
Output the shortest time for a particular city.


For example, this is an obvious short-circuit problem, which can be solved by dijkstra and spfa. The general practice is to find the minimum value in the shortest path from all the stations that are departing to all the terminals. In this way, we call dijkstra or spfa multiple times, however, if you use some techniques, you can greatly optimize it. In the question, the numbers a and B are all greater than 1. Therefore, you can set a vertex as the home of the grass and the serial number of the vertex is 0, as long as an edge is set up between the vertex and all origin sites and the distance is 0, you only need to call dijkstra or spfa once at the source point 0. I used spfa. Ah, this question WA has been used for countless times, but I couldn't find the error. Finally, I suddenly thought, some destinations may be isolated points (here, the points that cannot be reached by the grass, that is, those that have not appeared before ). See the Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <algorithm> # include <queue> # include <cmath> # include <map> using namespace std; const int MAXN = 1005; const int INF = 0x7fffffff; int vis [MAXN]; // mark the array to check whether struct Node {int adj; int dist exists in the city; node * next;}; Node * vert [MAXN]; queue <int> q; int m, st, dt; int dest [MAXN]; int ss [MAXN]; // record the number of cities on the Origin Site int dd [MAXN]; // record the end point Number of cities on the site int dis [MAXN]; int inq [MAXN]; int sumc; // record the number of cities that appear void spfa (int v0) {Node * p; int I; for (I = 0; I <= sumc; I ++) {dis [dest [I] = INF;} dis [0] = 0; while (! Q. empty () {q. pop () ;}q. push (v0); inq [v0] ++; while (! Q. empty () {int tmp = q. front (); q. pop (); inq [tmp] --; p = vert [tmp]; while (p! = NULL) {int td = p-> dist; int tadj = p-> adj; if (td + dis [tmp] <dis [tadj]) {dis [tadj] = td + dis [tmp]; if (inq [tadj] = 0) {inq [tadj] ++; q. push (tadj) ;}} p = p-> next ;}} void dele () // Delete the adjacent table {Node * p; int I; for (I = 0; I <= sumc; I ++) {if (I = 0) p = vert [0]; else p = vert [dest [I]; while (p! = NULL) {vert [dest [I] = p-> next; delete p; p = vert [dest [I] ;}} int main () {while (scanf ("% d", & m, & st, & dt )! = EOF) {memset (vis, 0, sizeof (vis); memset (vert, 0, sizeof (vert); memset (dest, 0, sizeof (dest )); memset (dis, 0, sizeof (dis); memset (inq, 0, sizeof (inq); int I; sumc = 0; Node * p; for (I = 0; I <m; I ++) {int a, B, w; cin> a> B> w; if (! Vis [a]) {vis [a] = 1; sumc ++; dest [sumc] = a;} if (! Vis [B]) {vis [B] = 1; sumc ++; dest [sumc] = B;} p = new Node; p-> adj = B; p-> dist = w; p-> next = vert [a]; vert [a] = p; p = new Node; p-> adj =; p-> dist = w; p-> next = vert [B]; vert [B] = p;} for (I = 0; I <st; I ++) {scanf ("% d", & ss [I]); if (! Vis [ss [I]) // do not forget to judge {vis [ss [I] = 1; sumc ++ here; dest [sumc] = ss [I];} p = new Node; p-> adj = ss [I]; p-> dist = 0; p-> next = vert [0]; vert [0] = p; p = new Node; p-> adj = 0; p-> dist = 0; p-> next = vert [ss [I]; vert [ss [I] = p;} for (I = 0; I <dt; I ++) {scanf ("% d", & dd [I]); if (! Vis [dd [I]) // do not forget to judge here. The Terminal City may appear for the first time {vis [dd [I] = 1; sumc ++; dest [sumc] = dd [I] ;}} spfa (0); int min = INF; for (I = 0; I <dt; I ++) {if (min> dis [dd [I]) {min = dis [dd [I] ;}} printf ("% d \ n", min ); dele ();} return 0 ;}

 

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.