POJ 2502 思维建图 Dijkstra

//思维建图+Dijkstra模板
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const double INF=1e30;
const int maxv=210;
bool visit[maxv];
double dis[maxv],mmap[maxv][maxv];
struct Coordinate {
    double x,y;
} coord[maxv];
struct Node {
    int u;
    double dis;
    bool operator< (const Node rhs) const {
        return this->dis>rhs.dis;
    }
} now,temp;
double Distance(Coordinate a,Coordinate b) {
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void init() {
    memset(mmap,0x7f,sizeof mmap);//第一开始我第二个参数直接传的INF
    memset(dis,0x7f,sizeof dis);//double类型要赋值7f才为极大
    memset(visit,false,sizeof visit);
}
void Dijkstra(int s,int n,double mmap[][maxv],double dist[maxv],bool visit[]) {
    //s是源点,n是元素总个数,本模板默认存储二维数组时按照[1][1]->[n][n]存储
    int i,v;
    dist[s]=0;
    priority_queue<Node> pq;
    temp.dis=0,temp.u=s;
    pq.push(temp);
    while(!pq.empty()) {
        temp=pq.top();
        pq.pop();
        if(visit[temp.u]==true)
            continue;
        visit[temp.u]=true;
        for(v=1; v<=n; v++) {
            if(visit[v]==false&&dist[v]>dist[temp.u]+mmap[temp.u][v]) {
                dist[v]=dist[temp.u]+mmap[temp.u][v];
                now.u=v;
                now.dis=dist[v];
                pq.push(now);
            }
        }
    }
}
int main(void) {
#ifndef ONLINE_JUDGE
    freopen("E:\\input.txt","r",stdin);
#endif // ONLINE_JUDGE
    double v1=10000.0/60.0,v2=40000.0/60.0;
    while(~scanf("%lf%lf%lf%lf",&coord[1].x,&coord[1].y,&coord[2].x,&coord[2].y)) {
        int n=2,cnt1=3,x,y;
        init();
        while(~scanf("%d%d",&x,&y)){
            if(x==-1&&y==-1){
                cnt1=n+1;
                continue;
            }
            n++;
            coord[n].x=x;
            coord[n].y=y;
            if(n!=cnt1)
                mmap[n][n-1]=mmap[n-1][n]=min(mmap[n][n-1],Distance(coord[n],coord[n-1])/v2);
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                mmap[i][j]=min(mmap[i][j],Distance(coord[i],coord[j])/v1);
        Dijkstra(1,n,mmap,dis,visit);
        printf("%.0f\n",dis[2]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shadandeajian/article/details/81302051
今日推荐