比赛 防止浮躁的小练习V0.1 评测结果 AAAAAAAAAA
题目名称 通信线路 最终得分 100
用户昵称 NewBee 运行时间 0.612 s
代码语言 C++ 内存使用 26.39 MiB
提交时间 2016-10-07 16:42:30
显示代码纯文本
#include<cstdio>
#include<algorithm>
#include<cstring>
#define Cu fclose(stdin);fclose(stdout);return 0;
#define Begin freopen("mcst.in","r",stdin);freopen("mcst.out","w",stdout);chul();Cu;
using namespace std;
//designed by New_Beeؼ 
const int maxn=1510;
int fath[maxn];
struct op{
	int fr,to,pay;
}r[maxn*maxn];
int n,tim;
void insert(int s,int t,int dis){
	r[++tim].fr=s;
	r[tim].to=t;
	r[tim].pay=dis;
}
void findfath(int x){
	if(fath[x]==x)return;
	findfath(fath[x]);
	fath[x]=fath[fath[x]];
}
bool comp(const op& a,const op& b){
	return a.pay<b.pay;
}
void chul(){
	scanf("%d",&n);int dist=0;
	for(int i=1;i<=n;i++){
		fath[i]=i;
		for(int j=1;j<=n;j++){
			scanf("%d",&dist);
			if(i<=j)continue;
			if(dist==-1)continue;
			insert(i,j,dist);
		}
	}sort(r+1,r+1+tim,comp);
	int cnt=0,ans=0;
	for(int i=1;i<=tim;i++){
		int s=r[i].fr,t=r[i].to;
		findfath(s);findfath(t);
		if(fath[s]==fath[t])continue;
		fath[fath[t]]=fath[s];
		ans+=r[i].pay;
		cnt++;
		if(cnt==n-1)break;
	}printf("%d\n",ans);
}
int main(){
	Begin;
}