【Luogu】每日一题——Day20. P4366 [Code+#4] 最短路 (图论)

P4366 Code+#4 最短路 - 洛谷

题目:

思路:

其实是找性质

本题我们首先可以想到的就是建图然后跑最短路,但是数据给的很大,如果直接暴力建图显然不行,考虑一些特殊情况需要建图

考虑 1 -> 7 这个例子

我们一种走法是直接从 1->7,那么二进制下就是 001 -> 111,花费就是 6 * c

另一种走法就是考虑走中间节点,具体的从 001 -> 011 -> 111,花费一样也是 6 * c

可以看出,只要两个数有超过一位不同,那么就能通过中间节点走到终点

具体的,我们每个数都建一条和自己有一位不同的边,那么其连接的点就是 i ^ ,即只有一位不同,这个位可以是 32 位中的任意一位,所以价值就是 c * (i ^ (i ^ )) = c *

所以就从 n*n 变成了 n*logn 的建图了,顺利解决

代码:

cpp 复制代码
#include <iostream>
#include <algorithm>
#include<cstring>
#include <iomanip>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <array>
#include <tuple>
using namespace std;
#define int long long
#define yes cout << "YES" << endl
#define no cout << "NO" << endl

vector<vector<pair<int,int>>> g(100005);
int n, m, c;
int dis[100005];
int s, e;

void Init()
{
    for (int i = 0; i <= n; i++)
    {
        for (int j = 1; j <= n; j <<= 1)
        {
            if ((i ^ j) > n)
                continue;
            g[i].push_back({ i ^ j, j * c });
        }
    }
}

void fuc()
{
    dis[s] = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
    pq.push({ 0,s });
    while (!pq.empty())
    {
        auto t = pq.top();
        pq.pop();
        if (t.second == e || dis[t.second] < t.first)
        {
            continue;
        }
        for (auto & son : g[t.second])
        {
            int v = son.first;
            int cost = son.second;
            if (dis[v] > t.first + cost)
            {
                dis[v] = t.first + cost;
                pq.push({ t.first + cost ,v });
            }
        }
    }
}

void solve()
{
    cin >> n >> m >> c;
    for (int i = 0; i < m; i++)
    {
        int u, v, c;
        cin >> u >> v >> c;
        g[u].push_back({ v, c });
    }
    cin >> s >> e;
    Init();
    memset(dis, 0x3f, sizeof dis);
    fuc();
    cout << dis[e] << endl;
}
signed main()
{
    //cin.tie(0)->sync_with_stdio(false);
    int t = 1;
    //cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
相关推荐
wuyk55513 小时前
28 搜索引擎索引更新:从 LSM Tree 到布隆过滤器的高效实现
stm32·单片机·嵌入式硬件·算法
我命由我1234513 小时前
LRU 缓存淘汰策略
数据结构·redis·学习·mysql·算法·缓存·学习方法
垆边人似月.13 小时前
华为题:合并区间
数据结构·c++·算法
垆边人似月.13 小时前
华为题:滑动窗口最大值
数据结构·c++·算法
2601_9657422214 小时前
AI是怎么判断一家本地商家值得推荐的?云南老板看完这篇就懂了
大数据·人工智能·算法·ai·新媒体运营
小O的算法实验室14 小时前
IEEE TAES,基于DQN 辅助的集群无人机自组网约束多目标重构与优化
人工智能·算法
_不会dp不改名_14 小时前
leetcode_3727 最大平方交替和
linux·算法·leetcode
信奥卷王14 小时前
[GESP202509 六级] 货物运输(含视频)
算法
余额瞒着我当琳14 小时前
C++算法学习篇5--二分查找法
c++·学习·算法
起个名字费劲死了15 小时前
基于灰度/形状的2D模板匹配
算法