poj2502 最短路

题意:一个人要从家到学校,步行速度10km/h,图上有地铁40km/h,地铁有不同线路,每个线路上的地铁可以互通,相邻两站之间地铁以直线运行,不同地铁线路之间不能直接通过地铁乘坐到达,但不同地点间可以直接步行,按直线走。给出家、学校、各地铁站台的坐标(单位 m),问从家到学校最短需要花费多少时间(min)

坑爹的单位…… km/h、m、min,然后需要注意地铁线路的建边需要一站一站建,因为只有相邻两站间才以直线运行,但同一条地铁线路并不一定建成一条直线。剩余没有连通的点对之间用步行相连,建完图之后用最短路就能计算了。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 #include<vector>
 5 #include<algorithm>
 6 #include<math.h>
 7 #include<map>
 8 using namespace std;
 9 typedef pair<double,int> pii;
10 const double INF=1e9;
11 
12 struct point{
13     int x,y;
14     bool operator < (const point a)const{
15         if(x==a.x)return y<a.y;
16         return x<a.x;
17     }
18     point(int a,int b):x(a),y(b){}
19 };
20 
21 int cnt;
22 double ma[205][205];
23 int vis[205][205];
24 double d[205];
25 int line[205],x[205],y[205];
26 int cl;
27 
28 struct cmp{
29     bool operator()(pii a,pii b){
30         return a.first>b.first;
31     }
32 };
33 
34 double cal(int a,int b){
35     return sqrt(1.0*(x[a]-x[b])*(x[a]-x[b])+1.0*(y[a]-y[b])*(y[a]-y[b]));
36 }
37 
38 void dij(){
39     int i;
40     priority_queue<pii,vector<pii>,cmp>q;
41     q.push(make_pair(0,1));
42     for(i=1;i<=cnt;++i)d[i]=INF;
43     d[1]=0;
44     while(!q.empty()){
45         pii u=q.top();
46         q.pop();
47         if(u.first>d[u.second])continue;
48         if(u.second==2){printf("%d
",(int)(d[2]/60.0+0.5));break;}
49         for(int j=2;j<=cnt;++j){
50             if(j==u.second)continue;
51             double v=ma[u.second][j];
52             if(d[j]>u.first+v){
53                 d[j]=u.first+v;
54                 q.push(make_pair(d[j],j));
55             }
56         }
57     }
58 }
59 
60 int main(){
61     cnt=0;
62     scanf("%d%d",&x[1],&y[1]);
63     scanf("%d%d",&x[2],&y[2]);
64     map<point,int>m;
65     m[point(x[1],y[1])]=++cnt;
66     m[point(x[2],y[2])]=++cnt;
67     int x1,y1;
68     cl=0;
69     while(scanf("%d%d",&x1,&y1)!=EOF){
70         if(x1==-1&&y1==-1){
71             for(int i=2;i<=cl;++i){
72                 int j=i-1;
73                 ma[line[j]][line[i]]=ma[line[i]][line[j]]=cal(line[i],line[j])/40.0*3.6;
74                 vis[line[i]][line[j]]=vis[line[j]][line[i]]=1;
75             }
76             cl=0;
77         }
78         else{
79             point tmp(x1,y1);
80             if(m[tmp])line[++cl]=m[tmp];
81             else{
82                 line[++cl]=m[tmp]=++cnt;
83                 x[cnt]=x1;y[cnt]=y1;
84             }
85         }
86     }
87     for(int i=1;i<=cnt;++i){
88         for(int j=i+1;j<=cnt;++j){
89             if(!vis[i][j])ma[i][j]=ma[j][i]=cal(i,j)/10.0*3.6;
90         }
91     }
92     dij();
93     return 0;
94 }
View Code
原文地址:https://www.cnblogs.com/cenariusxz/p/4785322.html