codevs 2602 最短路径问题 基础题

本文介绍了一种平面上寻找两点间最短路径的问题,并提供了三种不同的算法实现:SPFA、Dijkstra和Ford-Fulkerson算法。通过实例展示了如何计算两点间最短距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2602 最短路径问题

 

 时间限制: 1 s

 空间限制: 32000 KB

 题目等级 : 黄金 Gold

题解

题目描述 Description

平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间。其中的一些点之间有连线。若有连线,则表示可从一个点到达另一个点,即两点间有通路,通路的距离为两点间的直线距离。现在的任务是找出从一点到另一点之间的最短路径。

输入描述 Input Description

第一行为整数n。

第2行到第n+1行(共n行),每行两个整数x和y,描述了一个点的坐标。

    第n+2行为一个整数m,表示图中连线的个数。

    此后的m行,每行描述一条连线,由两个整数i和j组成,表示第i个点和第j个点之间有连线。

    最后一行:两个整数s和t,分别表示源点和目标点。

输出描述 Output Description

仅一行,一个实数(保留两位小数),表示从s到t的最短路径长度。

样例输入 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.41

基础题,不多废话了,下面附三种算法。

 

spfa


#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
const int INF=1<<30;
typedef long long LL;
using namespace std;
const int maxn=105;
struct Point{
	int x,y;
}P[maxn];
struct Edge
{
	int e;	//终点 
	double w; //权值 
	Edge(int _e,double _w):e(_e),w(_w){}
	Edge(){}
};
int N,M,S,T;
vector<Edge> G[maxn];
double dist[maxn];
double GetW(const Point &a,const Point &b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

void spfa(int v)
{
	for(int i=0;i<=N;i++) dist[i]=INF;
	dist[v]=0;
	queue<int> q;
	q.push(v);
	while(!q.empty())
	{
		int s=q.front();q.pop();
		for(int i=0;i<G[s].size();i++)
		{
			int e=G[s][i].e;
			if(dist[e]>dist[s]+G[s][i].w)
			{
				dist[e]=dist[s]+G[s][i].w;
				q.push(e);
			}
		}
	} 
}

int main()
{

	cin>>N;
	for(int i=1;i<=N;i++) cin>>P[i].x>>P[i].y;
	cin>>M;
	int s,e;
	double w;
	for(int i=0;i<M;i++) 
	{
		cin>>s>>e;
		w=GetW(P[s],P[e]);
		G[s].push_back(Edge(e,w));
		G[e].push_back(Edge(s,w));
	}
	cin>>S>>T;
	
	spfa(S);
	printf("%.2lf\n",dist[T]);
	
	return 0;
}

dijkstra


#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
const int INF=1<<30;
typedef long long LL;
using namespace std;
const int maxn=105;
struct Point{
	int x,y;
}P[maxn];
struct Edge
{
	int e;	//终点 
	double w; //权值 
	Edge(int _e,double _w):e(_e),w(_w){}
	Edge(){}
	bool operator < (const Edge &v)const{
		return w>v.w;
	}
};
priority_queue<Edge> q;
int N,M,S,T;
vector<vector<Edge> > v; //整个图的邻接表 
int Used[maxn];

double GetW(const Point &a,const Point &b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

void dijkstra()
{
	q.push(Edge(S,0)); //源点到自己的距离为0
	while(!q.empty()) 
	{
		Edge t=q.top();q.pop();
		if(Used[t.e]) continue; //已经求出了最短路 
		if(t.e==T) 
		{
			printf("%.2lf\n",t.w);
			return ;
		}
		Used[t.e]=1;
		for(int i=0;i<v[t.e].size();i++)
		{
			int e=v[t.e][i].e;
			double w=v[t.e][i].w;
			if(!Used[e]) q.push(Edge(e,t.w+w));
		
		}
	}
}


int main()
{

	cin>>N;
	v.clear();v.resize(N+1);
	memset(Used,0,sizeof(Used));
	for(int i=1;i<=N;i++) cin>>P[i].x>>P[i].y;
	cin>>M;
	int s,e;
	double w;
	for(int i=0;i<M;i++) 
	{
		cin>>s>>e;
		w=GetW(P[s],P[e]);
		v[s].push_back(Edge(e,w));
		v[e].push_back(Edge(s,w));
	}
	cin>>S>>T;
	
	dijkstra();	
		
	return 0;
}

ford


#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
const int INF=1<<30;
typedef long long LL;
using namespace std;
const int maxn=105;
struct Point{
	int x,y;
}P[maxn];
int N,M,S,T;
double G[maxn][maxn];
double dist[maxn][maxn];

double GetW(const Point &a,const Point &b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

void ford()
{
	for(int k=1;k<=N;k++)
	{
		for(int i=1;i<=N;i++)
		{
			for(int j=1;j<=N;j++)
			{
//				if(i!=j&&i!=k&&j!=k) 
					G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
			}
		}
	}
}

int main()
{
	
	cin>>N;
	for(int i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			G[i][j]=INF;
	
	for(int i=1;i<=N;i++) cin>>P[i].x>>P[i].y;
	cin>>M;
	int s,e;
	double w;
	for(int i=1;i<=M;i++) 
	{
		cin>>s>>e;
		w=GetW(P[s],P[e]);
		G[s][e]=G[e][s]=w;
	}
	cin>>S>>T;

	ford();
		
	printf("%.2lf\n",G[S][T]);
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值