HDU 1874 unblocked Project continuation (Floyd algorithm)

Source: Internet
Author: User

Title Source: [hdu1874] (http://acm.hdu.edu.cn/showproblem.php?pid=1874)
Main topic:
Enter a n,m, indicating that there are N cities, numbering from 1 to N, there is M road, each road input connected to the two city number, a, and the length of the road X, the title said X is from a to B two-way length, it should be built without a map. Then enter the starting point S and the end point T, the shortest path from S to T, if not present, output-1.
Topic Analysis:
The basis of the shortest path problem, using the Floyd algorithm can find the shortest distance between any two vertices, give the starting point and end point, will map[s][t] output can be, to determine whether the starting point and the endpoint is connected is also very simple, see map[s][t] is equal to the initial value inf, if so, Describes a path that does not exist to two points.
AC Code:

#include <stdio.h>#include <string.h>#define INF 0x3f3f3f3f //defines a large value that is used to initialize the #define MAX //Up to how many points intN,m;//n represents the number of points, M for the edgeint Map[MAX] [MAX];//To hold the weight of the path between two pointsvoidFloyd ()//floyd Algorithm{ for(intk=0; k<n;k++) for(intI=0; i<n;i++) for(intj=0; j<n;j++)if(MapI [j]>MapI [k]+MapK [j])MapI [j]=MapI [k]+MapK [j];}intMain () { while(scanf("%d%d", &n,&m)!=eof) { for(intI=0; i<n;i++)//Initialize the map array first         for(intj=0; j<n;j++)if(I==J)MapI [j]=0;Else            MapI [J]=inf; for(intI=0; i<m;i++) {intA,b,d;scanf("%d%d%d", &a,&b,&d);if(MapA [B]>d]//Storage edge, and go to heavy{MapA [B]=d;Map[b] [A]=d;//No direction diagram}} Floyd ();intS,e;scanf("%d%d", &s,&e);if(Map[S] [E]==inf]printf(" -1\n");Else            printf("%d\n",Map[S] [E]);//The shortest distance from the output start to the end}return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1874 unblocked Project continuation (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.