标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 34968 | Accepted: 11235 |
Description
Input
Output
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
题意:一只青蛙要从1走到2,求所有1-2的路径中最长的子段中最短的那条(minimax)。。有点难懂啊,,打个比方。
1 2 2
1 3 1.5
2 3 1
那么我们有 1 2 可以选择 ,路径长度为 2
还有 1 3 2 可以选择 路径长度 为 1.5+1 = 2.5
所以我们选择 1 3 2 答案为 1.5
这里可以用贪心的思想,利用kruskal进行添边,如果1 2 联通了就必定是这一条。。开始想复杂了,用二分+网络流去解。。结果果断TLE
#include<cstdio> #include<cstring> #include<algorithm> #include<math.h> #include<queue> #include<iostream> using namespace std; const int N = 205; struct Point{ double x,y; }p[N]; struct Edge{ int s,t; double v; }edge[N*N]; int father[N]; int n; void init(){ for(int i=1;i<=n;i++) father[i] = i; } double dis(Point a,Point b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } int _find(int x){ if(x==father[x]){ return father[x]; } return father[x] = _find(father[x]); } int cmp(Edge a,Edge b){ return a.v<b.v; } double kruskal(int m){ double MAX = -1; sort(edge+1,edge+1+m,cmp); for(int i=1;i<=m;i++){ int a = _find(edge[i].s); int b = _find(edge[i].t); if(a!=b) { father[a] = b; } if(_find(1)==_find(2)){ MAX = edge[i].v; return MAX; } } } int main() { int t = 1; while(scanf("%d",&n)!=EOF&&n) { init(); for(int i=1; i<=n; i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } int m=1; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ double d = dis(p[i],p[j]); edge[m].s = i; edge[m].t = j; edge[m++].v = d; } } m--; double res = kruskal(m); printf("Scenario #%d\nFrog Distance = %.3lf\n\n",t++,sqrt(res)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5576740.html