Hdu2066 one-person travel (the shortest path problem of multi-source and multi-sink points)

Source: Internet
Author: User
/* Train of Thought: the shortest path for multiple source points and multiple meeting points! Use the node with the minimum number-1 but the most source point as the sink point! Change the problem into the shortest path from a source point to a sink point! I forgot to initialize vector many times .... potholes */# include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <vector> # define M 1100 # define INF 0x3f3f3fusing namespace STD; struct node {int V; int tt; node () {} node (int v, int TT) {This-> V = V; this-> TT = tt ;}}; vector <node> V [m]; int d [m], vis [m]; int City [m]; int N; int T, S, D; void Dijkstra () {memset (D, 0x3f, sizeof (d); memset (VIS, 0, sizeof (VIS); D [0] = 0; vis [0] = 1; int root = 0; For (Int J = 0; j <= N; ++ J) {int minlen = inf, P, len = V [root]. size (); For (INT I = 0; I <Len; ++ I) {int u = V [root] [I]. v; If (! Vis [u] & D [u]> d [root] + V [root] [I]. TT) d [u] = d [root] + V [root] [I]. TT ;}// update the distance between all nodes connected to the root node for (INT I = 0; I <= n + 1; ++ I) // The shortest distance from 0 nodes to all nodes! If (! Vis [I] & minlen> d [I]) {P = I; minlen = d [I];} If (minlen = inf) return; root = P; vis [root] = 1 ;}} int main () {While (CIN> S> T> d) {int A, B, T; n =-1; while (s --) {CIN> A> B> T; V [A]. push_back (node (B, T); V [B]. push_back (node (A, T); n = max (n, max (A, B);} while (t --) {CIN>; V [0]. push_back (node (A, 0); V [A]. push_back (node (0, 0); n = max (n, a) ;}for (INT I = 1; I <= D; ++ I) {CIN> City [I]; n = max (n, City [I]);} For (INT I = 1; I <= D; ++ I) {v [n + 1]. push_back (node (City [I], INF); V [City [I]. push_back (node (n + 1, 0);} Dijkstra (); For (INT I = 0; I <= n + 1; ++ I) V [I]. clear (); cout <D [n + 1] <Endl;} 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.