您的位置:首页 > 编程语言 > Go语言

【luogu2840】 Moocast(gold)奶牛广播-金

2018-03-12 21:51 281 查看
(http://www.elijahqi.win/2017/07/11/%E3%80%90luogu2840%E3%80%91-moocastgold%E5%A5%B6%E7%89%9B%E5%B9%BF%E6%92%AD-%E9%87%91/)

题目描述

Farmer John’s cows () want to organize an emergency “moo-cast” system for broadcasting important messages among themselves.

Instead of mooing at each-other over long distances, the cows decide to equip themselves with walkie-talkies, one for each cow. These walkie-talkies each have a limited transmission radius, but cows can relay messages to one-another along a path consisting of several hops, so it is not necessary for every cow to be able to transmit directly to every other cow.

The cows need to decide how much money to spend on their walkie-talkies. If they spend !\sqrt{X}X$ for them to be able to communicate.

Please help the cows determine the minimum integer value of such that a broadcast from any cow will ultimately be able to reach every other cow.

FJ的N头牛(1≤N≤1000)为了在他们之间传播信息, 想要组织一个”哞哞广播”系统. 奶牛们决定去用步话机装备自己而不是在很远的距离之外互相哞哞叫, 所以每一头奶牛都必须有一个步话机. 这些步话机都有一个限制传播半径, 但是奶牛们可以间接地通过中间奶牛传播信息, 所以并不是每头牛都必须直接向其他每一头奶牛连边.

奶牛们需要去决定多少钱花在步话机上, 如果他们花了$X, 那么他们都将会得到sqrt(x)距离的步话机. 所以, 两头牛之间的欧几里得距离最多是X. 请帮助奶牛们找到最小的X使得图是强连通的.、

输入输出格式

输入格式:

The first line of input contains .

The next lines each contain the and coordinates of a single cow. These are both integers in the range .

输出格式:

Write a single line of output containing the integer giving the minimum amount the cows must spend on walkie-talkies.

输入输出样例

输入样例#1:

4

1 3

5 4

7 2

6 1

输出样例#1:

17

说明

感谢@MrMorning 提供翻译

题解:这大概是比较裸的最小生成树,写之前建议仔细读题 题目意思是求可以使得所有奶牛都联通的最大值最小

那么kruskal求得最后一条边即可

#include<cstdio>
#include<algorithm>
#define N 1100
using namespace std;
inline int qrt(int x){
return x*x;
}
int num,n,x
,y
,fa
;
inline int calc(int i,int j){
int ans=qrt(x[i]-x[j])+qrt(y[i]-y[j]);
return ans;
}
inline int read(){
int x=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9') {
x=x*10+ch-'0';ch=getchar();
}
return x;
}
struct node{
int x,y,w;
}data[N*N];
inline void insert1(int x,int y,int w){
data[++num].x=x;data[num].y=y;data[num].w=w;
}
inline int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
inline bool cmp(node a,node b){
return a.w<b.w;
}
int main(){
freopen("2840.in","r",stdin);
freopen("2840.out","w",stdout);
n=read();num=0;
for (int i=1;i<=n;++i) x[i]=read(),y[i]=read();
for (int i=1;i<=n;++i) fa[i]=i;
for (int i=1;i<=n;++i){
for (int j=i+1;j<=n;++j){
insert1(i,j,calc(i,j));
}
}
int tot=0;
sort(data+1,data+1+num,cmp);
//for (int i=1;i<=num;++i )printf("%d %d %d\n",data[i].x,data[i].y,data[i].w);
int i;
for (i=1;i<=num;++i){
int p=find(data[i].x),q=find(data[i].y);
if (p!=q) fa[p]=q,++tot;
//    printf("%d %d %d\n",data[i].x,data[i].y,data[i].w);
if (tot==n-1) break;
}
printf("%d",data[i].w);
return 0;
}

4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: