(Hdu step 6.2.1) Shortest path (find the shortest distance from point A to point B)

Source: Internet
Author: User

Topic:

Shortest circuit
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 274 Accepted Submission (s): 151
Problem description in the annual school game, all the students who enter the finals will get a very beautiful T-shirt. But every time our staff put demonstrating clothes from the store back to the game, it was very tiring! So now they want to find the shortest route from the store to the arena, can you help them?
Input inputs include multiple sets of data. The first row of each group of data is two integers n, m (n<=100,m<=10000), n indicates that there are several intersections on the streets of Chengdu, the intersection labeled 1 is the location of the store, the intersection labeled N is the location of the stadium, M said there are several roads in Chengdu. N=m=0 indicates the end of the input. The next m line, each line consists of 3 integers a,b,c (1<=a,b<=n,1<=c<=1000), indicating that there is a road between junction A and intersection B, and our staff needs C minutes to walk this road.
Enter a guarantee that there are at least 1 shops to the track.
Output
For each set of inputs, the output line indicates the shortest time the worker has walked from the store to the arena
Sample Input
2 11 2 33 31 2 52 3 53 1 20 0
Sample Output
32
SOURCEUESTC 6th Programming Contest Online
Recommendlcy


Topic Analysis:

Shortest path to the title, bare question. This data size should be used Dijkstra, Floyd, Bellman-ford, SPFA should all be possible. Give the wording of using Dijkstra.


If you are not very familiar with Dijkstra friends can first go to the following blog:

http://blog.csdn.net/hjd_love_zzt/article/details/26739593



The code is as follows:

/* * a.cpp * * Created on:2015 March 11 * author:administrator * * #include <iostream> #include <cstdio>usin G namespace Std;const int maxn = 105;const int inf = 9999999;int map[maxn][maxn];int visited[maxn];int dis[maxn];int n,m;i NT Target;int Dijkstra (int start) {int i;for (i = 1; I <= n; ++i) {visited[i] = false;dis[i] = Map[start][i];}  for (i = 1; i < n; ++i) {int min = Inf;int pos;int j;for (j = 1; j <= N; ++j) {if (!visited[j] && Dis[j] < Min) {min = dis[j];p os = j;}} Visited[pos] = true;for (j = 1; j <= N; ++j) {if (!visited[j] && dis[j] > Dis[pos] + map[pos][j]) {Dis[j] = di S[pos] + map[pos][j];}} return dis[target];} void init () {int I;int j;for (i = 1; I <= n; ++i) {for (j = 1; j <= N; ++j) {if (i = = j) {Map[i][j] = 0;} ELSE{MAP[I][J] = inf;}}}} int main () {while (scanf ("%d%d", &n,&m)!=eof,n| |  m) {init (); int i;for (i = 1; I <= m; ++i) {int a,b,c;scanf ("%d%d%d", &a,&b,&c), if (Map[a][b] > C) {map[a][b] = Map[b][a] = c;}}target = n;printf ("%d\n", Dijkstra (1));} return 0;}









(Hdu step 6.2.1) Shortest path (find the shortest distance from point A to point B)

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.