hdoj 5636 Shortest Path 【最短路】

题目链接:hdoj 5636 Shortest Path

题意:有一条长度为 n 的链. 节点i i+1 之间有长度为 1 的边. 现在又新加了3条边, 每条边长度都是 1. 给出 m 个询问, 每次询问两点之间的最短路.最后对于每组数据, 输出一个整数ans=ni=1(iq[i])表示第i组询问的答案。

思路: st 的最短路只有两种可能。 (1) 经过特殊的 6 个点;(2)不经过;
对于情况 (1) ,我们考虑经过多少个点。经过一个点,直接枚举就好了;经过两个点同样枚举;经过三个点和经过两个点本质是一样的。这样只需枚举一个点和两个点的情况即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MOD = 1e9+7;
int dist[6][6];
int num[6];
int u[6], v[6];
int main()
{
    int t; scanf("%d", &t);
    while(t--)
    {
        int n, m; scanf("%d%d", &n, &m);
        int top = 0;
        for(int i = 1; i <= 3; i++)
        {
            scanf("%d%d", &u[i], &v[i]);
            num[top++] = u[i]; num[top++] = v[i];
        }
        for(int i = 0; i < 6; i++)
            for(int j = 0; j < 6; j++)
                dist[i][j] = abs(num[i]-num[j]);
        top = 0;
        for(int i = 1; i <= 3; i++)
            dist[top][top+1] = dist[top+1][top] = min(1, dist[top][top+1]), top += 2;
        for(int k = 0; k < 6; k++)
            for(int i = 0; i < 6; i++)
                for(int j = 0; j < 6; j++)
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
        LL ans = 0;
        for(int k = 1; k <= m; k++)
        {
            int s, t;
            scanf("%d%d", &s, &t);
            int res = abs(s-t);
            for(int i = 0; i < 6; i++)
            {
                for(int j = 0; j < 6; j++)
                {
                    res = min(res, abs(s-num[i]) + abs(t-num[j]) + dist[i][j]);
                    res = min(res, abs(s-num[j]) + abs(t-num[i]) + dist[i][j]);
                }
            }
            //cout << res << endl;
            ans = (ans + 1LL * res * k % MOD) % MOD;
        }
        cout << ans << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值