求起点到终点的通路中每天路的最大权中的最小值
最短路保证最大权联通求最小值(最短)
Floyd 524K 79MS 1170B
#include
using namespace std;
typedef struct Point
{
int x,y;
}Point;
Point p[233];
double dis[233][233];
int n;
int dcmp(double x)
{
return x < -esp? -1: x > esp;
}
double Dis(int a,int b)
{
return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)*1.0+(p[a].y-p[b].y)*(p[a].y-p[b].y)*1.0);
}
void Floyd()
{
int i,j,k;
for(k = 0; k < n; ++k)
{
for(i = 0; i < n; ++i)
{
for(j = 0; j < i; ++j)
{
if(dcmp(dis[i][j] - dis[i][k]) > 0 && dcmp(dis[i][j] - dis[k][j]) > 0)
{
dis[i][j] = dis[j][i] = max(dis[i][k],dis[k][j]);
}
}
}
}
}
int main()
{
int i,j,z = 0;
while(~scanf("%d",&n) && n)
{
for(i = 0; i < n; ++i)
{
scanf("%d %d",&p[i].x,&p[i].y);
for(j = 0; j <= i; ++j)
{
dis[j][i] = dis[i][j] = Dis(j,i);
}
}
Floyd();
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",++z,dis[0][1]);
}
return 0;
}
Dijkstra 532K 0MS 1448B
#include
using namespace std;
typedef struct Point
{
int x,y;
}Point;
Point p[233];
double dis[233][233];
double ds[233];
bool vis[233];
int n;
int dcmp(double x)
{
return x < -esp? -1: x > esp;
}
double Dis(int a,int b)
{
return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)*1.0+(p[a].y-p[b].y)*(p[a].y-p[b].y)*1.0);
}
double Dijkstra()
{
memset(vis,0,sizeof(vis));
int i,p,j;
for(i = 1; i < n; ++i) ds[i] = 111111;
ds[0] = 0;
double m;
for(i = 0; i < n; ++i)
{
m = 111111;
p = -1;
for(j = 0; j < n; ++j)
{
if(!vis[j] && m > ds[j])
{
p = j;
m = ds[j];
}
}
if(p == 1) return ds[1];
vis[p] = 1;
for(j = 0; j < n; ++j)
{
if(!vis[j] && dcmp(ds[j] - ds[p]) > 0 && dcmp(ds[j] - dis[p][j]) > 0)
{
ds[j] = max(ds[p],dis[p][j]);
}
}
}
}
int main()
{
int i,j,z = 0;
while(~scanf("%d",&n) && n)
{
for(i = 0; i < n; ++i)
{
scanf("%d %d",&p[i].x,&p[i].y);
for(j = 0; j <= i; ++j)
{
dis[j][i] = dis[i][j] = Dis(j,i);
}
}
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",++z,Dijkstra());
}
return 0;
}
SPFA 536K 16MS 1409B
#include
using namespace std;
typedef struct Point
{
int x,y;
}Point;
Point p[233];
double dis[233][233];
double ds[233];
bool vis[233];
int n;
int dcmp(double x)
{
return x < -esp? -1: x > esp;
}
double Dis(int a,int b)
{
return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)*1.0+(p[a].y-p[b].y)*(p[a].y-p[b].y)*1.0);
}
double SPFA()
{
memset(vis,0,sizeof(vis));
int u,i;
for(i = 1; i < n; ++i) ds[i] = 111111;
ds[0] = 0;
queue <int> q;
q.push(0);
while(!q.empty())
{
u = q.front();
q.pop();
vis[u] = 0;
for(i = 0; i < n; ++i)
{
if(dcmp(ds[i] - dis[u][i]) > 0 && dcmp(ds[i] - ds[u]) > 0)
{
ds[i] = max(dis[u][i],ds[u]);
if(!vis[i])
{
vis[i] = 1;
q.push(i);
}
}
}
}
return ds[1];
}
int main()
{
int i,j,z = 0;
while(~scanf("%d",&n) && n)
{
for(i = 0; i < n; ++i)
{
scanf("%d %d",&p[i].x,&p[i].y);
for(j = 0; j <= i; ++j)
{
dis[j][i] = dis[i][j] = Dis(j,i);
}
}
printf("Scenario #%d\nFrog Distance = %.3lf\n\n",++z,SPFA());
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-18 01:32:43