(简单) POJ 2502 Subway,Dijkstra。

  Description

  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.
 
  这个题目是最短路问题,建边的话就是地铁线上的是直线距离除以车的速度,其他的话是除以走的速度,是一个完全图。。。
 
代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
    
using namespace std;

const int INF=10e8;

bool vis[300];

void Dijkstra(double lowcost[],double cost[][300],int N,int start)
{
    for(int i=1;i<=N;++i)
    {
        lowcost[i]=INF;
        vis[i]=0;
    }
    lowcost[start]=0;

    int k;
    double minn;

    for(int cas=1;cas<=N;++cas)
    {
        minn=INF;
        k=-1;

        for(int i=1;i<=N;++i)
            if(!vis[i] && minn>lowcost[i])
            {
                minn=lowcost[i];
                k=i;
            }

        if(k==-1)
            return;

        vis[k]=1;

        for(int i=1;i<=N;++i)
            if(!vis[i] && cost[k][i]>=0 && lowcost[i]>lowcost[k]+cost[k][i])
                lowcost[i]=lowcost[k]+cost[k][i];
    }
}

int X[300],Y[300];
int N;
double map1[300][300];
double ans[300];

double getdis(int a,int b)
{
    return sqrt((double(X[a]-X[b]))*(X[a]-X[b])+(double(Y[a]-Y[b]))*(Y[a]-Y[b]));
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    for(int i=1;i<=250;++i)
        for(int j=1;j<=250;++j)
            map1[i][j]=-1;

    scanf("%d %d %d %d",&X[1],&Y[1],&X[2],&Y[2]);
    
    N=3;
    int base;

    while(~scanf("%d %d",&X[N],&Y[N]))
    {
        base=N;
        while(X[N]!=-1 || Y[N]!=-1)
        {
            ++N;
            scanf("%d %d",&X[N],&Y[N]);
        }

        for(int i=base;i<N-1;++i)
            map1[i][i+1]=map1[i+1][i]=3.0*getdis(i,i+1)/2000.0;
    }
    --N;

    for(int i=1;i<=N;++i)
        for(int j=1;j<=i;++j)
            if(map1[i][j]<0)
                map1[i][j]=map1[j][i]=3.0*getdis(i,j)/500.0;

    Dijkstra(ans,map1,N,1);

    printf("%d\n",(int)(ans[2]+0.5));
    
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/whywhy/p/4338499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值