题意:给出地铁线 起点和 终点 坐地铁速度为v2 走路为v1 求起点到终点的最短距离 (答案需要四舍五入这里坑了好久)
拿给出的地铁站点 和起点终点建边即可 然后跑个迪杰斯特拉
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const double v1=10000.0/60; 7 const double v2=40000.0/60; 8 int n; 9 const int maxn=300+5; 10 double dist[maxn]; 11 double cost[maxn][maxn]; 12 int vis[maxn]; 13 const double INF=1e30; 14 15 struct Node{ 16 double x,y; 17 }node[maxn]; 18 19 double dis(const Node&a,const Node&b){ 20 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 21 } 22 23 void Dijkstra(){ 24 for(int i=1;i<=n;i++){ 25 dist[i]=INF; 26 } 27 memset(vis,0,sizeof(vis)); 28 dist[1]=0; 29 for(int j=0;j<n;j++){ 30 int k=-1; 31 double minnum=INF; 32 for(int i=1;i<=n;i++){ 33 if(!vis[i]&&dist[i]<minnum){ 34 minnum=dist[i]; 35 k=i; 36 } 37 } 38 if(k==-1)break; 39 vis[k]=1; 40 for(int i=1;i<=n;i++){ 41 if(!vis[i]&&dist[k]+cost[k][i]<dist[i]){ 42 dist[i]=dist[k]+cost[k][i]; 43 } 44 } 45 } 46 } 47 int main(){ 48 while(scanf("%lf%lf%lf%lf",&node[1].x,&node[1].y,&node[2].x,&node[2].y)==4){ 49 n=2; 50 int cnt1=3; 51 for(int i=1;i<maxn;i++) 52 for(int j=1;j<maxn;j++) 53 if(i!=j)cost[i][j]=INF; 54 else cost [i][j]=0; 55 int x,y; 56 bool ok=0; 57 58 while(scanf("%d%d",&x,&y)==2){ 59 if(x==-1&&y==-1){ 60 ok=0; 61 continue; 62 } 63 n++; 64 node[n].x=x; 65 node[n].y=y; 66 if(ok){ 67 cost[n][n-1]=cost[n-1][n]=min(cost[n][n-1],dis(node[n],node[n-1])/v2); 68 } 69 ok=1; 70 } 71 for(int i=1;i<=n;i++) 72 for(int j=1;j<=n;j++){ 73 cost[i][j]=cost[j][i]=min(cost[i][j],dis(node[i],node[j])/v1); 74 } 75 Dijkstra(); 76 cout << int( dist[2] + 0.5 ); 77 } 78 return 0; 79 }
原文地址:https://www.cnblogs.com/ttttttttrx/p/9742421.html
时间: 2024-11-10 12:30:04