2602 shortest path problem, 2602 Path Problem

Source: Internet
Author: User

2602 shortest path problem, 2602 Path Problem
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<cmath> 4 #include<cstring> 5 using namespace std; 6 struct node 7 { 8     double x; 9     double y;10     double cd;11 }a[1001];12 double map[101][101];13 double maxn=127;14 int main()15 {16     memset(map,maxn,sizeof(map));17     int n;18     scanf("%d",&n);19     for(int i=1;i<=n;i++)20     {21         scanf("%lf%lf",&a[i].x,&a[i].y);22         //a[i].cd=sqrt((pow(abs(x),2))+(pow(abs(y),2)));23     }24     int m;25     scanf("%d",&m);26     for(int i=1;i<=m;i++)27     {28         int p,q;29         scanf("%d%d",&p,&q);30         double y=sqrt(pow(a[p].x-a[q].x,2)+pow(a[p].y-a[q].y,2));31         map[p][q]=y; 32         map[q][p]=y;33     }34     for(int k=1;k<=n;k++)35     {36         for(int i=1;i<=n;i++)37         {38             for(int j=1;j<=n;j++)39             {40                 if(map[i][j]>map[i][k]+map[k][j])41                 map[i][j]=map[i][k]+map[k][j];42             }43         }44     }45     int u,v;46     scanf("%d%d",&u,&v);47     printf("%.2lf",map[u][v]);48     return 0;49 }

 

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.