POJ2253 - Frogger - Floyd-Warshall
题意比较难懂,难点也在于转换题意
题意:找a-b所有路径中最大步数里面最小的
本来先用最小生成树写的,但是没写出来:
#include<iostream>
#include<string.h>
#include<cmath>
#include<iomanip>
#define inf 0x3f3f3f3f
using namespace std;
//kruskal
int n,p;
struct edge
{
double x;
double y;
}e[220];
double addedge[4001];
int f[4001];
double pre[4001];
double cmp1(double x,double y)
{
return x<y;
}
double d(double x1,double y1,double x2,double y2)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
void init()
{
for(int i=1;i<p;i++)
f[i]=i;
return ;
}
int getf(int x)
{
if(f[x]==x)
return x;
return getf[f[x]];
}
int merge(int x,int y)
{
int t1=merge(x);
int t2=merge(y);
if(t1!=t2)
{
f[t2]=t1;
return 1;
}
return 0;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(cin>>n)
{
if(n==0)
break;
memset(e,0,sizeof(e));
memset(addedge,0,sizeof(addedge));
memset(f,0,sizeof(f));
for(int i=0;i<n;i++)
cout<<e[i].x<<e[i].y;
p=1;//p条边
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
addedge[p++]=d(e[i].x,e[i].y,e[j].x,e[j].y);
}
}
sort(addedge+1,addedge+p+1,cmp1);
init();
int minmaxx=-inf;
int countt=0;
int q=0;
for(int i=1;i<p;i++)
{
for(int j=i+1;j<p;j++)
{
if(merge(addedge[i],addedge[j])==1)//说明还未连通
{
countt++;
pre[q++]=
minmaxx=max(minmaxx,)
}
if(countt==p-2)
break;
}
}
int tt=1;
cout<<"Scenario #"<<tt++<<endl;
cout<<"Frog Distance = ";
cout<<setiosflags(ios::fixed)<<setprecision(3)<<dis<<endl;
}
return 0;
}
AC的是用那个五行代码过的:
#include<iostream>
#include<string.h>
#include<cmath>
#include<iomanip>
#define inf 0x3f3f3f3f
using namespace std;
//点点之间的距离floyd
//找a-b所有路径中最大步数里面最小的
struct edge
{
// double x;
// double y;
int x;
int y;
} e[220];
double a[220][220];
int n;
double d(int x1,int y1,int x2,int y2)
{
return sqrt(((x2-x1)*(x2-x1)*1.0+(y2-y1)*(y2-y1)*1.0)*1.0);
}
void init()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
a[i][j]=0;
else
a[i][j]=inf;
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt=1;
while(cin>>n)
{
if(n==0)
break;
memset(e,0,sizeof(e));
memset(a,0,sizeof(a));
init();
for(int i=1; i<=n; i++)//n个顶点
cin>>e[i].x>>e[i].y;
// p=1;//p条边
for(int i=1; i<=n; i++)
{
for(int j=i+1; j<=n; j++)
{
// addedge[p++]=d(e[i].x,e[i].y,e[j].x,e[j].y);
double dd=d(e[i].x,e[i].y,e[j].x,e[j].y);
if(a[i][j]>dd||a[j][i]>dd)
{
a[j][i]=a[i][j]=dd;
}
}
}
// sort(addedge+1,addedge+p+1,cmp1);
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
//if(a[i][j]a[i][k]+a[k][j])
a[i][j]=min(a[i][j],max(a[i][k],a[k][j]));
}
}
}
double dis=a[1][2];
cout<<"Scenario #"<<tt++<<endl;
cout<<"Frog Distance = ";
cout<<setiosflags(ios::fixed)<<setprecision(3)<<dis<<endl<<endl;
}
return 0;
}