点击打开链接
//dijkstra 关键是建图
#include <iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef pair<double,int>PII;
const int maxn=200+10;
const double INF=1e10;
struct point{
double x,y;
};
double G[maxn][maxn]; //储存时间
point pos[maxn]; //储存坐标
double d[maxn]; //储存到各点的最短时间
bool done[maxn];
int ans;
void dijkstra(int start)
{
memset(done,false,sizeof(done));
for(int i=0;i<ans;i++)
d[i]=(i==start?0:INF);
priority_queue<PII,vector<PII>,greater<PII> >pq;
pq.push(PII(d[start],start));
while(!pq.empty())
{
PII k=pq.top();
pq.pop();
int v1=k.second;
if(done[v1]) continue;
done[v1]=true;
for(int i=0;i<ans;i++)
{
if(d[i]>d[v1]+G[v1][i])
{
d[i]=d[v1]+G[v1][i];
pq.push(PII(d[i],i));
}
}
}
}
int main()
{
ans=2;
int flag=0;
cin>>pos[0].x>>pos[0].y>>pos[1].x>>pos[1].y;
memset(G,0,sizeof(G));
while(cin>>pos[ans].x>>pos[ans].y)
{
if(pos[ans].x==-1&&pos[ans].y==-1)
{
flag=0;
continue;
}
if(flag)
{
double temp=hypot(pos[ans].x-pos[ans-1].x,pos[ans].y-pos[ans-1].y)/40000.0;
G[ans][ans-1]=G[ans-1][ans]=temp;
}
flag=1;
ans++;
}
for(int i=0;i!=ans;i++)
for(int j=0;j!=ans;j++)
{
if(i!=j&&G[i][j]==0.0)
G[i][j]=G[j][i]=hypot(pos[i].x-pos[j].x,pos[i].y-pos[j].y)/10000.0;
}
dijkstra(0);
cout<<int(60.0*d[1]+0.5)<<endl;
return 0;
}
poj 2502 dijkstra(关键是建图)
最新推荐文章于 2024-09-02 16:35:28 发布