比赛 20190521热身赛 评测结果 AAAAAAAAAA
题目名称 挖水井 最终得分 100
用户昵称 欧鹰 运行时间 0.193 s
代码语言 C++ 内存使用 17.48 MiB
提交时间 2019-05-21 20:03:02
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define int long long
using namespace std;
int read(){
    int w=1,x=0,ch=getchar();
    for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')w=-1;
    for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
    return x*w;
}
const int MAXN = 100010;
struct Edge{
    int from,to,val;
    Edge(int from,int to,int val){
        this->from=from;
        this->to=to;
        this->val=val;
    }
    Edge(){}
    bool operator <(const Edge&x)const {return val<x.val;}
}E[MAXN];
const int INF = 0x3f3f3f3f;
int tot=0;
int f[MAXN];
int fa(int x){
    return f[x]==x?f[x]:fa(f[x]);
}
int n;
int anspart=INF,ans=0;
int wi[MAXN];
void init(){
    n=read();
    for(int i=1;i<=n;i++)f[i]=i;
    for(int i=1;i<=n;i++)wi[i]=read();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            int x=read();
            E[++tot]=Edge(i,j,x);  
        }
    for(int i=1;i<=n;i++)E[++tot]=Edge(0,i,wi[i]);
    sort(E+1,E+1+tot);
    int cnt=0;
    for(int i=1;i<=tot;i++){
        if(fa(E[i].from)!=fa(E[i].to)){
            f[fa(E[i].from)]=f[fa(E[i].to)];
            ans+=E[i].val;
        	cnt++;
		}
		if(cnt==n)break;
    }
}
signed main(){
	freopen("water.in","r",stdin);
	freopen("water.out","w",stdout);
    init();
    printf("%d",ans);
    return 0;
}
/*
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
*/