Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11248 | Accepted: 3672 |
Description
Input
Output
Sample Input
0 0 10000 1000 0 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1
Sample Output
21
稠密图额最短路,关键在于输入和建边;边的权值为耗费的时间
同一地铁线路中相邻两点建立;任意两点建立步行权值的边;
注意输出浮点数的四舍五入;数据量较小,dijkstra()floyd()均可~
#include<iostream> #include<algorithm> #include<queue> #include<vector> #include<cstring> #include<utility> #include<cmath> #include<cstdio> using namespace std; const double v1=10000.0/60.0; const double v2=40000.0/60.0; const int maxn=333; double cost[maxn][maxn]; struct node{int x,y;}; node a[maxn]; int sz; const int inf=0x3f3f3f3f; void init() { for(int i=1;i<=300;i++) { for(int j=1;j<=300;j++) i==j?cost[i][j]=0:cost[i][j]=inf; } } double get(int aa,int bb) { return sqrt((double)(a[aa].x-a[bb].x)*(a[aa].x-a[bb].x)+(a[aa].y-a[bb].y)*(a[aa].y-a[bb].y)); } double dis[maxn]; int book[maxn]; typedef pair<double,int>pr; void dijkstra() { for(int i=1;i<=sz;i++)dis[i]=inf; memset(book,0,sizeof(book));dis[1]=0; priority_queue<pr,vector<pr>,greater<pr> >q; q.push(make_pair(0,1)); while(!q.empty()) { pr tt=q.top();q.pop(); double d=tt.first;int x=tt.second; if(book[x])continue;book[x]=1; for(int i=1;i<=sz;i++) { if(dis[i]>dis[x]+cost[x][i]) { dis[i]=dis[x]+cost[x][i]; q.push(make_pair(dis[i],i)); } } } } int main() { init(); cin>>a[1].x>>a[1].y>>a[2].x>>a[2].y; sz=2;int cnt1=3; int x,y; while(scanf("%d %d",&x,&y)==2) { if(x==-1&&y==-1){ cnt1=sz+1; continue; } ++sz;a[sz].x=x;a[sz].y=y; if(sz!=cnt1)cost[sz][sz-1]=cost[sz-1][sz]=(min(cost[sz][sz-1],get(sz,sz-1))/v2); } for(int i=1;i<=sz;i++) { for(int j=1;j<=sz;j++) { cost[j][i]=cost[i][j]=min(cost[i][j],get(i,j)/v1); } } dijkstra(); printf("%0.0lf\n",dis[2]); return 0; }