Dijkstra算法1.2(网格点转化为无向图)

http://poj.org/problem?id=3037

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int maxn=10000+10;
#define INF 10000000000
//错误1, 这里INF 之前设为1e8 让我WA,应为最大距离为double 很可能远大于1e8
int R,C;
double v[100+5][100+5];
int height[100+5][100+5];
int dr[]={-1,1,0,0};//上下左右
int dc[]={0,0,-1,1};
 
struct Edge
{
    int from,to;
    double dist;
    Edge(int from,int to,double dist):from(from),to(to),dist(dist){}
};
 
struct HeapNode
{
    double d;
    int u;
    HeapNode(double d,int u):d(d),u(u){}
    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];
    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)
    {
        edges.push_back(Edge(from,to,dist));
        m=edges.size();
        G[from].push_back(m-1);
    }
 
    void dijkstra()
    {
        priority_queue<HeapNode> Q;
        for(int i=0;i<n;i++) d[i]=INF;
        d[0]=0.0;
        memset(done,0,sizeof(done));
        Q.push(HeapNode(d[0],0));
 
        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] > d[u] + e.dist)
                {
                    d[e.to] = d[u]+e.dist;
                    p[e.to] = G[u][i];
                    Q.push(HeapNode(d[e.to],e.to));
                }
            }
        }
    }
}DJ;
 
int main()
{
    scanf("%lf%d%d",&v[0][0],&R,&C);
    for(int i=0;i<R;i++)for(int j=0;j<C;j++)
    {
        scanf("%d",&height[i][j]);
        if(i!=0 || j!=0)
        {
            v[i][j] = v[0][0]*pow(2.0,height[0][0]-height[i][j]);//速度
        }
    }
 
    DJ.init(R*C);
 
    for(int r=0;r<R;r++)for(int c=0;c<C;c++)
    {
        for(int d=0;d<4;d++)
        {
            int nr = r+dr[d], nc=c+dc[d];
            if(nr>=0&&nr<R&&nc>=0&&nc<C)
            {
                DJ.AddEdge(r*C+c,nr*C+nc,1.0/v[r][c] );//错误2,之前写成了 r*R+c,nr*R+nc
            }
        }
    }
 
    DJ.dijkstra();
 
    printf("%.2lf\n",DJ.d[R*C-1]);
 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/83041841