HDU 1599find the Mincost route (Floyd algorithm, least-loop graph)

Source: Internet
Author: User

Find the Mincost route Time limit:1000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2995 Accepted Submission (s): 1204


Problem description Hangzhou has n scenic spots, there are some two-way connection between the scenic spots, now 8600 want to find a tourist route, this route from a point and finally back to a point, assuming the route is V1,v2,.... VK,V1, then must meet the k>2, that is, apart from the starting point to go through at least 2 other different scenic spots, and can not be repeated through the same scenic area. Now 8600 needs you to help him find a route like this, and the less it costs the better.

The first line of input is 2 integers n and m (n <=, M <= 1000), representing the number of scenic spots and the number of roads.
In the next M-line, each row consists of 3 integers a,b,c. Represents a path between A and B and costs C (c <= 100).
Output for each test instance, the minimum value is spent if such a route can be found. If it is not found, 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)
The topic requires a ring, so there are some improvements to the Floyd algorithm. can refer to the explanation here: http://blog.sina.com.cn/s/blog_476a25110100mag6.html
#include <stdio.h>
#include <string.h>
int n,m,mat[105][105],dis[105][105];
int main ()
{
int a,b,c,i,j,k;
while (scanf ("%d%d", &n,&m)!=eof)
{
for (i=0;i<105;i++)
for (j=0;j<105;j++)
{mat[i][j]=99999999;
dis[i][j]=99999999;
}
for (int i=0;i<m;i++)
{
scanf ("%d%d%d", &a,&b,&c);
if (mat[a][b]>c)
Mat[a][b]=mat[b][a]=dis[b][a]=dis[a][b]=c;
}
int i,k,j,temp=99999999;
for (k=1;k<=n;k++)
{
for (i=1;i<k;i++)
for (j=1;j<i;j++)
{

if (Temp>dis[i][j]+mat[i][k]+mat[k][j])
TEMP=DIS[I][J]+MAT[I][K]+MAT[K][J]; The equivalent of a ring with the shortest path found at the time of the K-th.
}
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (Dis[i][j]>dis[i][k]+dis[k][j])//To update the shortest path
DIS[I][J]=DIS[I][K]+DIS[K][J];
}
if (temp<99999999)
printf ("%d\n", temp);
else printf ("It ' s impossible.\n");
}
return 0;
}

HDU 1599find the Mincost route (Floyd algorithm, least-loop graph)

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.