have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.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
此题没输出,真是蛋疼,错了也不知道怎么改,最后还是参考得来,因为范围问题错了n次,最关键的地方就是储存图!!!
处理时很要注重细节
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const double inf=1e30;
double d[300],cost[300][300];
int n=2;
struct node{
double x,y;
}e[300];
double dis(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void dijkstra()
{
queue<int>Q;
bool vis[300];
for(int i=0;i<n;i++)
{
d[i]=inf;
vis[i]=0;
}
d[0]=0;
Q.push(0);
vis[0]=1;
while(!Q.empty()){
int a=Q.front();
Q.pop();
vis[a]=0;
for(int i=0;i<n;i++)
if(d[i]>d[a]+cost[a][i])
{
d[i]=d[a]+cost[a][i];
if(!vis[i])
{
vis[i]=1;
Q.push(i);
}
}
}
}
int main()
{
scanf("%lf%lf%lf%lf",&e[0].x,&e[0].y,&e[1].x,&e[1].y);
for(int i=0;i<300;i++)
for(int j=0;j<300;j++)
{
if(i==j)cost[i][j]=0;
else cost[i][j]=inf;
}
double a,b;
while(~scanf("%lf%lf",&a,&b)){
int m=n;
while(1){
if(a==-1&&b==-1)break;
e[n++]={a,b};
scanf("%lf%lf",&a,&b);
}
for(int i=m+1;i<n;i++)
{
cost[i][i-1]=cost[i-1][i]=dis(e[i],e[i-1])*3.0/2000;
}
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cost[i][j]=min(cost[i][j],dis(e[i],e[j])*6.0/1000);
dijkstra();
printf("%.0f\n",d[1]);
return 0;
}