今天刚刚开始学习最短路,前面几题还是比较简单的,不会像dp那么烧脑!!
不知道为什么编译错误半天~
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int Max = 1005;
const double inf = 9999999.9;
int n,stone[Max][2];
double dict[Max];
bool vis[Max];
double maxx(double x,double y) {
if(x < y)
return y;
else
return x;
}
int main() {
int tt = 1;
while(scanf("%d",&n) != EOF && n) {
memset(dict,inf,sizeof(dict));
memset(vis,false,sizeof(vis));
dict[1] = 0;
vis[1] = true;
for(int i = 1;i <= n;i++)
scanf("%d %d",&stone[i][0],&stone[i][1]);
int now = 1,k;
double mm;
for(int s = 1;s <= n;s++) {
mm = inf;
for(int i = 2;i <= n;i++) {
if(!vis[i]) {
double xx = (stone[now][0] - stone[i][0])*(stone[now][0] - stone[i][0]);
double yy = (stone[now][1] - stone[i][1])*(stone[now][1] - stone[i][1]);
double ttt = sqrt(xx+yy);
if(dict[i] > ttt)
dict[i] = ttt;
if(mm > dict[i]) {
mm = dict[i];
k = i;
}
}
}
vis[k] = true;
if(k == 2)
break;
now = k;
}
double ans = 0;
for(int s = 2;s <= n;s++)
if(vis[s])
ans = maxx(ans,dict[s]);
printf("Scenario #%d\n",tt++);
printf("Frog Distance = %.3f\n\n",ans);
//printf("%.3f\n",dict[1]);
}
}
后面放上poj discuss大神提供的测试数据
2 0 0 3 4 3 17 4 19 4 18 5 8 1 1 4 0 1 2 2 2 3 2 4 2 3 0 5 1 3 9 10 10 10 100 10 6 5 5 100 100 4 4 3 3 2 2 1 1 5 1 2 2 1 3 2 4 1 5 2 3 999 999 1 1 3 3 0 ----- Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414 Scenario #3 Frog Distance = 1.414 Scenario #4 Frog Distance = 1.000 Scenario #5 Frog Distance = 134.350 Scenario #6 Frog Distance = 1.414 Scenario #7 Frog Distance = 1408.557