1502200905-hd-unblocked Project continued

Source: Internet
Author: User

Unblocked Works continuedTime limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 31707 Accepted Submission (s): 11585

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 30 1 10 2 31 2 10 23 10 1 11 2
 
Sample Output
2-1
Thinking of solving problemsThis is a way to find the shortest path, you can use the Floyd algorithm, and I use the Dijkstra algorithm, the advantages of this algorithm is to save time, the shortest path of a moving problem can be solved with Dijkstra, the disadvantage is not to deal with negative power path. Dijkstra algorithm for the shortest path, not to say step by step to find the shortest distance add, but starting from the STA, find unmarked and nearest point, mark it, with K to J distance and Sta to J to compare the distance to judge update, repeat the above action. If there is no distance from S to E, i.e. they do not have a path directly, then their shortest distance is the INF that is initially initialized, that is, the output-1 Code
#include <stdio.h> #include <string.h> #define INF 999999 <span style= "color: #ff0000;" &GT;//macro definition does not add;</span> int map[201][201],min[201];int ok[201];int main () {int n,m;int a,b,c;int sta,end;int i,j,k; int Nmin;while (scanf ("%d%d", &n,&m)!=eof) {for (i=0;i<n;i++) for (j=0;j<n;j++) Map[i][j]=inf;<span style= "COLOR: #ff0000;" >//1, the distance between the two places is initialized to Infinity </span>for (i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); if (Map[a][b] &GT;C) {map[a][b]=map[b][a]=c;} <span style= "color: #ff0000;" >//2, this step is judged to avoid the occurrence of a number of different distances between the road </span>}scanf ("%d%d", &sta,&end); memset (ok,0,sizeof (OK)); <span style= "COLOR: #ff0000;" >//3, the initial all minimum distance is not determined, if OK is marked as 1</span>for (i=0;i<n;i++) Min[i]=map[sta][i];min[sta]=0;<span style= " Color: #ff0000; " >//4, the initial min array is the distance from the STA to the various places (to their own distance of 0) </span>for (i=0;i<n;i++) {nmin=inf;for (j=0;j<n;j++) if (!ok[j]&    &nmin>min[j]) {k=j;    NMIN=MIN[J]; }<span style= "color: #ff0000;" >//5. Find a closer distance apart from the shortest distance </span>if (nmin==inf) break;ok[k]=1;<span style= "color: #ff0000;" >//6, determine it as STA to K shortest distance </span>for (j=0;j<n;j++) if (!ok[j]&&min[j]>min[k]+map[k][j]) min[j]= Min[k]+map[k][j];<span style= "color: #ff0000;" >//7, constantly updated STA to J distance </span>}if (Min[end]==inf) printf ("%d\n", -1); else printf ("%d\n", Min[end]); }return 0;}



1502200905-hd-unblocked Project continued

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.