HDU 1874 smooth engineering continued

Source: Internet
Author: User

DijkstraAlgorithmTemplate question, Xi, the first most short-circuit, template question, originally fantasized about 1A, but unfortunately, one condition is missing, a very important condition, since it is the shortest short circuit, if there is a duplicate edge, of course, it is to save a shorter edge. As a result, I did not notice that it directly covered

Tragedy ,........................

After reading the four Shortest Path Algorithms for almost a day, I finally got a little bit of experience. Thanks to the detailed explanation on Daniel's blog

Http://www.wutianqi.com /? P = 1890 Dijkstra Algorithm Implementation

Http://www.wutianqi.com /? P = 1912 Bellman-Ford Algorithm

Http://www.wutianqi.com /? P = 2285Spfa (Shortest Path faster
Algorithm) Optimization of the previous Algorithm

// The above three are in-depth, with the single-source shortest path

Http://www.wutianqi.com /? P = 1903

// This is the requestReturns the shortest distance between any two points.

# Include <iostream> # include <string> using namespace STD; bool s [200]; int C [200] [200], DIS [200], n, m, S, t; void Dijkstra () {for (INT I = 0; I <n; I ++) {dis [I] = C [s] [I];} dis [s] = 0; For (INT I = 1; I <n; I ++) {int TMP = int_max, V = s; For (Int J = 0; j <n; j ++) if (! S [J] & dis [J] <TMP) {TMP = dis [J]; V = J;} s [v] = 1; for (Int J = 0; j <n; j ++) if (! S [J] & C [v] [J] <int_max) {int newdis = dis [v] + C [v] [J]; if (newdis <dis [J]) dis [J] = newdis ;}}int main () {int Len; while (CIN >>n >> m) {for (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) c [I] [J] = int_max; int A, B; For (int K = 0; k <m; k ++) {CIN> A> B> Len; If (LEN <C [a] [B]) {C [a] [B] = Len; c [B] [a] = Len ;}} CIN >>> t; memset (S, 0, sizeof (s); s [s] = 1; dijkstra (); If (DIS [T] <int_max) cout <dis [T] <Endl; else cout <-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.