hdoj-1874-unblocked project Continuation-dijkstra algorithm

Source: Internet
Author: User

Smooth Project Continuation
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 28356 Accepted Submission (s): 10275
Problem Description
A province has built up a lot of roads since it implemented a project plan that has been unblocked for many years. Just a lot of road is not good, every time from a town to a town, there are many ways to choose the path, and some of the programmes are more than some of the distance to walk a lot shorter. This is very disturbing to pedestrians.


Now that you know the starting point and end point, you have to figure out how much distance you need to walk from the start to the end.



Input
This topic includes multiple sets of data, please handle to the end of the file.
The first row of each group of data consists of 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 you need to walk in a row. Assuming there is no route from S to T, the output is-1.


Sample Input
3 3
0 1 1
0 2 3
1 2 1
2 p
3 1
0 1 1
1 2

Sample Output
2

-1

The </pre><pre name= "code" class= "CPP" >/*****dijkstra (Dijkstra) algorithm is a typical single-source shortest path algorithm for calculating the shortest path of a node to all other nodes. *****/# include<iostream> #include <stdio.h> #include <string.h> #define N 100000000using namespace std;               int Dist[205],map[205][205];bool visit[205]; BOOL Boolean variable int n,m;void init ()//init is usually used as an abbreviation for initialization.     That is: Set the initial value, initialize the meaning {int i,j;          For (i=0, i<n; i++) {for (j=0; j<n; j + +) Map[i][j]=n;          Dist[i]=n;     Visit[i]=false;    }}int Dijkstra (int s,int t) {int mim,k,i;    dist[s]=0;    I=1;    memset (visit,false,sizeof (visit));           while (true) {visit[k]=true;           if (k==t) break; for (i=0; i<n; i++) if (!visit[i]&&map[k][i]!=n) dist[i]=min (dist[i],dist[k]+           Map[k][i]);           Mim=n;           K=-1;                   for (i=0; i<n; i++) {if (!visit[i]&&mim>dist[i]) { Mim=dist[i];                K=i;               }} if (K==-1) break; } return k==t?            DIST[K]:-1;    }int Main () {int s,t,i,j,x;           while (scanf ("%d%d", &n,&m)!=eof) {init ();                  while (m--) {scanf ("%d%d%d", &i,&j,&x); if (X<map[i][j])//Select Small side, here is the wrong one.                      I've met but I haven't noticed.           Map[i][j]=map[j][i]=x;           } scanf ("%d%d", &s,&t);          printf ("%d\n", Dijkstra (s,t)); } return 0;}

hdoj-1874-unblocked project Continuation-dijkstra algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.