unblocked Works continued
Time limit:3000/1000ms (java/other) Memory limit:32768/32768k (java/other)
Total Submission (s): Accepted submission (s):
font:times New Roman | Verdana | Georgia
Font size:←→
problem DescriptionA 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.
InputThis 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.
OutputFor 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
AuthorLinle
Source2008 Zhejiang University Postgraduate second-round warm-up (2)--Full true simulation
#include <iostream> #include <cstring> #include <stdio.h> #include <stdlib.h> #define T 10000000
using namespace Std;
int a,b,n,c,m;
int s,tt;
int biao[250];
int map[250][250];
int num[250];
int Min;
void search () {int i,j,k;
memset (biao,0,sizeof (Biao));
for (i=0;i<n;i++) num[i]=map[s][i];
num[s]=0;
for (i=0;i<n;i++) {min=t;
for (j=0;j<n;j++) {if (biao[j]==0&&num[j]<min) {min=num[j];
K=j;
}} if (min==t) break;
Biao[k]=1;
for (j=0;j<n;j++) {if (Biao[j]==0&&num[j]>num[k]+map[k][j]) {num[j]=num[k]+map[k][j];
}}}} int main () {while (cin>>n>>m) {if (n==0&&m==0) is break;
for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) map[i][j]=t;
for (int i=1;i<=m;i++) {cin>>a>>b>>c;
if (map[a][b]>c) {map[a][b]=c;
Map[b][a]=c;
}} cin>>s>>tt;
Search (); if (num[tt]!=t) cout<<num[tt]<<endl;
Else cout<< "-1" <<endl;
} return 0; }