HDU2544 Shortest path "Dijkstra algorithm"

Source: Internet
Author: User

Shortest Way

Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)Total Submission (s): 34628 Accepted Submission (s): 15003
Problem Description
In the annual school game, all the students who enter the finals will get a 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
The input includes 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 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0

Sample Output
3
2

Source

UESTC 6th Programming Contest Online


The main topic: give you n nodes and M road, followed by M Road, a B w indicates node A to node B weights are w.

Then, starting from Node 1, ask how long the shortest path of node n is.

Idea: Seek the shortest path of single source, use Dijkstra algorithm to do.

Dijkstra algorithm:

Divides all the points into two collections. If the shortest path to the source point s to u has been determined, the point u belongs to the set V1, otherwise it is

To the collection V2.

1. The direct distance from the source point S to the points in the graph is recorded as the minimum distance from the s to each point as the initial value, which cannot be reached as

Inf. s to s distance is 0.

2. Find a point in the set V2 point U, so that the source point S to the point U path length is shortest, you remove from the V2, plus

Into the V1. The shortest path of the current s to U is obtained.

3. Update the newly determined point u to the distance from each point V in the set V2, if the distance from S to u plus the direct distance from U to V

Distances less than the current S to V means that the newly found shortest path length is shorter than the previous one, so update this distance,

and update the drunk-down path.

4. Repeat step 2.3 until there is no point in the collection V2, or V2 does not have a point from the source point S can be reached.


#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 110;const int INF = 1000000000;int Map[maxn][maxn],pre[maxn],dist[maxn];bool vis[MAXN];//Map[] to store the graph,    Pre[] to save the node precursor, source point, end point//dist[i] is the shortest distance from the source point S to the node I//vis[i] The record point I belongs to the collection v1void Dijkstra (int n,int s) {int Min;        for (int i = 1; I <= N; ++i) {vis[i] = false;            if (i! = s) {Dist[i] = Map[s][i];        Pre[i] = s;    }} Dist[s] = 0;    Vis[s] = true;        for (int i = 1; I <= N; ++i) {Min = INF;        int k = 0;  for (int j = 1; j <= N; ++j) {if (!vis[j] && dist[j] < min) {min                = Dist[j];            K = J;        }} if (k = = 0) return;        Vis[k] = true; for (int j = 1; j <= N; ++j) {if (!vis[j] && map[k][j]! = INF && dist[j] > Dist[k]     + Map[k][j]) {           DIST[J] = Dist[k] + map[k][j];            PRE[J] = k;    }}}}int Main () {int n,m,a,b,w; while (~SCANF ("%d%d", &n,&m) && (n| |        M) {for (int i = 1, i <= N; ++i) for (int j = 1; j <= N; ++j) map[i][j] = INF; Memet (MAP); this is wrong. map,inf,sizeof        Cannot initialize this ...        memset (dist,inf,sizeof (Dist));        memset (pre,0,sizeof (pre));            for (int i = 0; i < M; ++i) {scanf ("%d%d%d", &a,&b,&w);        MAP[A][B] = Map[b][a] = w;        } Dijkstra (n,1);    printf ("%d\n", Dist[n]); } return 0;}


HDU2544 Shortest path "Dijkstra 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.