zoukankan      html  css  js  c++  java
  • 洛谷P3110 [USACO14DEC]Piggy Back S 题解 BFS

    题目链接:https://www.luogu.com.cn/problem/P3110

    解题思路:
    (1,2,n) 三个点分别求一下最短路(因为是无权图所以可以直接用BFS实现最短路)。

    示例代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 40040;
    int B, E, P, n, m, disB[maxn], disE[maxn], disP[maxn], ans = INT_MAX;
    vector<int> g[maxn];
    queue<int> que;
    void handle(int s, int dis[]) {
        while (!que.empty()) que.pop();
        memset(dis, -1, sizeof(int)*(n+1));
        dis[s] = 0;
        que.push(s);
        while (!que.empty()) {
            int u = que.front();
            que.pop();
            int sz = g[u].size();
            for (int i = 0; i < sz; i ++) {
                int v = g[u][i];
                if (dis[v] == -1) {
                    dis[v] = dis[u] + 1;
                    que.push(v);
                }
            }
        }
    }
    int main() {
        cin >> B >> E >> P >> n >> m;
        while (m --) {
            int u, v;
            cin >> u >> v;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        handle(1, disB);
        handle(2, disE);
        handle(n, disP);
        for (int i = 1; i <= n; i ++) {
            ans = min(ans, disB[i] * B + disE[i] * E + disP[i] * P);
        }
        cout << ans << endl;
        return 0;
    }
    
  • 相关阅读:
    while循环学习之统计流量
    MySQL的启动脚本
    UVA 725 Division
    UVA 712 S-tree
    UVA 514
    字典树
    UVA 1595 multimap 的应用
    C++ map 和 multimap
    浮点数
    UVA 227
  • 原文地址:https://www.cnblogs.com/quanjun/p/13691387.html
Copyright © 2011-2022 走看看