poj 2253 dijkstra模板的使用

来源:互联网 发布:淘宝好评返现模板 编辑:程序博客网 时间:2024/06/09 14:57

需要对于模板进行修改

题意:给定N个点,求从1到2的所有路径中最大边长最小的解(最大值最小)

依据dijkstra单源最短路径的贪心思想

d[i] 表示从源点s到i所满足条件的值

那么 d[i] = min { max(d[k], e(k to i) ) } 

松弛的时候

        if d[i] > max(d[cur], edge)

d[i] = max(d[cur], edge)

注意数据类型int和double切换

#include <iostream>#include <vector>#include <map>#include <list>#include <set>#include <deque>#include <stack>#include <queue>#include <algorithm>#include <cmath>#include <cctype>#include <cstdio>#include <iomanip>#include <cmath>#include <cstdio>#include <iostream>#include <string>#include <sstream>#include <cstring>#include <queue>using namespace std;///宏定义const int  INF = 10000000;const int MAXN = 310;const int maxn = MAXN;///全局变量 和 函数///struct Edge{int from;int to;double dist;};struct HeapNode{int u;double d;bool operator < (const HeapNode& rhs) const{return d > rhs.d;}};struct Dijkstra{int n, m;             //点数和边数vector<Edge> edges;   //边列表vector<int> G[maxn];  //每个结点出发的边编号bool done[maxn];      //是否已永久编号//int d[maxn];          //s到各个点的距离double d[maxn];int p[maxn];          //最短路中的上一条边 (打印结果用)void init(int n){this->n = n;for (int i = 0; i <= n; i++)G[i].clear();edges.clear();}void AddEdge(int from, int to, double dist){Edge tmp;tmp.from = from;tmp.to = to;tmp.dist = dist;edges.push_back(tmp);m = edges.size();G[from].push_back(m - 1);}void dijkstra(int s){priority_queue<HeapNode> Q;for (int i = 0; i <= n; i++)d[i] = INF;d[s] = 0;memset(done, 0, sizeof(done));HeapNode tmp;tmp.d = 0;tmp.u = s;Q.push(tmp);while (!Q.empty()){HeapNode x = Q.top();Q.pop();int u = x.u;if (done[u])continue;done[u] = true;for (int i = 0; i < G[u].size(); i++){Edge &e = edges[G[u][i]];if (d[e.to] > max(d[u], e.dist)){d[e.to] = max(d[u], e.dist);//p[e.to] = G[u][i];HeapNode temp;temp.d = d[e.to];temp.u = e.to;Q.push(temp);}}}}};int n;double posx[maxn];double posy[maxn];int main(){///变量定义int i, j;int cases = 1;while (1){scanf("%d", &n);if (n == 0)break;Dijkstra dij;dij.init(n);for (i = 1; i <= n; i++){cin >> posx[i] >> posy[i];}for (i = 1; i <= n; i++){for (j = 1; j <= n; j++){if (i != j){double dist = sqrt( (posx[i] - posx[j]) * (posx[i] - posx[j]) + (posy[i] - posy[j]) * (posy[i] - posy[j]) );dij.AddEdge(i, j, dist);//dij.AddEdge(j, i, dist);}}}dij.dijkstra(1);double ans = dij.d[2];printf("Scenario #%d\n", cases++);printf("Frog Distance = %.3f\n", ans);printf("\n");}///结束return 0;}