题解 洛谷 Luogu P1828 [USACO3.2] 香甜的黄油 Sweet Butter 最短路 堆优化Dijkstra Floyd C++

题目

传送门

P1828 [USACO3.2] 香甜的黄油 Sweet Butter - 洛谷 | 计算机科学教育新生态https://www.luogu.com.cn/problem/P1828

思路

以每头奶牛所在的牧场为起点,求得到全图各个点的最短距离

再枚举全图所有点,计算从所有起点到某点的距离之和,取最小值即可

可以循环依次跑 Dijkstra,也可跑一遍 Floyd

后者更好写,但前者更快,实测前者时间效率约为后者的 6 倍

细节

每个牧场可能有不止一头奶牛,最后结算的距离需要乘上每个牧场的奶牛的头数

代码

堆优化Dijkstra

cpp 复制代码
#include <map>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
typedef pair<int, int> PII;
constexpr int C = 501, N = 801;
vector<PII> to[N];
map<int, int> cow;
int n, m, c, x, dist[C][N], cnt, ans = 1E9; //dist 记录不同起点跑出来的 Dijkstra 的结果
void Dijkstra(int start, int num) //起点,奶牛头数
{
    priority_queue<PII, vector<PII>, greater<PII>> q;
    bool mk[N]{};
    dist[cnt][start] = 0;
    q.emplace(0, start);
    while (q.size()) //真是模模又板板啊
    {
        int x = q.top().second;
        q.pop();
        if (mk[x]) continue;
        mk[x] = true;
        for (auto& z : to[x])
        {
            int X = z.first, w = z.second;
            if (dist[cnt][x] + w < dist[cnt][X])
            {
                dist[cnt][X] = dist[cnt][x] + w;
                q.emplace(dist[cnt][X], X);
            }
        }
    }
    for (int i = 1; i <= n; ++i) dist[cnt][i] *= num; //结果要乘以奶牛头数
    ++cnt; //用 cnt 分配每轮 Dijkstra 用到的 dist 的第一个下标,最后 cnt = cow.size()
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    memset(dist, 0x3f, sizeof(dist)); //别忘了初始化
    cin >> c >> n >> m;
    while (c--) cin >> x, ++cow[x]; //first 存奶牛所在的牧场,second 存该牧场有多少头奶牛
    while (m--)
    {
        int x, y, w;
        cin >> x >> y >> w;
        to[x].emplace_back(y, w);
        to[y].emplace_back(x, w);
    }
    for (auto& z : cow) Dijkstra(z.first, z.second); //循环跑 Dijkstra
    for (int i = 1; i <= n; ++i)
    {
        int sum = 0;
        for (int j = 0; j < cnt; ++j)
        {
            if (dist[j][i] == dist[0][0]) { sum = 1E9; break; } //可能有无法到达的牧场,遇到直接终止
            sum += dist[j][i];
        }
        ans = min(ans, sum);
    }
    cout << ans;
    return 0;
}

Floyd

cpp 复制代码
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
constexpr int N = 801;
map<int, int> cow;
int n, m, c, x, dist[N][N], ans = 1E9;
int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    memset(dist, 0x3f, sizeof(dist));
    cin >> c >> n >> m;
    while (c--) cin >> x, ++cow[x]; //first 存奶牛所在的牧场,second 存该牧场有多少头奶牛
    for (int i = 1; i <= n; ++i) dist[i][i] = 0; //自己到自己的距离是 0,别忘了
    while (m--)
    {
        int x, y, w;
        cin >> x >> y >> w;
        dist[x][y] = dist[y][x] = min(dist[x][y], w); //去重边
    }
    for (int k = 1; k <= n; ++k) //真是模模又板板啊
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
    for (int i = 1; i <= n; ++i)
    {
        int sum = 0;
        for (auto& z : cow)
        {
            if (dist[z.first][i] == dist[0][0]) { sum = 1E9; break; } //无法到达的点
            sum += dist[z.first][i] * z.second; //别忘了乘以奶牛头数
        }
        ans = min(ans, sum);
    }
    cout << ans;
    return 0;
}
相关推荐
纪伊路上盛名在4 分钟前
scRNA-seq scanpy教程1:准备工作+AnnData数据结构理解
数据结构·python·可视化·生物学·单细胞·scanpy
不学会Ⅳ5 分钟前
Redis7.0八种数据结构底层原理
数据结构
平生不喜凡桃李8 分钟前
C++ list介绍
c++·windows·list
一期一祈^10 分钟前
C++STL(六)——list模拟
c++·list
玉带湖水位记录员14 分钟前
C++模板编程——typelist的实现
开发语言·c++
大G哥15 分钟前
Java中有100万个对象,用list map泛型存储和用list对象泛型存储,那个占用空间大,为什么...
java·开发语言·数据结构·windows·list
曙曙学编程17 分钟前
进阶数据结构——单调队列
数据结构
liruiqiang0540 分钟前
机器学习 - 理解偏差-方差分解
人工智能·算法·机器学习
轩源源1 小时前
数据结构——红黑树的实现
开发语言·数据结构·c++·算法·红黑树·单旋+变色·双旋+变色
_DCG_1 小时前
c++设计模式之策略模式
c++·设计模式·策略模式