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)