unblocked Works continued
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 process 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, in a row, the shortest distance to walk. 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
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 111111111
using namespace std;
int n,m;
int st,endd;
int dis[205],vis[205],dp[205][205];
void init ()
{for
(int i=0;i<n;i++)
{
Dis[i]=max;
vis[i]=0;
for (int j=0;j<n;j++)
{
Dp[i][j]=dp[j][i]=max;}}
}
void Dijkstra ()
{
dis[st]=0;//*** while
(1)
{
int v=-1;
for (int i=0;i<n;i++)
{
if (vis[i]==0&& (v==-1| | DIS[I]<DIS[V])
v=i;
}
if (v==-1) break
;
Vis[v]=1;
for (int i=0;i<n;i++)
{
if (dis[i]> (Dis[v]+dp[v][i]))
dis[i]=dis[v]+dp[v][i];}}
}
int main ()
{While
(~scanf ("%d%d", &n,&m))
{
init ();
while (m--)
{
int p,q,s;
scanf ("%d%d%d", &p,&q,&s);
Dp[p][q]=dp[q][p]=min (S,dp[p][q]); Prevent duplicate Paths
}
scanf ("%d%d", &st,&endd);
Dijkstra ();
if (Dis[endd]<max)
printf ("%d\n", Dis[endd]);
else
printf (" -1\n");
}
return 0;
}