HDU 1599 find the mincost route (minimum ring and Floyd algorithm)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1599

Find the mincost route

Time Limit: 1000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2530 accepted submission (s): 1006


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 input3 31 2 12 3 11 3 13 31 2 11 2 32 3 1

 

Sample output3it's impossible. If you cannot return to the starting point, it's impossible will be output. If you can, the minimum cost will be output. I saw this question at the beginning of the competition. I thought it was the template question of dijesla. Later I thought I could not mark the question, so I gave up; I also want to directly use and check the set to see if a circle can be directly formed. Then I found that the minimum cost could not be found, so I had to struggle for a long time, you can easily use the smallest ring and floydj. Refer to the description of Min ring + Floyd. Reposted on the Internet. The Improved Writing Method of Floyd can solve the problem of least ring. The time complexity is still O (N ^ 3), and the storage structure is also the adjacent matrix int mincircle = infinity; Dist = graph;
For (int K = 0; k <nvertex; ++ K) {// Add part:
For (INT I = 0; I <K; ++ I)
For (Int J = 0; j <I; ++ J)
Mincircle = min (mincircle, DIST [I] [J] + graph [J] [k] + graph [k] [I]); // The usual Floyd section: for (INT I = 0; I <nvertex; ++ I)
For (Int J = 0; j <I; ++ J) {int temp = DIST [I] [k] + disk [k] [J]; if (temp <Dist [I] [J]) Dist [I] [J] = DIST [J] [I] = temp ;}} is 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. If it is a directed graph, you only need to make a slight change. Note that two vertices in a directed graph can form a ring. Refer to the code for this question.
 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4  5 int n,node[1010][1010],map[1010][1010],Min; 6 const int INF=99999999; 7  8 int floyd() 9 {10     for (int i=1; i<=n; i++)11         for (int j=1; j<=n; j++)12         {13             node[i][j]=map[i][j];14         }15     Min=INF;16     for (int k=1; k<=n; k++)17     {18         for (int i=1; i<=k; i++)19             for (int j=1; j<i; j++)20             {21                 if (Min>node[i][j]+map[j][k]+map[k][i])22                     Min=node[i][j]+map[j][k]+map[k][i];23                 //cout<<Min<<endl;24             }25         for (int i=1; i<=n; i++)26             for (int j=1; j<=n; j++)27             {28                 if (node[i][j]>node[i][k]+node[k][j])29                     node[i][j]=node[i][k]+node[k][j];30             }31     }32     return Min;33 }34 35 int main ()36 {37     int m;38     while (~scanf("%d%d",&n,&m))39     {40         for (int i=1; i<=n; i++)41         {42             for (int j=1; j<=n; j++)43                 map[i][j]=node[i][j]=INF;44         }45         while (m--)46         {47             int a,b,c;48             scanf("%d%d%d",&a,&b,&c);49             if (map[a][b]>c)50                 map[a][b]=map[b][a]=c;51         }52         floyd();53         if (Min==INF)54             printf ("It‘s impossible.\n");55         else56             printf ("%d\n",Min);57     }58     return 0;59 }

 

 

HDU 1599 find the mincost route (minimum ring and Floyd algorithm)

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.