zoukankan      html  css  js  c++  java
  • Day5

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
    Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
    Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
    The distance between any two farms will not exceed 100,000.

    Input

    The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

    Output

    For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    Sample Input

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    

    Sample Output

    28

    思路:简单的最小生成树板子题(prim)
    const int maxm = 105;
    
    struct Node {
        int u, v, w;
        Node(int _u, int _v, int _w):u(_u), v(_v), w(_w){}
        bool operator<(const Node &a)const{
            return a.w < w;
        }
    };
    
    vector<Node> Edge;
    vector<int> G[maxm];
    
    void addedge(int u, int v, int w) {
        Edge.push_back(Node(u, v, w));
        G[u].push_back(Edge.size() - 1);
    }
    
    int N, vis[maxm];
    
    void init() {
        for(int i = 1; i <= N; ++i)
            G[i].clear();
        memset(vis, 0, sizeof(vis));
        Edge.clear();
    }
    
    int main() {
        while(scanf("%d", &N) != EOF) {
            init();
            int val, ans, siz, times;
            for(int i = 1; i <= N; ++i)
                for(int j = 1; j <= N; ++j) {
                    scanf("%d", &val);
                    if(i != j)
                        addedge(i, j, val);
                }
            ans = times = 0;
            vis[1] = 1;
            priority_queue<Node> q;
            siz = G[1].size();
            for(int i = 0; i < siz; ++i) {
                int num = G[1][i];
                q.push(Node(1, Edge[num].v, Edge[num].w));
            }
            while(!q.empty() && times < N) {
                Node now = q.top();
                q.pop();
                int u = now.v;
                if(vis[u]++) continue;
                ans += now.w;
                siz = G[u].size();
                times++;
                for(int i = 0; i < siz; ++i) {
                    int num = G[u][i];
                    if(!vis[Edge[num].v])
                        q.push(Node(u, Edge[num].v, Edge[num].w));
                }
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    View Code
    
    
    
     
  • 相关阅读:
    小程序---云开发----云函数
    小程序的基本概念-生命周期(组件 wxml)
    小程序的基本概念
    vue登录功能和将商品添加至购物车实现
    vue脚手架创建项目
    node.js评论列表和添加购物车数据库表创建
    学习脚手架--组件之间跳转与参数(组件之间参数)
    node.js 需要注意知识点
    如何查询小程序官方手册
    vue ui九宫格、底部导航、新闻列表、跨域访问
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12176254.html
Copyright © 2011-2022 走看看