标签:des style http color os io strong for ar
题意:一只青蛙在湖中一颗石头上, 它想去有另一只青蛙的石头上,但是 湖里的水很脏,它不愿意游泳,所以它要跳过去;
给出 两只青蛙所在石头的坐标, 及 湖里其他石头的坐标;任一两个坐标点间都是双向连通的。显然从1到2存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离。
#include<iostream> #include<algorithm> #include<cstdio> #include<vector> #include<queue> #include<map> #include<cmath> #include<cstring> using namespace std; #define INF 1e7 #define maxn 205 typedef long long LL; int n,x[maxn],y[maxn]; double w[maxn][maxn]; double d[maxn]; bool inq[maxn]; double dist(int x1,int y1,int x2,int y2){return sqrt((double)(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));} struct Heapnode { int u; double w; bool operator < (const Heapnode & pp) const { return w>pp.w; } }; void dijkstra(double m) { for(int i=0;i<=n;i++)d[i]=INF; d[1]=0; memset(inq,0,sizeof(inq)); priority_queue<Heapnode>q; q.push((Heapnode){1,0}); while(!q.empty()) { Heapnode tmp=q.top();q.pop(); int u=tmp.u; if(inq[u])continue; inq[u]=true; for(int i=1;i<=n;i++)if(w[u][i]<=m&&d[i]>d[u]+m-w[u][i]) { d[i]=d[u]+m-w[u][i]; if(d[2]<INF)return ; q.push((Heapnode){i,d[i]}); } } } int main() { int cas=0; while(~scanf("%d",&n)&&n) { cas++; for(int i=1;i<=n;i++) scanf("%d%d",x+i,y+i); for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) { w[i][j]=w[j][i]=(i==j?0:dist(x[i],y[i],x[j],y[j])); } printf("Scenario #%d\n",cas); double l=0,r=40000.0; while(r-l>=0.0001) { double m=(l+r)/2; dijkstra(m); if(d[2]<INF)r=m; else l=m; } printf("Frog Distance = %.3f\n\n",l); } return 0; }
#include<iostream> #include<algorithm> #include<cstdio> #include<vector> #include<queue> #include<map> #include<cmath> #include<cstring> using namespace std; #define INF 1e7 #define maxn 205 typedef long long LL; int n,x[maxn],y[maxn]; double w[maxn][maxn]; double d[maxn]; bool inq[maxn]; double dist(int x1,int y1,int x2,int y2){return sqrt((double)(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));} void spfa(double s) { for(int i=0;i<=n;i++)d[i]=(i==1?0:INF); memset(inq,0,sizeof(inq)); queue<int>q; q.push(1); while(!q.empty()) { int u=q.front();q.pop(); inq[u]=false; for(int i=1;i<=n;i++) { if(w[u][i]>s)continue; if(d[u]-w[u][i]+s<=d[i])d[i]=d[u]+s-w[u][i]; else continue; if(d[2]<INF)return ; if(!inq[i]) { inq[i]=true; q.push(i); } } } } int main() { int cas=0; while(~scanf("%d",&n)&&n) { cas++; for(int i=1;i<=n;i++) scanf("%d%d",x+i,y+i); for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) { w[i][j]=w[j][i]=(i==j?0:dist(x[i],y[i],x[j],y[j])); } printf("Scenario #%d\n",cas); double l=0,r=40000.0; while(r-l>=0.0001) { double m=(l+r)/2; spfa(m); if(d[2]<INF)r=m; else l=m; } printf("Frog Distance = %.3f\n\n",l); } return 0; }
#include<iostream> #include<algorithm> #include<cstdio> #include<vector> #include<queue> #include<map> #include<cmath> #include<cstring> using namespace std; #define INF 1e7 #define maxn 205 typedef long long LL; int n,x[maxn],y[maxn]; double w[maxn][maxn]; double d[maxn]; bool inq[maxn]; double dist(int x1,int y1,int x2,int y2){return sqrt((double)(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));} void floyd() { for(int k=1;k<=n;k++) for(int i=1;i<=n-1;i++) for(int j=i+1;j<=n;j++) { if(w[i][j]>w[i][k]&&w[i][j]>w[k][j]) { if(w[i][k]>w[k][j])w[i][j]=w[j][i]=w[i][k]; else w[i][j]=w[j][i]=w[k][j]; } } } int main() { int cas=0; while(~scanf("%d",&n)&&n) { cas++; for(int i=1;i<=n;i++) scanf("%d%d",x+i,y+i); for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) { w[i][j]=w[j][i]=(i==j?0:dist(x[i],y[i],x[j],y[j])); } printf("Scenario #%d\n",cas); floyd(); printf("Frog Distance = %.3f\n\n",w[1][2]); } return 0; }
标签:des style http color os io strong for ar
原文地址:http://blog.csdn.net/acvcla/article/details/38821595