P1550 [USACO08OCT]打井Watering Hole

题目描述

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时间!!!

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
    return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
    return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
    return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
    return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int N = 1000005;
const int mod=100003;
// name*******************************
struct edge
{
    int from,to,w;
} e[N];
int pre[N];
int Rank[N];
int ans=0;
int t=0;
int n;
// function******************************
void init(int x)
{
    pre[x]=-1;
    Rank[x]=0;
}
int find(int x)
{
    int r=x;
    while(pre[r]!=-1)r=pre[r];
    while(x!=r)
    {
        int t=pre[x];
        pre[x]=r;
        x=t;
    }
    return r;
}
void unionone(int a,int b)
{
    int t1=find(a);
    int t2=find(b);
    if(Rank[t1]>Rank[t2])
        pre[t2]=t1;
    else
        pre[t1]=t2;
    if(Rank[t1]==Rank[t2])
        Rank[t2]++;
}
bool cmp(edge a,edge b)
{
    return a.w<b.w;
}
//***************************************
int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0);
    // freopen("test.txt", "r", stdin);
    //  freopen("outout.txt","w",stdout);
    cin>>n;
    For(i,0,n)init(i);
    int x;
    For(i,1,n)
    {
        cin>>x;
        e[++t].from=0;
        e[t].to=i;
        e[t].w=x;
    }
    For(i,1,n)
    For(j,1,n)
    {
        cin>>x;
        if(j>i)
        {
            e[++t].from=i;
            e[t].to=j;
            e[t].w=x;
        }
    }
    sort(e+1,e+1+t,cmp);
    int cnt=0;
    For(i,1,t)
    {
        if(find(e[i].from)!=find(e[i].to))
        {
            unionone(e[i].from,e[i].to);
            cnt++;
            ans+=e[i].w;
        }
        if(cnt==t-1)break;
    }
    cout<<ans;

    return 0;
}

 

posted @ 2018-04-05 22:48  planche  阅读(130)  评论(0)    收藏  举报