[275] [Title] water shortest path problem

The shortest path problem

Very water a question you, but separated by nine months to get back to my very unfriendly first day, a variety of small pitfalls ah! Floyd together again seek simple coordinate distance formula. Almost a template title

topic:

Of n points in the plane, the coordinates of each point are between -10,000 to 10,000. There are a number of connection points between them. If the connection, then can be reached from one point to another point, that path between two points, distance of a straight line path distance between two points. The task now is to find the shortest path from one point to another.

Input:

Input file total n + m + 3 rows, wherein: the first behavior integer n.

Row 2 to row n + 1 (of n lines), each row two integers x and y, coordinates of a point are described.

A behavior of the integer n + 2 m, the figure represents the number of connection.

After the m rows, each row describes a connection of two integers i and j consisting, expressed the connection point between the i-th and j-th points.

Last line: two integers s and t, respectively, represent the source and destination points

Output:

Only one line of the output file, a real number (five decimal places), represents the shortest path from s to t in length.

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:

3.41421

* Note that use floating-point output ah qwq
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
int MAX=100000000;
double dist[101][101];
struct ch{
	int x;
	int y;
}a[101];
int main() {
	int n;
	int m;
	int s,t;
	int head,tail;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i].x>>a[i].y;
		cin>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			dist[i][j]=dist[j][i]=MAX;
	for(int i=1;i<=m;i++){
		cin>>head>>tail;
	dist[head][tail]=dist[tail][head]=sqrt(pow(a[head].x-a[tail].x,2.0)+pow(a[head].y-a[tail].y,2.0));
	}
	for(int k=1;k<=n;k++){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(dist[i][j]>dist[i][k]+dist[k][j])
					dist[i][j]=dist[i][k]+dist[k][j];
				}
			}
		}
	cin>>s>>t;
	printf("%.5lf\n",dist[s][t]);
		return 0;
	}
	

In memory of the first to problem solving blog qwq

Guess you like

Origin blog.csdn.net/sericon/article/details/94413622