zoukankan      html  css  js  c++  java
  • 洛谷P1550 [USACO08OCT]打井Watering Hole

    题目背景

    John的农场缺水了!!!

    题目描述

    Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conveniently numbered 1..N. He may bring water to a pasture either by building a well in that pasture or connecting the pasture via a pipe to another pasture which already has water.

    Digging a well in pasture i costs W_i (1 <= W_i <= 100,000).

    Connecting pastures i and j with a pipe costs P_ij (1 <= P_ij <= 100,000; P_ij = P_ji; P_ii=0).

    Determine the minimum amount Farmer John will have to pay to water all of his pastures.

    POINTS: 400

    农民John 决定将水引入到他的n(1<=n<=300)个牧场。他准备通过挖若

    干井,并在各块田中修筑水道来连通各块田地以供水。在第i 号田中挖一口井需要花费W_i(1<=W_i<=100,000)元。连接i 号田与j 号田需要P_ij (1 <= P_ij <= 100,000 , P_ji=P_ij)元。

    请求出农民John 需要为使所有农场都与有水的农场相连或拥有水井所需要的钱数。

    输入输出格式

    输入格式:

    第1 行为一个整数n。

    第2 到n+1 行每行一个整数,从上到下分别为W_1 到W_n。

    第n+2 到2n+1 行为一个矩阵,表示需要的经费(P_ij)。

    输出格式:

    只有一行,为一个整数,表示所需要的钱数。

    说明

    John等着用水,你只有1s时间!!!

    分割线

    此题看似很难,其实只要想通一个点就很简单了:打井可以看做与0连接。

    然后再敲一遍克鲁斯卡尔(最小生成树)模板就好了,用并查集维护。

    AC代码奉上(果然还是菜鸡,一个模板题想了好长时间qwq):

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int n,cnt,cnt2,ans,fa[90010];
    struct node//用结构体记录,方便排序 
    {
        int l,r;
        int num;
    }w[90010];
    int find(int x)//并查集 
    {
        if(fa[x]==x)return x;
        return fa[x]=find(fa[x]);
    }
    bool cmp(node x,node y)//sort排序规则cmp函数 
    {
        return x.num<y.num;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&w[++cnt].num);
            w[cnt].l=0;
            w[cnt].r=i;
        }
        for(int i=1;i<=n;i++){fa[i]=i;}//并查集初始化 
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int ff;
                scanf("%d",&ff);
                if(i>j)
                {
                    w[++cnt].num=ff;
                    w[cnt].l=i;
                    w[cnt].r=j;
                }
            }
        }
        sort(w+1,w+cnt+1,cmp);//排序 
        for(int i=1;i<=cnt;i++)
        {
            int dx=find(w[i].l);
            int dy=find(w[i].r);
            if(dx!=dy)//如果不在同一个并查集里 
            {
                fa[dx]=dy;//合并并查集 
                ans+=w[i].num;//记录答案 
                cnt2++;
            }
            if(cnt2==n)break;//连接的牧场够n个了 
        }
        printf("%d
    ",ans);
        return 0;
    }

    end.谢谢阅读

  • 相关阅读:
    053(二十七)
    【leetcode❤python】141. Linked List Cycle
    【leetcode❤python】13. Roman to Integer
    【leetcode❤python】121. Best Time to Buy and Sell Stock
    【leetcode❤python】119. Pascal's Triangle II
    【leetcode❤python】118. Pascal's Triangle
    【leetcode❤python】110. Balanced Binary Tree
    【leetcode❤python】107. Binary Tree Level Order Traversal II
    【leetcode❤python】102. Binary Tree Level Order Traversal
    【leetcode❤python】101. Symmetric Tree
  • 原文地址:https://www.cnblogs.com/lcccAngelo/p/9890342.html
Copyright © 2011-2022 走看看