unblocked Works continued
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 30908 Accepted Submission (s): 11252
Problem Description
A province has been building a lot of roads since the implementation of many years of smooth engineering projects. But the road is not good, every time from one town to another town, there are many ways to choose, and some programmes are more than others to walk the distance is much shorter. This makes pedestrians very troubled.
Now that you know the starting and ending points, you can figure out how much distance you need to walk from the start to the end.
Input
This topic contains multiple sets of data, please handle to the end of the file.
The first row of each group of data contains two positive integers N and M (0<n<200,0<m<1000), representing the number of existing towns and the number of roads that have been built. The towns were numbered 0~n-1.
Next is the M-Line road information. Each line has three integers a,b,x (0<=a,b<n,a!=b,0<x<10000), indicating that there is a two-way road with X length between town A and town B.
The next line has two integer s,t (0<=s,t<n), representing the starting and ending points, respectively.
Output
For each set of data, output the shortest distance to walk in a row. If there is no route from S to T, then output-1.
Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
Sample Output
2
-1
Author
Linle
Source
2008 Zhejiang University Postgraduate second-round warm-up (2)--Full true simulation
The main topic: give you n points, m two-way side. Give you the beginning S and the end point T, and ask for the shortest path of s to T.
Idea: Find the shortest path between a pair of vertices. Use the Dijkstra algorithm to do it. Here are a few things to note about this problem:
(1) Pay attention to the heavy edge situation, (2) Note the case of s = = T, the output is 0; (3) When the mark K is initialized, it is never
can Mark into 0~n-1.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 220;const int INF = 10000000;int map[maxn][maxn],dist[maxn],vis[maxn];void Dijkstra (int N,int s) { int Min; memset (vis,0,sizeof (VIS)); for (int i = 0; i < N; ++i) dist[i] = Map[s][i]; Dist[s] = 0; Vis[s] = 1; for (int i = 0; i < N; ++i) {Min = INF; int k =-1; for (int j = 0; j < N; ++j) {if (!vis[j] && dist[j] < min) {min = Dist[j]; K = J; }} if (k = =-1)//Mark K must not and number equal return; Vis[k] = 1; for (int j = 0; j < N; ++j) {if (!vis[j] && map[k][j]!=inf && dist[j] > dist[k] + M Ap[k][j]) {dist[j] = Dist[k] + map[k][j]; }}}}int Main () {int n,m,u,v,w,s,t; while (~SCANF ("%d%d", &n,&m)) { for (int i = 0, i < n; ++i) for (int j = 0; j < N; ++j) map[i][j] = INF; for (int i = 0; i < N; ++i) dist[i] = INF; for (int i = 0; i < M; ++i) {scanf ("%d%d%d", &u,&v,&w); if (W < map[u][v]) map[u][v] = Map[v][u] = w; } scanf ("%d%d", &s,&t); Dijkstra (n,s); if (dist[t]! = INF) printf ("%d\n", dist[t]); else printf (" -1\n"); } return 0;}
HDU1874 unblocked Project continued "Dijkstra"