bzoj 3479: [Usaco2014 Mar]Watering the Fields 最小生成树
2017-09-02 18:00
471 查看
→题目链接←
裸的最小生成树...只会prim算法
n^2暴力建边
然后跑最小生成树
学了这么长的时间终于第一次写最小生成树
真棒啊
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
struct node{
int s,t;
int len;
node(int x,int y,int z){
s=x;
t=y;
len=z;
}
friend bool operator < (node a,node b){
return a.len>b.len;
}
};
struct point{
int x,y;
}p[2020];
priority_queue<node>q;
vector<int>v[2020];
bool vis[2020];
int n,m;
int cal(int x,int y){
return (p[x].x-p[y].x)*(p[x].x-p[y].x)+(p[x].y-p[y].y)*(p[x].y-p[y].y);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++){
int x,y;
scanf("%d%d",&p[i].x,&p[i].y);
}
for(int i=1; i<=n; i++){
for(int j=i+1; j<=n; j++){
if(cal(i,j)>=m){
v[i].push_back(j);
v[j].push_back(i);
}
}
}
int ans=0;
int sum=1;
vis[1]=true;
for(int i=0; i<v[1].size(); i++)q.push(node(1,v[1][i],cal(1,v[1][i])));
while(!q.empty()){
node e=q.top();
q.pop();
if(vis[e.t])continue;
ans+=e.len;
sum++;
if(sum==n)break;
vis[e.t]=true;
for(int i=0; i<v[e.t].size(); i++)q.push(node(e.t,v[e.t][i],cal(e.t,v[e.t][i])));
}
if(sum==n)printf("%d\n",ans);
else printf("-1\n");
return 0;
}
裸的最小生成树...只会prim算法
n^2暴力建边
然后跑最小生成树
学了这么长的时间终于第一次写最小生成树
真棒啊
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
struct node{
int s,t;
int len;
node(int x,int y,int z){
s=x;
t=y;
len=z;
}
friend bool operator < (node a,node b){
return a.len>b.len;
}
};
struct point{
int x,y;
}p[2020];
priority_queue<node>q;
vector<int>v[2020];
bool vis[2020];
int n,m;
int cal(int x,int y){
return (p[x].x-p[y].x)*(p[x].x-p[y].x)+(p[x].y-p[y].y)*(p[x].y-p[y].y);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++){
int x,y;
scanf("%d%d",&p[i].x,&p[i].y);
}
for(int i=1; i<=n; i++){
for(int j=i+1; j<=n; j++){
if(cal(i,j)>=m){
v[i].push_back(j);
v[j].push_back(i);
}
}
}
int ans=0;
int sum=1;
vis[1]=true;
for(int i=0; i<v[1].size(); i++)q.push(node(1,v[1][i],cal(1,v[1][i])));
while(!q.empty()){
node e=q.top();
q.pop();
if(vis[e.t])continue;
ans+=e.len;
sum++;
if(sum==n)break;
vis[e.t]=true;
for(int i=0; i<v[e.t].size(); i++)q.push(node(e.t,v[e.t][i],cal(e.t,v[e.t][i])));
}
if(sum==n)printf("%d\n",ans);
else printf("-1\n");
return 0;
}
相关文章推荐
- 【BZOJ3479】[Usaco2014 Mar]Watering the Fields【最小生成树】
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields(最小生成树)
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )
- bzoj3479 [Usaco2014 Mar]Watering the Fields(prim裸题)
- 【BZOJ】3479: [Usaco2014 Mar]Watering the Fields(kruskal)
- BZOJ3479: [Usaco2014 Mar]Watering the Fields
- BZOJ_3479_[Usaco2014 Mar]Watering the Fields_Prim
- bzoj3479 [Usaco2014 Mar]Watering the Fields
- bzoj 3479: [Usaco2014 Mar]Watering the Fields
- bzoj3479 [Usaco2014 Mar]Watering the Fields
- bzoj3479【Usaco2014 Mar】Watering the Fields
- bzoj 1682: [Usaco2005 Mar]Out of Hay 干草危机(最小生成树)
- BZOJ3476 [Usaco2014 Mar]The Lazy Cow
- 【最小生成树】BZOJ1682[Usaco2005 Mar]-Out of Hay 干草危机
- BZOJ 1682 [Usaco2005 Mar] 最小生成树 解题报告
- BZOJ3476 : [Usaco2014 Mar]The Lazy Cow
- BZOJ_P1682 [Usaco2005 Mar]Out of Hay 干草危机(最小生成树)
- BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离
- BZOJ 3476: [Usaco2014 Mar]The Lazy Cow
- 高中OJ3810. 【USACO2014 3月】Watering the Fields