Continuation-dijkstra algorithm of hdoj-1874-unblocked project

Source: Internet
Author: User

Unblocked Works continued
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 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 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.



Output
For 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
7 ·
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output
2

-1

The/*****dijkstra (Dijkstra) algorithm is a typical single-source shortest path algorithm that calculates 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==-1 -1:dist[k]);    }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 seen it before, 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;}


Continuation-dijkstra algorithm of hdoj-1874-unblocked project

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.