最短路--------L - Subway

You 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
Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.
Output
Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
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

这道题虽然构图比较麻烦,但还是能写出来的,但是被输出wa成狗了,原先输出是%d,改成%.0f,就AC了。。。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 1005;

int sx,sy,tx,ty;
double dis[N];
bool vis[N];
vector<pair<int,double> >ve[N];
pair<int,int>ans[N];

double Distance(int x1,int y1,int x2,int y2)
{
    return sqrt(1.0 * (x2 - x1) * (x2 - x1) + 1.0 * (y2 - y1) * (y2 - y1));
}

void spfa()
{
    for(int i = 1;i <= 250;++i) dis[i] = inf;
    memset(vis,false,sizeof(vis));
    queue<int>que;
    vis[1] = true;
    dis[1] = 0;
    que.push(1);
    while(!que.empty())
    {
        int tmp = que.front();
        que.pop();
        vis[tmp] = false;
        int len = ve[tmp].size();
        for(int i = 0;i < len;++i)
        {
            int to = ve[tmp][i].fi;
            double val = ve[tmp][i].se;
            if(dis[tmp] + val < dis[to]){
                dis[to] = dis[tmp] + val;
                if(!vis[to]){
                    vis[to] = true;
                    que.push(to);
                }
            }
        }
    }
}

int main()
{
    //freopen("out.txt","r",stdin);
    scanf("%d %d %d %d",&sx,&sy,&tx,&ty);
    ve[1].pb(mp(2,Distance(sx,sy,tx,ty) * 6.0 / 1000.0));
    ans[1].fi = sx;ans[1].se = sy;
    ans[2].fi = tx;ans[2].se = ty;
    int cnt = 3;
    int x,y;
    int t = 0;//表示这个站点前面是否有站点,便于计算
    while(~scanf("%d %d",&x,&y))
    {
        if(x == -1 && y == -1){
            t = 0;
            continue;
        }
        ans[cnt].fi = x;ans[cnt].se = y;
        for(int i = 1;i < cnt - t;++i){
            double dis = Distance(x,y,ans[i].fi,ans[i].se) * 6.0 / 1000.0;
            ve[cnt].pb(mp(i,dis));
            ve[i].pb(mp(cnt,dis));
        }
        if(t){
            double dis= Distance(x,y,ans[cnt - 1].fi,ans[cnt - 1].se) * 6.0 / 4000.0;
            ve[cnt].pb(mp(cnt - 1,dis));
            ve[cnt - 1].pb(mp(cnt,dis));
        }
        cnt++;t = 1;
    }
    spfa();
    printf("%.0f\n",dis[2]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/82945459