poj 2253
dijkstra借鉴
http://blog.csdn.net/wangjian8006/article/details/7871812
#include<stdio.h>
#include<math.h>#define N 300
#define inf 999999999
double map[N][N],dis[N];
double max(double a,double b) {
return a>b?a:b;
}
void dijkstra(int s,int t,int n) {
int visit[N],u,i,j;double min;
for(i=1;i<=n;i++) {
visit[i]=0;
dis[i]=map[s][i];
}
visit[i]=1;
for(i=1;i<n;i++) {
min=inf;
for(j=1;j<=n;j++)
if(min>dis[j]&&visit[j]==0) {
min=dis[j];
u=j;
}
visit[u]=1;
if(u==2)
return ;
for(j=1;j<=n;j++)
if(visit[j]==0&&map[u][j]<dis[j]&&dis[u]<dis[j])
dis[j]=max(map[u][j],dis[u]);
}
}
int main() {
int n,i,j,count=0;
double a[N],b[N],c;
while(scanf("%d",&n),n) {
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=inf;
for(i=1;i<=n;i++)
scanf("%lf%lf",&a[i],&b[i]);
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++) {
c=(a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]);
map[i][j]=map[j][i]=sqrt(c);
}
dijkstra(1,2,n);
printf("Scenario #%d\nFrog Distance = %.3f\n\n",++count,dis[2]);
}
return 0;
}
flord借鉴http://blog.csdn.net/lyy289065406/article/details/6645854
#include<stdio.h>
#include<math.h>
#define N 300
#define inf 999999999
double map[N][N],dis[N],a[N],b[N],c;
int main() {
int n,k,i,j,count=0;
while(scanf("%d",&n),n) {
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=inf;
for(i=1;i<=n;i++)
scanf("%lf%lf",&a[i],&b[i]);
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++) {
c=(a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]);
map[i][j]=map[j][i]=sqrt(c);
}
for(k=1;k<=n;k++)
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(map[i][j]>map[i][k]&&map[i][j]>map[k][j]) {
if(map[i][k]>map[k][j])
map[i][j]=map[j][i]=map[i][k];
else
map[i][j]=map[j][i]=map[k][j];
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n",++count,map[1][2]);
}
return 0;
}
并查集
#include<stdio.h> #include<math.h> #include<stdlib.h> #define N 300 int pre[N]; int find(int n ) { if(n==pre[n]) return n; return pre[n]=find(pre[n]); } struct node { int x,y; double distance; }dis[N*N],h; int cmp(const void *a,const void *b){ return (*(struct node *)a).distance>(*(struct node *)b).distance?1:-1; } int main(){ int n,m,count=0,i,j,k,f1,f2; double c,a[N],b[N]; while(scanf("%d",&n),n) { for(i=1;i<=n;i++) pre[i]=i; for(i=1;i<=n;i++) scanf("%lf%lf",&a[i],&b[i]); m=0; for(i=1;i<n;i++) for(j=i+1;j<=n;j++) { c=(a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]); dis[m].x=i; dis[m].y=j; dis[m++].distance=sqrt(c); } qsort(dis,m,sizeof(dis[0]),cmp); i=0; while(find(1)!=find(2)) { f1=find(dis[i].x); f2=find(dis[i].y); if(f1!=f2) pre[f2]=f1; i++; } printf("Scenario #%d\n",++count); printf("Frog Distance = %.3f\n\n",dis[i-1].distance); } return 0; }
#include<stdio.h>
#include<math.h>
#define N 300
double map[N][N];
struct node {
int x,y;
}dis[N];
int pre[N],n;
double cmp(int a,int b) {
return sqrt(1.0*(dis[a].x-dis[b].x)*(dis[a].x-dis[b].x)+1.0*(dis[a].y-dis[b].y)*(dis[a].y-dis[b].y));
}
int find(int n) {
if(pre[n]!=n)
pre[n]=find(pre[n]);
return pre[n];
}
int judge(double s) {
int i,j;
for(i=1;i<=n;i++)
pre[i]=i;
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(map[i][j]<s)
pre[find(i)]=find(j);
if(find(1)==find(2))
return 1;
return 0;
}
int main() {
int t,i,j,m,count=0;
double left,right,mid;
while(scanf("%d",&n),n) {
for(i=1;i<=n;i++)
scanf("%d%d",&dis[i].x,&dis[i].y);
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
map[i][j]=cmp(i,j);
left=0;right=map[1][2];
while(right-left>1e-6) {
mid=(left+right)/2;
if(judge(mid))right=mid;
else left=mid;
}
printf("Scenario #%d\n",++count);
printf("Frog Distance = %.3f\n\n",left);
}
return 0;
}