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
    
    
    
     
  • 相关阅读:
    WeGame 上线,打造一个wegame游戏的良性游戏圈子
    H5 视频作为背景 source src改变后 循环播放的问题笔记
    windows无法安装到这个磁盘具有mbr分区表
    jquery的扩展:编写好代码封装起来供他人使用
    获取 HTML data-*属性的值( 文章列表页面,存储文章id 为读取详细页面
    max(min)-device-width和max(min)-width的区别
    git 红色标出没明白
    git-flow 的工作流程
    会计服务平台 使用条款
    若依:设置跨域配置位置和图片中框出代码
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12176254.html
Copyright © 2011-2022 走看看