Hangzhou Electric 1874-unblocked project continued (Shortest path, Dijkstra,spfa,floyd)

Source: Internet
Author: User

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

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 Input
3 30 1 10 2 31 2 10 23 10 1 11 2

Sample Output
2-1

Authorlinle
Source2008 Zhejiang University Postgraduate second-round warm-up (2)--Full true simulation


Observation point: Shortest Path (DIJKSTRA,FLOYD,SPFA)

Main topic: Now there are n towns M Road, give the starting point and end point, ask whether can reach, if can, find the shortest road length

In fact, this is a typical shortest path problem, I now Master is three methods, DIJKSTRA,SPFA and Floyd, of which Floyd is the most easy to write, but the complexity of the time is the highest, triple for loop, use before or to ponder!

Fortunately, this problem data is small, will not time out;


Here are three ways to code!


1:dijkstra

#include <cstdio> #include <cstring> #include <algorithm> #define INF 0x3f3f3f3fusing namespace std; int map[210][210],dis[210],vis[210];int m,n;void Dijkstra (int x,int y) {memset (vis,0,sizeof (VIS)); int i,j,mark,mi;for (i=0;i<m;i++) dis[i]=map[x][i];//Initialize the starting point to the shortest distance of all points vis[x]=1;//tag visited dis[x]=0;//from X so dis[x]=0 for (i=0;i<m;i++) { Mark=-1;mi=inf;for (j=0;j<m;j++) {if (!VIS[J]&AMP;&AMP;DIS[J]&LT;MI)//Find the shortest one in all roads and mark {mi=dis[j];mark=j;}} if (mark==-1)//If not found, is the end, jump out of the Loop break;vis[mark]=1;for (j=0;j<m;j++)//update each point to the access point collection of the shortest distance {if (!vis[j]&&dis [J]>dis[mark]+map[mark][j]) dis[j]=dis[mark]+map[mark][j];}} if (dis[y]==inf) printf (" -1\n"); elseprintf ("%d\n", Dis[y]);} int main () {int i,j,start,end,a,b,c;while (scanf ("%d%d", &m,&n)!=eof) {memset (map,inf,sizeof (map));// To initialize the graph first to infinity, that is, the for (i=0;i<n;i++) {scanf ("%d%d%d", &a,&b,&c), if (map[a][b]>c)//In order to prevent the input of multiple paths, Choose the shortest map[a][b]=map[b][a]=c;} scanf ("%d%d", &start,&end);//input start and end point Dijkstra (start,end);} return 0;}


2:floyd


#include <cstdio> #include <cstring> #include <algorithm> #define INF 0x3f3f3f3fusing namespace std; int map[210][210];int m,n;void Floyd (int x,int y) {int i,j,k;for (k=0;k<m;k++) {for (i=0;i<m;i++) {for (j=0;j<m;j + +) {if (Map[i][j]>map[i][k]+map[k][j])//K as the middle point, find the smallest road map[i][j]=map[i][k]+map[k][j];}} if (x==y) printf ("0\n"), else if (map[x][y]==inf) printf (" -1\n"), elseprintf ("%d\n", Map[x][y]);} int main () {int i,j,start,end,a,b,c;while (scanf ("%d%d", &m,&n)!=eof) {memset (map,inf,sizeof (map));// To initialize the graph first to infinity, that is, the for (i=0;i<n;i++) {scanf ("%d%d%d", &a,&b,&c), if (map[a][b]>c)//In order to prevent the input of multiple paths, Choose the shortest map[a][b]=map[b][a]=c;} scanf ("%d%d", &start,&end);//input start and end point Floyd (Start,end);} return 0;}


3:spfa


#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define INF 0x3f3f3f3fusing namespace Std;int dis[210],vis[210],head[210];int m,n,t;struct node {int u,v,w,next;} S[2000];void addedge (int a,int b,int c) {s[t].u=a;s[t].v=b;s[t].w=c;s[t].next=head[a];head[a]=t++;} void Spfa (int x,int y) {memset (vis,0,sizeof (Vis)); memset (dis,inf,sizeof (dis));d is[x]=0;vis[x]=1;queue<int> q; Q.push (x); while (!q.empty ()) {int U=q.front (); Q.pop (); vis[u]=0;for (int k=head[u];k!=-1;k=s[k].next)// Put the points that have not been visited and have a relationship with u in the queue {int v=s[k].v;if (DIS[V]&GT;DIS[U]+S[K].W) {dis[v]=dis[u]+s[k].w;//Update shortest distance if (!vis[v]) {vis[v]=1 ; Q.push (v);}}} if (dis[y]==inf) printf (" -1\n"); elseprintf ("%d\n", Dis[y]);} int main () {int i,j,start,end,a,b,c;while (scanf ("%d%d", &m,&n)!=eof) {memset (head,-1,sizeof (head)); T=0;for ( i=0;i<n;i++) {scanf ("%d%d%d", &a,&b,&c), Addedge (a,b,c); Addedge (b,a,c);//Establish two-way adjacency table}scanf ("%d%d", &start,&end);//input start and end SPFA (start,end);} return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hangzhou Electric 1874-unblocked project continued (Shortest path, Dijkstra,spfa,floyd)

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.