POJ 2502 Subway dij
这个题的输入输出注意一下就好
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #include<vector> #include<cmath> using namespace std; typedef long long LL; const int N=4e3+5; const int INF=0x3f3f3f3f; struct Edge{ int v,next; double w; bool operator<(const Edge &e)const{ return w>e.w; } }edge[N*N*2]; int head[N],tot,n; double d[N]; void add(int u,int v,double w){ edge[tot].v=v; edge[tot].w=w; edge[tot].next=head[u]; head[u]=tot++; } priority_queue<Edge>q; bool vis[N]; double dij(int s,int t){ for(int i=1;i<=n;++i)d[i]=INF,vis[i]=0; d[s]=0,q.push(Edge{s,0,0}); while(!q.empty()){ while(!q.empty()&&vis[q.top().v])q.pop(); if(q.empty())break; int u=q.top().v; q.pop(); vis[u]=1; for(int i=head[u];~i;i=edge[i].next){ int v=edge[i].v; if(!vis[v]&&d[v]>d[u]+edge[i].w){ d[v]=d[u]+edge[i].w; q.push(Edge{v,0,d[v]}); } } } return d[t]; } struct Point{ int x,y; }p[N]; double dis(int i,int j){ return sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)); } int main(){ memset(head,-1,sizeof(head)),tot=0; scanf("%d%d%d%d",&p[1].x,&p[1].y,&p[2].x,&p[2].y); n=3; bool flag=0; while(~scanf("%d%d",&p[n].x,&p[n].y)){ if(p[n].x==-1&&p[n].y==-1){ flag=0; continue; } if(flag){ double tmp=dis(n,n-1)/40000.0; add(n,n-1,tmp),add(n-1,n,tmp); } flag=1; ++n; } --n; for(int i=1;i<=n;++i) for(int j=1;j<=n;++j){ if(i==j)continue; double tmp=dis(i,j)/10000.0; add(i,j,tmp),add(j,i,tmp); } printf("%d\n",(int)(dij(1,2)*60.0+0.5)); return 0; }