2602 shortest path problem Dihstra algorithm, 2602 dihstra

Source: Internet
Author: User

2602 shortest path problem Dihstra algorithm, 2602 dihstra
DescriptionDescription

There are n points (n <= 100) on the plane, and the coordinates of each point are-10000 ~ In the range of 10000. Some of these points are connected. If there is a line, it means you can reach another point from one point, that is, there is a path between two points, the distance of the path is a straight line between two points. The current task is to find the shortest path from one point to another.

Input description Input Description

The first behavior is an integer n.

Rows 2nd to n + 1 (n rows in total), each line has two integers x and y, describing the coordinates of a point.

The n + 2 behavior is an integer m, indicating the number of connections in the graph.

Next m rows, each line describes a line consisting of two integers I and j, indicating that there is a line between the I and j.

Last line: two integers, s and t, respectively, represent the source point and the target point.

Output description Output Description

Only one row and one real number (with two decimal places retained) indicates the shortest path length from s to t.

Sample Input Sample Input

5

0 0

2 0

2 2

0 2

3 1

5

1 2

1 3

1 4

2 5

3 5

1 5

Sample output Sample Output

3.41

Data range and prompt Data Size & Hint
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 double db_maxn=127; 7 double maxn=127; 8 struct node 9 {10     double x;11     double y;12 }a[1001];13 double dis[1001];14 int vis[1001];15 int n;16 double map[101][101];17 void Dijkstra(int u)18 {19     memset(vis,0,sizeof(vis));20     for(int i=1;i<=n;i++)21     {22         dis[i]=map[u][i];23     }24     dis[u]=0;25     vis[u]=1;26     for(int i=1;i<n;i++)27     {28         double minn=99999999;29         int k=-1;30         for(int j=1;j<=n;j++)31         {32             if((dis[j]<=minn)&&vis[j]==0)33             {34                 minn=dis[j];35                 k=j;36             }37         }38         vis[k]=1;39         for(int j=1;j<=n;j++)40         {41             if((dis[j]>=dis[k]+map[k][j])&&vis[j]==0)42             dis[j]=dis[k]+map[k][j];43         }44     }45 }46 int main()47 {48     memset(map,db_maxn,sizeof(map));49     memset(dis,db_maxn,sizeof(dis));50     scanf("%d",&n);51     for(int i=1;i<=n;i++)52     {53         scanf("%lf%lf",&a[i].x,&a[i].y);54         //a[i].cd=sqrt((pow(abs(x),2))+(pow(abs(y),2)));55     }56     int m;57     scanf("%d",&m);58     for(int i=1;i<=m;i++)59     {60         int p,q;61         scanf("%d%d",&p,&q);62         double y=sqrt(pow(a[p].x-a[q].x,2)+pow(a[p].y-a[q].y,2));63         map[p][q]=y; 64         map[q][p]=y;65     }66     int u,v;67     scanf("%d%d",&u,&v);68     Dijkstra(u);69     printf("%0.2lf",dis[v]);70     return 0;71 }

 

Related Article

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.