POJ - 2502(恶心建图 + 朴素dijkstra)

题目链接

思路:
思路就是求最短路,最恶心的就是如何建图,
建图思路:
1.图上的点数 = 起点 + 终点 + 车站数,同一个地铁路线的点按地铁速度算,其他不在同一路线上的点按步行算,要将所有点的距离都算出来。
2.先将相同路线地铁站之间耗费时间算出来,在将所有点之间步行耗费时间算出来即可
最后输出时要用(int)(ans + 0.5)对ans进行四舍五入

代码:

#include <iostream>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <climits>
#include <iomanip>
#include <queue>
#include <vector>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define debug(a) cout << "debug : " << (#a) << " = " << a << endl

using namespace std;

typedef long long ll;
typedef pair<double, double> PII;

const int N = 500;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 998244353;

PII data[N];
int idx;
double a[N][N], dis[N];
bool vis[N];

void dijkstra()
{
    for (int i = 0; i < N; i++)
        dis[i] = INF;

    dis[1] = 0;
    for (int i = 1; i <= idx; i++)
    {
        int t = -1;
        for (int j = 1; j <= idx; j++)
        {
            if (!vis[j] && (t == -1 || dis[j] < dis[t]))
                t = j;
        }
        vis[t] = true;
        for (int j = 1; j <= idx; j++)
            dis[j] = min(dis[j], dis[t] + a[t][j]);
    }
}

int main()
{
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            if (i != j)
                a[i][j] = INF;
        }
    double x, y;
    for (int i = 1; i <= 3; i++) //i=3时就是第一条路线的第一个地铁站
    {
        cin >> x >> y;
        data[++idx] = PII(x, y);
    }
    while (cin >> x >> y)
    {
        if (x != -1 || y != -1)
        {
            data[++idx] = PII(x, y);
            int l = idx - 1, r = idx;
            double x1 = data[l].first, y1 = data[l].second, x2 = data[r].first, y2 = data[r].second;
            a[l][r] = a[r][l] = (sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2))) / 1000 / 40 * 60; //记录相同路线地铁站之间距离
        }
        else
        {
            cin >> x >> y; //输入路线的第一个地铁站
            data[++idx] = PII(x, y);
        }
    }
    for (int i = 1; i <= idx; i++)
    {
        for (int j = i + 1; j <= idx; j++)
        {
            double x1 = data[i].first, y1 = data[i].second, x2 = data[j].first, y2 = data[j].second;
            a[i][j] = a[j][i] = min((sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2))) / 1000 / 10 * 60, a[i][j]);
        }
    }
    dijkstra();
    cout << (int)(dis[2] + 0.5) << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值