標籤:
題意:給出一堆點,求從起點到終點的所有通路中相鄰點的距離的最大值的最小值。(意思就是自己百度吧……)
解法:用相鄰點的最大值作為權值代替路徑的距離跑最短路或者最小產生樹。然後我寫了一個我以為是最佳化過的dijkstra但好像是prim的東西- -啊差不多啦……
總之用優先隊列維護權值進行廣搜……然後交G++一直wa也不知道為啥……交了C++就過了……
代碼:
#include<stdio.h>#include<iostream>#include<algorithm>#include<string>#include<string.h>#include<math.h>#include<limits.h>#include<time.h>#include<stdlib.h>#include<map>#include<queue>#include<set>#include<stack>#include<vector>#define LL long longusing namespace std;struct node{ int x, y;}p[205];struct node1{ int point; int step; node1(int point, int step) : point(point), step(step) {} node1() {} bool operator < (const node1 &tmp) const { return step > tmp.step; }};int n;int caldis(node a, node b){ return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);}int dis[205][205];vector <int> edge[205];bool vis[205];double bfs(){ memset(vis, 0, sizeof vis); priority_queue <node1> q; q.push(node1(0, 0.0)); while(!q.empty()) { node1 tmp = q.top(); q.pop(); vis[tmp.point] = 1; if(tmp.point == 1) return tmp.step; for(int i = 0; i < edge[tmp.point].size(); i++) { if(!vis[edge[tmp.point][i]]) q.push(node1(edge[tmp.point][i], max(tmp.step, dis[tmp.point][edge[tmp.point][i]]))); } }}int main(){ int cse = 1; while(~scanf("%d", &n) && n) { for(int i = 0; i < n; i++) scanf("%d%d", &p[i].x, &p[i].y); int maxn = 0.0; for(int i = 0; i < n; i++) edge[i].clear(); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) { if(i == j) continue; int tmp = caldis(p[i], p[j]); edge[i].push_back(j); dis[i][j] = tmp; } double ans = bfs(); printf("Scenario #%d\nFrog Distance = %.3lf\n\n", cse++, sqrt(ans)); } return 0;}
POJ 2253 Difference of Clustering