【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;
}
相关推荐
Cx330❀8 分钟前
【数据结构初阶】--排序(四):归并排序
c语言·开发语言·数据结构·算法·排序算法
余_弦30 分钟前
区块链中的密码学 —— 密钥派生算法
算法·区块链
亲爱的非洲野猪1 小时前
令牌桶(Token Bucket)和漏桶(Leaky Bucket)细节对比
网络·算法·限流·服务
NAGNIP1 小时前
一文读懂LLAMA
算法
烧冻鸡翅QAQ1 小时前
62.不同路径
算法·动态规划
番薯大佬1 小时前
编程算法实例-冒泡排序
数据结构·算法·排序算法
queenlll1 小时前
P2404 自然数的拆分问题(典型的dfs)
算法·深度优先
wydaicls1 小时前
用函数实现方程函数解题
人工智能·算法·机器学习
·白小白1 小时前
力扣(LeetCode) ——100. 相同的树(C语言)
c语言·算法·leetcode
CoovallyAIHub2 小时前
为什么85%的企业AI项目都失败了?
深度学习·算法·计算机视觉