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)。

    输出格式

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

    输入输出样例

    输入 #1
    4
    5
    4
    4
    3
    0 2 2 2
    2 0 3 3
    2 3 0 4
    2 3 4 0
    输出 #1
    9

    说明/提示

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

    本题看似很难,实际上思路非常简单——如果你想通了。

    首先有一个问题:图中有几个点?大部分的人会回答n个点。错了,有n+1个。

    多出来的那个点在哪?关键在于你要理解每一个决策的意义。实际上,多出来的那个点是地下的FarmerJohn山泉天然矿泉水。当我们打井时,我们实际上是在往地下连边。理解了这一点,代码就没有任何难度了。

    构图时,我们只需多加一个点,对于每个点i,我们连边i→n+1,边权为w_i。然后直接跑最小生成树就没了。就没了。

    #include <iostream>
    #include <cstdio>
    #include <queue>
    using namespace std;
    struct edge{
        int from,to,len;
        const bool operator < (edge b) const{
            return this->len>b.len;
        }
    }e[100000];
    priority_queue <edge> que;
    int n,a[302][302],w[301],fa[302],ans;
    int find(int x)
    {
        if(fa[x]==x)return x;
        return fa[x]=find(fa[x]);
    }
    int mst()
    {
        while(!que.empty()){
            edge p=que.top();
            if(find(p.from)!=find(p.to))
            {
                fa[fa[p.to]]=fa[p.from];
                ans+=p.len;
            }
            que.pop();
        }
        return ans;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&w[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j]);
                if(i!=j)que.push((edge){i,j,a[i][j]});
            }
        }
        n++;
        for(int i=1;i<n;i++){
            que.push((edge){i,n,w[i]});
            que.push((edge){n,i,w[i]});
        }
        for(int i=1;i<=n;i++)fa[i]=i;
        printf("%d",mst());
        return 0;
    }
  • 相关阅读:
    发音技巧
    SCROM标准和一些概念(转)
    我要告诉测试新手的 (转)
    LCMS与LMS
    SCORM标准的LMS ELearning 学习平台介绍
    【转载】经常在网上看人家的帖子,分享给组里面的兄弟共赏
    选择学习管理系统(LMS)不可忽略的十大要素
    委托(delegate)的使用
    LMS/LCMS相关概念简介
    软件测试
  • 原文地址:https://www.cnblogs.com/hrj1/p/11749972.html
Copyright © 2011-2022 走看看