codevs 2602 最短路径问题 基础题
2017-07-28 17:07
246 查看
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 4000 (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; }
相关文章推荐
- codevs 2602 最短路径问题x
- JAVA代码—算法基础:三角形最短路径问题
- code vs 2602 最短路径问题
- wikioi 2602 最短路径问题 Dijkstar
- 2602 最短路径问题
- T2602 最短路径问题 codevs
- 2602 最短路径问题
- 【CODEVS2602】最短路径问题
- Codevs 2602 最短路径问题
- 2602最短路径问题
- Hud 3790 最短路径问题[基础最短路Dijkstra]
- 2602 最短路径问题Dihstra算法
- codevs 2602 最短路径问题 Floyd 解题报告
- Codevs 2602 最短路径问题
- 2602 最短路径问题
- 2602 最短路径问题
- nyoj-街区最短路径问题
- HDU 3790最短路径问题
- NYoj-街区最短路径问题
- HDU - 3790 最短路径问题