Hdu 1599 find the mincost route (the smallest ring of an undirected graph: find the shortest path to return to the origin after traversing all nodes from a point), hdumincost

Source: Internet
Author: User

Hdu 1599 find the mincost route (the smallest ring of an undirected graph: find the shortest path to return to the origin after traversing all nodes from a point), hdumincost

Make an advertisement for yourself before writing a question ~.. Sorry, I hope you can support my CSDN video courses at the following address:

Http://edu.csdn.net/course/detail/209


Question:

Find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2801 Accepted Submission (s): 1115


Problem Description There are N scenic spots in Hangzhou, and there are some two-way links between scenic spots. Now 8600 wants to find a travel route. This route starts from and returns, assume that the route is V1, V2 ,.... VK and V1 must meet the requirements of K> 2, that is, they must go through at least two different scenic spots except the starting point, and cannot go through the same scenic spot again. Now, you need to help him find such a route, and the less the cost, the better.
 
The first line of Input is two integers, N and M (N <= 100, M <= 1000), representing the number of scenic spots and the number of roads.
In the next M row, each row contains three integers a, B, and c. representing a channel between a and B, and the cost of c is RMB 100 (c <= ).
Output: For each test instance, if such a route can be found, the minimum Output cost. If not, output "It's impossible .".
Sample Input
3 31 2 12 3 11 3 13 31 2 11 2 32 3 1
 
Sample Output
3It's impossible.
 
Author8600
SourceHDU 2007-Spring Programming Contest-Warm Up (1)
Recommend


Question Analysis:

The smallest ring of an undirected graph.

The Floyd algorithm ensures that 0 is obtained between all vertices when the outermost layer loops to k... The k-1 is the shortest path of the intermediate point. A ring has at least three vertices, set the maximum vertex of a ring number to L, and the two vertices directly connected to it in the ring are M and N (M, N <L), then the minimum ring length with the maximum number of L is Graph (M, L) + Graph (N, L) + Dist (M, N ), dist (M, N) indicates 0... L-1 vertex is the shortest path of the intermediate point, just in line with the Floyd algorithm when the outermost layer loops to k = L, then, after combining all vertices whose numbers are less than L in the M and N cycles, the minimum ring with the maximum number of L can be found. The minimum ring of the entire graph can be found through the k loop of the outermost layer. ,


It should be noted that when a Runtime Error (ACCESS_VIOLATION) Error is reported, it may be caused by a problem in Data Reading. Read by number of edges as read by number of points.


The Code is as follows:

/** D. cpp ** Created on: February 7, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <cmath> using namespace std; const int maxn = 110; const int inf = 1000000; int dist [maxn] [maxn]; int e [maxn] [maxn]; int n, m; void initial () {int I; int j; for (I = 1; I <= n; ++ I) {for (j = 1; j <= n; ++ j) {if (I = j) {e [I] [j] = 0;} else {e [I] [j] = inf ;}}} int floyd () {int I; int j; int k; int mincircle = in F; // dist = e; for (I = 1; I <= n; ++ I) {for (j = 1; j <= n; ++ j) {dist [I] [j] = e [I] [j] ;}// according to the Floyed principle, after the outermost loop is K-1, dis [I] [j] indicates the shortest path for (k = 1; k <= n; ++ k) in the path from I to j) {// the minimum length of the ring is edge [I] [k] + edge [k] [j] + I-> all the shortest path lengths smaller than k in the j Path. (I = 1; I <k; ++ I) {for (j = I + 1; j <k; ++ j) {if (dist [I] [j] + e [I] [k] + e [k] [j] <inf) {mincircle = min (mincircle, dist [I] [j] + e [j] [k] + e [k] [I]) ;}}// floyd original part, more New dist [I] [j] for (I = 1; I <= n; ++ I) {for (j = 1; j <= n; ++ j) {if (dist [I] [j]> dist [I] [k] + dist [k] [j]) {dist [I] [j] = dist [I] [k] + dist [k] [j] ;}}return mincircle;} int main () {while (scanf ("% d", & n, & m )! = EOF) {initial (); int I; for (I = 1; I <= m; ++ I) {int a; int B; int c; scanf ("% d", & a, & B, & c); if (e [a] [B]> c) {e [a] [B] = e [B] [a] = c ;}} int ans = floyd (); if (ans! = Inf) {printf ("% d \ n", ans);} else {printf ("It's impossible. \ n") ;}} 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.