Unblocked Works continued
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 41849 Accepted Submission (s): 15463
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 INPUT3 30 1 10 2 31 2 10 23 10 1 11 2
Sample output2-1
Recently on the Bilibili to see a uestc of the great God teaching, by the way to learn the basic method. The adjacency matrix is better understood than the Freudian comparison. Because the complexity is N3 (which is the price of good understanding), the topic of my own school directly tle ... After watching Dijkstra and SPFA back to see ...
Code:
#include <iostream> #include <algorithm> #include <cstdlib> #include <sstream> #include < cstring> #include <cstdio> #include <string> #include <deque> #include <cmath> #include < queue> #include <set> #include <map>using namespace std;typedef long long ll;int mat[1000][1000];int n,m,s , T;int main (void) {Ios::sync_with_stdio (false); int I,j,k,ans; while (cin>>n>>m) {int x,y,dx; memset (Mat,0,sizeof (MAT)); For (i=0, i<n; i++) {for (j=0; j<n; J + +) {if (i==j) mat[i][j]=0;//himself to himself Of course 0 Else mat[i][j]=1e9;//is first set to infinity or not connected} for (i=0; i<m; i++) { cin>>x>>y>>dx; Mat[x][y]=min (MAT[X][Y],DX);//Distance update mat[y][x]=min (MAT[Y][X],DX); } cin>>s>>t; For (i=0, i<n; i++) {for (j=0; j<n; J + +) { for (k=0; k<n; k++) {mat[j][k]=min (mat[j][k],mat[j][i]+mat[i][k]);//State transfer }}} if (MAT[S][T]==1E9) cout<<-1<<endl; else cout<<mat[s][t]<<endl; } return 0;}
hdu--1874 unblocked project continuation (adjacency matrix Freud)