POJ 1734 extended Floyd

Source: Internet
Author: User

Question: There are n vertices and m rows under the undirected edge. m entries have the right to expand the path learning function of the smallest ring in the undirected edge graph. It is easier to find the ring first and then relax the dfs.

// Search is really nice # include <cstdio> # include <cstring> # include <iostream> # define find_min (a, B) a <B? A: B # define N 150 # define inf 0x7ffffff using namespace std; inline int Min (int a, int B) {return a> B? B: a;} int map [N] [N], dis [N] [N], pre [N] [N], path [N], n; int main () {int I, j, k, m, u, v, d; int num; while (~ Scanf ("% d", & n, & m) {for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) map [I] [j] = inf, pre [I] [j] = I; while (m --) {scanf ("% d", & u, & v, & d ); map [u] [v] = map [v] [u] = Min (map [v] [u], d);} memcpy (dis, map, sizeof (map); int ans = inf; for (k = 1; k <= n; k ++) {for (I = 1; I <k; I ++) for (j = I + 1; j <k; j ++) {int len = dis [I] [j] + map [I] [k] + map [k] [j]; if (len <ans) {ans = len; num = 0; int now = j; while (now! = I) path [num ++] = now, now = pre [I] [now]; // pre [I] [j] indicates I-> pre [I] [j]-> j path [num ++] = I; path [num ++] = k ;}}for (I = 1; I <= n; I ++) // common relaxation k points for (j = 1; j <= n; j ++) if (dis [I] [j]> dis [I] [k] + dis [k] [j]) {dis [I] [j] = dis [I] [k] + dis [k] [j]; pre [I] [j] = pre [k] [j]; // This learner} if (ans = inf) {printf ("No solution. \ n "); continue;} for (I = 0; I <num-1; I ++) printf (" % d ", path [I]); printf ("% d \ n", path [I]);} 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.