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
    
    
    
     
  • 相关阅读:
    Crazyflie 2.0 System Architecture
    HDU 4856 Tunnels(BFS+状压DP)
    scp报错:Host key verification failed. REMOTE HOST IDENTIFICATION HAS CHANGED!
    HDU 4175 Class Schedule (暴力+一点dp)
    正則表達式
    匿名訪问之(一)web application级别
    Android UI布局之TableLayout
    cocos2d-x-3.3rc2-003 cocos中的引用计数和自己主动释放池
    一步一步跟我学习hadoop(7)----hadoop连接mysql数据库运行数据读写数据库操作
    hdu Swipe Bo(bfs+状态压缩)错了多次的题
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12176254.html
Copyright © 2011-2022 走看看