标签:def void min 标记 main frog ble printf bre
Input
Output
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample OutputScenario #1
Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
题解:
该题所给相当于石头坐标,求两点间的距离,即求出三角形的最长边,然后在这些边中求最短路;
//因为是坐标,两点间距离为三角形最长边,求出所有最长边的最小值 #include <iostream> #include <cstdio> #include <cstring> #define inf 1e18 #include<algorithm> #include<cmath> using namespace std ; int n; int w[250][250],dis[250]; //w数组记录i-j的距离 ,dis数组记录并更新 bool vis[250]; //标记作用, struct node { int x; int y; }p[250]; double dist(node a,node b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } void dijkstra() //核心代码 { memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) dis[i]=w[1][i]; //初始化dis数组,1到各个顶点的距离 for(int i=1;i<=n;i++) { int u,m=inf; for(int j=1;j<=n;j++) if(!vis[j]&&dis[j]<=m) m=dis[u=j]; vis[u]=1; for(int j=1;j<=n;j++) { int k=max(dis[u],w[u][j]); dis[j]=min(k,dis[j]); } } } int main() { int q=1; while(cin>>n) { if(n==0)break; for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) w[i][j]=dist(p[i],p[j]); dijkstra(); cout<<"Scenario #"<<q++<<endl; cout<<"Frog Distance = "; printf("%.3f\n\n",sqrt(dis[2])); } return 0; }
标签:def void min 标记 main frog ble printf bre
原文地址:https://www.cnblogs.com/ylrwj/p/10679295.html