标签:map getc queue 思路 [] max top c代码 i++
给起点和终点的坐标,然后给出多条地铁每一站的坐标,每站地铁只能到相邻的地铁站,地铁的速度是40km/h,人行走的速度是10km/h,求起点到终点的最小时间(给出的坐标单位是m,最后求的时间单位是分钟)
这题关键点在与建图,把图建好后跑dijkstra就很简单了,之前一直wa是想复杂了,以为要考虑起点和终点在地铁站的情况和不同线路的地铁站相交的情况,后来想不需要这么麻烦,同一条线路相邻的40,不同的10就可以了,相交的话,以10km/h走时间花费是0。
#include<vector>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<set>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<double,int> pii;
typedef pair<int,pii> PII;
const int inf = 1<<30;
const int maxn = 1e5+5;
struct Node
{
int x,y;
Node(){}
Node(int a,int b):x(a),y(b){}
};
Node beg,en;
vector<Node> node;
Node D[maxn];
struct Edge
{
int from,to;
double val;
double cost;
};
vector<Edge> G[maxn];
double getval(Node &a,Node& b)
{
return sqrt((double)(abs(a.x-b.x)*abs(a.x-b.x)+abs(a.y-b.y)*abs(a.y-b.y)));
}
double getcost(double cnt,bool issubway)
{
double ans = 0;
if(issubway){
ans = cnt/40000 * 60;
}else{
ans = cnt/10000 * 60;
}
return ans;
}
double dis[maxn];
bool vis[maxn];
int cnt = 0;
double dijkstra()
{
memset(vis,0,sizeof(vis));
fill(dis,dis+maxn,inf);
priority_queue<pii,vector<pii>,greater<pii> > Q;
Q.push(pii(0,cnt));
while(!Q.empty()){
pii t = Q.top();
Q.pop();
int u = t.second;
double d = t.first;
if(vis[u])continue;
vis[u] = 1;
for(int i=0;i<G[u].size();i++){
Edge e = G[u][i];
if(e.cost + d < dis[e.to]){
dis[e.to] = e.cost + d;
Q.push(pii(dis[e.to],e.to));
}
}
}
return dis[cnt+1];
}
int main(int argc, char const *argv[])
{
scanf("%d%d",&beg.x,&beg.y);
scanf("%d%d",&en.x,&en.y);
int x,y;
int k = 0;
Edge e;
while(~scanf("%d%d",&x,&y)){
if(x==-1&&y==-1){
for(int i=k;i<cnt-1;i++){
e.val = getval(D[i], D[i+1]);
e.cost = getcost(e.val, 1);
e.from = i;
e.to = i+1;
G[e.from].push_back(e);
swap(e.from, e.to);
G[e.from].push_back(e);
}
k = cnt;
continue;
}
D[cnt] = Node(x,y);
cnt++;
}
D[cnt] = beg;
D[cnt+1] = en;
for(int i=0;i<cnt+2;i++){
for(int j=i+1;j<cnt+2;j++){
e.val = getval(D[i], D[j]);
e.cost = getcost(e.val, 0);
e.from = i;
e.to = j;
G[e.from].push_back(e);
swap(e.from, e.to);
G[e.from].push_back(e);
}
}
double ans = dijkstra();
printf("%d\n",(int)(ans+0.5));
return 0;
}
标签:map getc queue 思路 [] max top c代码 i++
原文地址:https://www.cnblogs.com/django-lf/p/9734252.html