CSU-1307-CityTour + Dij + Kruskal

Source: Internet
Author: User

/* The Shortest Path + the minimum spanning tree. A graph is given, starting point, and ending point. Find the path from the start point to the end point (the longest section of this path must be the shortest !) Enumerate the "Longest path", which can be divided into two parts or calculated directly. */# Include <stdio. h> # include <string. h> # include <stdlib. h >#include <algorithm> # include <queue> using namespace std; const int maxn = 2005; const int maxm = 50005; const int inf = 99999999; int mat [maxn] [maxn]; int fa [maxn]; bool vis [maxn]; int dis [maxn]; struct Node {int x, y, val ;} edge [maxm <1]; int cmp (Node a, Node B) {return. val <B. val;} void init (int n) {for (int I = 1; I <= n; I ++) {fa [I] = I; for (int J = 1; j <= n; j ++) {mat [I] [j] = inf ;}} int find (int x) {if (x = fa [x]) return x; return fa [x] = find (fa [x]);} int Kruskal (int n, int m, int A, int B) {sort (edge, edge + m, cmp); int x, y; int MaxEdge = 0; for (int I = 0; I <m; I ++) {x = find (edge [I]. x); y = find (edge [I]. y); if (x! = Y) {if (x> y) fa [y] = x; else fa [x] = y; if (find (A) = find (B )) {MaxEdge = edge [I]. val; return MaxEdge;} MaxEdge = max (MaxEdge, edge [I]. val) ;}} return MaxEdge;} int Dij (int n, int MaxEdge, int A, int B) {for (int I = 1; I <= n; I ++) {vis [I] = false; dis [I] = inf;} dis [A] = 0; for (int I = 1; I <= n; I ++) {int M = inf; int id =-1; for (int j = 1; j <= n; j ++) {if (! Vis [j] & M> dis [j]) {M = dis [j]; id = j ;}} if (id =-1) break; vis [id] = true; for (int j = 1; j <= n; j ++) {if (! Vis [j] & dis [j]> dis [id] + mat [id] [j] & mat [id] [j] <= MaxEdge) {dis [j] = dis [id] + mat [id] [j] ;}} return dis [B];} int main () {// freopen ("in.txt", "r", stdin); int n, m, A, B; while (scanf ("% d ", & n, & m, & A, & B )! = EOF) {init (n); for (int I = 0; I <m; I ++) {scanf ("% d ", & edge [I]. x, & edge [I]. y, & edge [I]. val); if (mat [edge [I]. x] [edge [I]. y]> edge [I]. val) {mat [edge [I]. x] [edge [I]. y] = mat [edge [I]. y] [edge [I]. x] = edge [I]. val ;}} int MaxEdge = 0; MaxEdge = Kruskal (n, m, A, B); // printf ("MaxEdge = % d \ n", MaxEdge ); int Sum = Dij (n, MaxEdge, A, B); printf ("% d \ n", Sum );}}

 

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.