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
Sample Output
#include <stdio.h> #include <string.h> #define MAX 2000001int dist[1005];int c[205][205];int n,m;bool s[205] ; int cnt = 0;void Dijkstra (int v) {int u;dist[v] = 0;for (int j = 0;j < N;j + +) {u = -1;int tmp = max;//Find the smallest worthwhile subscript for in Dist (int i = 0;i < N;i + +) if (S[i]&&dist[i] < TMP) {TMP = dist[i];u=i;} if (U = =-1) break;s[u] = false;//Update dist for (int i = 0;i < N;i + +) {int sum = C[u][i] + dist[u];if (S[i]&&dist[i] & Gt SUM) {dist[i] = sum;}}}} int main () {//freopen ("OUT.txt", "R", stdin),//freopen ("In.txt", "w", stdout), while (~SCANF ("%d%d", &n,&m)) { for (int i = 0;i < N;i +) for (int j = 0;j < N;j + +) C[i][j] = max;for (int i = 0;i < N;i + +) {Dist[i] = max;s[i] = TR UE;} for (int i = 0;i < M;i + +) {int a,b,x;scanf ("%d%d%d", &a,&b,&x); if (c[a][b]>x) {c[a][b]=x;c[b][a]=x;}} int x,y;scanf ("%d%d", &x,&y);D Ijkstra (x), if (Dist[y] >= MAX) dist[y] = -1;printf ("%d\n", dist[y],cnt + +);} return 0;}
The shortest water problem HDU 1874 unblocked works continued