POJ2502---Subway (最短路:spfa or floyed)

【题目来源】https://vjudge.net/problem/POJ-2502
【题意】
给出起点终点坐标,给出多条线路,分别给出x,y坐标,以-1,-1结束一条线路,直到文件输入完全为止。
【思路】
这道题有个点,并没有说清楚:题意中说共有不足200个地铁站,但并没有说明每条线路相互之间是否存在相同的地铁站,如果把每个站都记录下来,floyed的一层循环极限就不止200了,可以是很多很多。
然后,我写了去重了,可能是写残了,一直没过。然后就直接不去重,然后floyed一把,就过了。。。
【代码】

#include<cmath>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int tot;
struct node
{
    int x,y;
} sub[250];
double w[250][250];
double dis(int i,int j)
{
    return sqrt((double)(sub[i].x-sub[j].x)*(double)(sub[i].x-sub[j].x)+(double)(sub[i].y-sub[j].y)*(double)(sub[i].y-sub[j].y)*1.0);
}
struct cmp
{
    bool operator() (int &a,int &b)
    {
        return a>b;
    }
};
void floyed()
{
    for(int k=1; k<=tot; k++)
    {
        for(int i=1; i<=tot; i++)
        {
            for(int j=1; j<=tot; j++)
            {
                if(w[i][j]>w[i][k]+w[k][j])
                    w[i][j]=w[i][k]+w[k][j];
            }
        }
    }
    printf("%d\n",(int)(w[1][2]+0.5));
}
int main()
{
    tot=2;
    scanf("%d%d%d%d",&sub[1].x,&sub[1].y,&sub[2].x,&sub[2].y);
    for(int i=1; i<=240; i++)
    {
        for(int j=1; j<=240; j++)
        {
            w[i][j]=i==j?0:INF;
        }
    }
    while(~scanf("%d %d",&sub[tot].x,&sub[++tot].y))
    {
//        if(sub[tot].x==-1&&sub[tot].y==-1)
//        {
//            tot--;
//            break;
//        }
        while(~scanf(" %d %d",&sub[tot].x,&sub[++tot].y))
        {
            if(sub[tot].x==-1&&sub[tot].y==-1)
            {
                tot--;
                break;
            }
            w[tot][tot-1]=w[tot-1][tot]=min(dis(tot,tot-1)/40000.0*60,w[tot-1][tot]);
        }
    }
    for(int i=1;i<=tot;i++)
    {
        for(int j=i+1;j<=tot;j++)
        {
            w[i][j]=w[j][i]=min(dis(i,j)/10000.0*60,w[i][j]);
        }
    }
    floyed();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值