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
    
    
    
     
  • 相关阅读:
    【灵感】wifi通过wifi发送优惠信息
    Web 通信 之 长连接、长轮询(long polling)
    深入了解 Dojo 的服务器推送技术
    浅谈TCP优化
    非IE内核浏览器支持activex插件
    树莓派安装 Nginx + PHP7.0 + Pi Dashboard
    ChartView与LineSeries搭配实现曲线局部缩放功能
    QT开发(十二)——QT事件处理机制
    QT源码之Qt信号槽机制与事件机制的联系
    详解 QT 源码之 Qt 事件机制原理
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12176254.html
Copyright © 2011-2022 走看看