标签:spfa
链接:http://poj.org/problem?id=2253
题意:一个青蛙在一块石头上,看到了另一个青蛙在另一块石头上,它想跳过去找它,如果距离太远它就需要借助别的石头当跳板,两块石头之间的青蛙距离被定义成两块石头之间所有路径中最大跳跃距离的最小值,求两个青蛙之间的青蛙距离。
poj2263和它类似,链接:http://poj.org/problem?id=2263
解题报告:Here
这是最短路的变形,每两点之间都有路可以跳,更新最短路的值,权值记录成目前到这一点的最小青蛙距离就行了
spfa+邻接表写法
数组需要开大才能存下边,我RE了两发一怒之下改成50W,vis和head也跟着变大。。。memset更加费时,下回注意
#include<cstring> #include<string> #include<fstream> #include<iostream> #include<iomanip> #include<cstdio> #include<cctype> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<stack> #include<ctime> #include<cstdlib> #include<functional> #include<cmath> using namespace std; #define PI acos(-1.0) #define MAXN 510000 #define eps 1e-7 #define INF 0x7FFFFFFF #define seed 131 #define ll long long #define ull unsigned ll #define lson l,m,rt<<1 #define rson r,m+1,rt<<1|1 struct node{ int u,next; double v; }edge[MAXN]; int head[MAXN],vis[MAXN]; double point[MAXN][2]; double dist[MAXN]; int n,cnt; void add_edge(int a,int b,double c){ edge[cnt].u = b; edge[cnt].v = c; edge[cnt].next = head[a]; head[a] = cnt++; } void spfa(){ int i,j; for(i=1;i<=n;i++){ dist[i] = INF; } dist[1] = 0; memset(vis,0,sizeof(vis)); vis[1] = 1; queue<int>q; q.push(1); while(!q.empty()){ int temp = q.front(); q.pop(); vis[temp] = 0; for(i=head[temp];i!=-1;i=edge[i].next){ double xx = edge[i].v; if(dist[edge[i].u]>max(xx,dist[temp])){ dist[edge[i].u] = max(xx,dist[temp]); if(!vis[edge[i].u]){ vis[edge[i].u] = 1; q.push(edge[i].u); } } } } } int main(){ int k = 1,i; while(scanf("%d",&n),n){ memset(head,-1,sizeof(head)); cnt = 0; for(i=1;i<=n;i++){ scanf("%lf%lf",&point[i][0],&point[i][1]); for(int j=1;j<i;j++){ double temp = (point[i][0]-point[j][0])*(point[i][0]-point[j][0])+(point[i][1]-point[j][1])*(point[i][1]-point[j][1]); add_edge(i,j,sqrt(temp)); add_edge(j,i,sqrt(temp)); } } spfa(); printf("Scenario #%d\n",k++); printf("Frog Distance = %.3lf\n\n",dist[2]); } return 0; }
floyd+邻接矩阵写法
#include<cstring> #include<string> #include<fstream> #include<iostream> #include<iomanip> #include<cstdio> #include<cctype> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<stack> #include<ctime> #include<cstdlib> #include<functional> #include<cmath> using namespace std; #define PI acos(-1.0) #define MAXN 510 #define eps 1e-7 #define INF 0x7FFFFFFF #define seed 131 #define ll long long #define ull unsigned ll #define lson l,m,rt<<1 #define rson r,m+1,rt<<1|1 double edge[MAXN][MAXN]; double point[MAXN][2]; int n; void floyd(){ int i,j,k; for(k=1;k<=n;k++){ for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ edge[i][j] = min(edge[i][j],max(edge[i][k],edge[k][j])); } } } } int main(){ int k = 1,i; while(scanf("%d",&n),n){ for(i=1;i<=n;i++){ for(int j=1;j<=n;j++){ edge[i][j] = INF; } } for(i=1;i<=n;i++){ scanf("%lf%lf",&point[i][0],&point[i][1]); for(int j=1;j<i;j++){ double temp = (point[i][0]-point[j][0])*(point[i][0]-point[j][0])+(point[i][1]-point[j][1])*(point[i][1]-point[j][1]); edge[i][j] = edge[j][i] = sqrt(temp); } edge[i][i] = 0; } floyd(); printf("Scenario #%d\n",k++); printf("Frog Distance = %.3lf\n\n",edge[1][2]); } return 0; }
POJ2253&ZOJ1942--Frogger【SPFA】单源最短路变形,布布扣,bubuko.com
POJ2253&ZOJ1942--Frogger【SPFA】单源最短路变形
标签:spfa
原文地址:http://blog.csdn.net/zzzz40/article/details/38337251