Subway

POJ-2502

  • 这里除了直接相连的地铁站,其他图上所有的点都要连线,这里是走路的速度。
  • 记住最后的结果需要四舍五入,否则出错。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef pair<int,int> p;
const int INF=0x3f3f3f3f;
int sx,sy,ex,ey;
struct edge{
int to;
double cost;
int next;
};
struct node{
double dis;
int to;
node(){}
node(int a,int b):dis(a),to(b){}
bool operator<(const node& t)const{
return dis>t.dis;
}
};
edge ma[1500005];
int head[220];
int top;//指向头结点
double d[220];
int tn;//结点个数
int n=220;//最大结点数
map<pair<int,int>,int> mmap;
map<int,pair<int,int> >mmap1;
void addedge(int a,int b,double c){
ma[top].to=b;
ma[top].cost=c;
ma[top].next=head[a];
head[a]=top;
top++;
}
void dijikstra(int s){
priority_queue<node> que;
for(int i=0;i<tn;i++){
d[i]=INF;
}
d[s]=0;
que.push(node(0,s));
while(!que.empty()){
node temp=que.top();
que.pop();
int v=temp.to;
if(d[v]<temp.dis)
continue;
for(int h=head[v];h!=-1;h=ma[h].next){
edge e=ma[h];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(node(d[e.to],e.to));
}
}
}
}
bool isnode(int x,int y){
if(!mmap.count(p(x,y))){
mmap1[tn]=p(x,y);
mmap[p(x,y)]=tn++;
return true;
}else return false;
}
double G[220][220];
int main(){
memset(head,-1,sizeof(head));
top=0;
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
mmap1[tn]=p(sx,sy);
mmap[p(sx,sy)]=tn++;//0
mmap1[tn]=p(ex,ey);
mmap[p(ex,ey)]=tn++;//1
int x,y;
while(scanf("%d%d",&x,&y)!=EOF){
int nx,ny;
if(!mmap.count(p(x,y))){
mmap1[tn]=p(x,y);
mmap[p(x,y)]=tn++;
}
while(scanf("%d%d",&nx,&ny)!=EOF&&(nx!=-1||ny!=-1)){
if(!mmap.count(p(nx,ny))){
mmap1[tn]=p(nx,ny);
mmap[p(nx,ny)]=tn++;
}
double distance=sqrt((nx-x)*(nx-x)+(ny-y)*(ny-y));
distance/=40000.0;
int ttn=mmap[p(nx,ny)];
int ttnp=mmap[p(x,y)];
addedge(ttn,ttnp,distance);
addedge(ttnp,ttn,distance);
G[ttn][ttnp]=distance;
G[ttnp][ttn]=distance;
x=nx,y=ny;
}
}
for(int i=0;i<tn;i++){
for(int j=0;j<tn;j++){
if(G[i][j]==0&&i!=j){
p p1=mmap1[i];
sx=p1.first,sy=p1.second;
p p2=mmap1[j];
ex=p2.first,ey=p2.second;
double distance1=sqrt((ex-sx)*(ex-sx)+(ey-sy)*(ey-sy));
distance1/=10000.0;
addedge(i,j,distance1);
G[i][j]=distance1;
}
}
}
dijikstra(0);
int final=d[1]*60.0+0.5;
cout<<final<<endl;
//system("pause");
return 0;
}

最新文章

  1. DEBIAN下中文显示
  2. 如果因特网中的所有链路都提供可靠的交付服务,TCP可靠传输服务是多余的吗?
  3. osg::NodeVisitor中计算一个节点对应的世界变换矩阵、法向量、顶点坐标
  4. CF735C 数论\平衡树叶子节点的最大深度\贪心\斐波那契\条件归一化
  5. 在linux上通过JDBC连接ORACLE 时总是出现 java.sql.SQLRecoverableException: IO Error: Connection reset 的问题
  6. VC自动与Internet时间服务器同步更新
  7. Spring 4.x org.springframework.http.converter.json.MappingJacksonHttpMessageConverter ClassNotFoundException:
  8. 反恐训练营(LCS)
  9. 修改linux默认ssh 端口
  10. cpu95%,查找问题sql
  11. [模板]quicksort快速查找、排列算法
  12. 转:Linux(Centos)之安装Nginx及注意事项
  13. 最新Java基础面试题及答案整理
  14. Android:如何获取屏幕的宽高
  15. groovy动态类型--能力式设计
  16. 【Networking】网络编程常见问题汇总
  17. C#自定义控件 ————进度条
  18. Erlang进程堆垃圾回收机制
  19. HALCON算子1
  20. git 恢复误删的文件

热门文章

  1. P4074 [WC2013]糖果公园 树上莫队带修改
  2. zjnu1749 PAROVI (数位dp)
  3. L2-013 红色警报 (25分) 并查集复杂度
  4. css整理之-----------选择器
  5. Dapr微服务应用开发系列0:概述
  6. Rails框架学习
  7. js sort map by key
  8. O&amp;#178; &amp; O₂
  9. React Toolchains
  10. Set-Cookie &amp; Secure &amp; HttpOnly &amp; SameSite