Unblocked Works continuedTime
limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 36731 Accepted Submission (s): 13502
Problem description a province since the implementation of many years of smooth engineering plans, finally built a lot of roads. 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 30 1 10 2 31 2 10 23 10 1 11 2
Sample Output
2-1
Idea: Also belong to a template tutorial problem, but one thing to pay attention to is to consider the beginning point of the same situation. AC Code:
#include <stdio.h> #include <string.h> #define INF 0x3f3f3f3fint Vis[210],map[210][210],dis[210],n,m,beg, End;void Init () {for (Int. i=0;i<n;i++) for (int j=0;j<n;j++) {if (i==j) map[i][j]=map[j][i]=0;elsemap[i][j]=map[j] [I]=inf;}} void Dijkstra () {int I,flag=0;memset (vis,0,sizeof (VIS)); for (i=0;i<n;i++) dis[i]=map[beg][i];vis[beg]=1;for (i=0; i<n;i++) {int j,k,temp=inf;for (j=0;j<n;j++) if (!vis[j]&&temp>dis[j]) temp=dis[k=j];if (k==end) { Flag=1;break;} Vis[k]=1;for (j=0;j<n;j++) if (!vis[j]&&dis[j]>dis[k]+map[k][j]) dis[j]=dis[k]+map[k][j];} if (flag) printf ("%d\n", Dis[end]); elseprintf (" -1\n");} int main () {while (scanf ("%d%d", &n,&m)!=eof) {int i;init (); for (i=0;i<m;i++) {int a,b,cost;scanf ("%d%d%d", &a,&b,&cost); if (Map[a][b]>cost) map[a][b]=map[b][a]=cost;} scanf ("%d%d", &beg,&end); if (beg==end) {//On this point quite deceptive, just do not consider, incredibly still have the starting point and the end of the same situation. printf ("0\n"); continue;} Dijkstra ();} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdoj--1874--unblocked Project continued