zoukankan      html  css  js  c++  java
  • 7-6 公路村村通(30 分) 【prime】

    7-6 公路村村通(30 分)

    现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本。
    输入格式:

    输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N);随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本。为简单起见,城镇从1到N编号。
    输出格式:

    输出村村通需要的最低成本。如果输入数据不足以保证畅通,则输出−1,表示需要建设更多公路。
    输入样例:

    6 15
    1 2 5
    1 3 3
    1 4 7
    1 5 4
    1 6 2
    2 3 4
    2 4 6
    2 5 2
    2 6 6
    3 4 6
    3 5 1
    3 6 1
    4 5 10
    4 6 8
    5 6 3

    输出样例:

    12

    思路

    用的是最小生成树 的方法
    链接:https://blog.csdn.net/qq_35644234/article/details/59106779

    然后 自己写的最小生成树 的思路 超时了

    就是 对每一个 已经入队的 点 一个一个遍历 寻找下一个最短路径

    但是 更好的方法是
    对每一个点 入队
    更新 到 未入队的点的最短距离 就是 距离比原先的距离小 就更新 这样每次对每个点 更新 那么就能保证 lowCost 中的 距离 在当前状态下就是最小的

    然后每次 让 距离最短的 入队 就可以了

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    #define pb push_back
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = 3.14159265358979323846264338327;
    const double E = exp(1);
    const double eps = 1e-30;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e3 + 5;
    const int MOD = 1e9 + 7;
    
    int G[maxn][maxn];
    int lowCost[maxn];
    
    int n, m;
    
    int findMin()
    {
        int Min = INF;
        int flag = 0;
        for (int i = 1; i <= n; i++)
        {
            if (lowCost[i] && lowCost[i] < Min)
            {
                Min = lowCost[i];
                flag = i;
            }
        }
        return flag;
    }
    
    int prime()
    {
        int ans = 0;
        for (int i = 1; i <= n; i++)
            lowCost[i] = G[1][i];
        lowCost[1] = 0;
        for (int i = 2; i <= n; i++)
        {
            int k = findMin();
            if (k)
            {
                ans += lowCost[k];
                lowCost[k] = 0;
                for (int j = 1; j <= n; j++)
                {
                    if (lowCost[j] && G[k][j] < lowCost[j])
                        lowCost[j] = G[k][j];
                }
            }
            else
                return -1;
        }
        return ans;
    }
    
    
    int main()
    {
        memset(G, 0x3f, sizeof(G));
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; i++)
            G[i][i] = 0;
        int x, y, v;
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &x, &y, &v);
            G[x][y] = G[y][x] = v;
        }
        cout << prime() << endl;
    }
    
    
    
    
    
    
    
  • 相关阅读:
    容器跨主机网络通信学习笔记(以Flannel为例)
    Kubernetes控制器Job和CronJob
    记一次使用Flannel插件排错历程
    Kubernetes控制器Deployment
    Kubernetes如何通过StatefulSet支持有状态应用?
    react18 来了,我 get 到...
    gojs 实用高级用法
    vuecli3 vue2 保留 webpack 支持 vite 成功实践
    calibre 报错 This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. 解决
    unable to recognize "*.yaml": no matches for kind "RoleBinding" in version "rbac.authorization.k8s.io/v1beta1"
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433188.html
Copyright © 2011-2022 走看看