hdoj1875题目:
Problem Description
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
Input
输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
Output
每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
Sample Input
2 2 10 10 20 20 3 1 1 2 2 1000 1000
Sample Output
1414.2 oh!
题目的思路都是记录下每个点的坐标后!计算出每两个点之间的距离
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define maxn 5000
struct land
{
int x,y;
int u,v;
double w;
}edge[maxn];
int f[105];
int cmp(land a,land b)
{
return a.w<b.w;
}
int find(int x)
{
if(x==f[x]) return f[x];
else return f[x]=find(f[x]);
}
void kruskal(int d,int tol)
{
double ans=0;
int cnt=0;
sort(edge,edge+tol,cmp);
//cout<<edge[0].w<<d<<tol<<endl;
for(int i=0;i<tol;i++)
{
if(edge[i].w>1000.0) break;
else if(edge[i].w<10.0) continue;
int x=edge[i].u;
int y=edge[i].v;
int t1=find(x);
int t2=find(y);
if(t1!=t2)
{
ans+=edge[i].w;
cnt++;
f[t1]=t2;
}
if(cnt==d-1) break;
}
if(cnt!=d-1) printf("oh!\n");
else printf("%0.1lf\n",ans*100);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int c;
scanf("%d",&c);
for(int i=1;i<=c;i++) f[i]=i;
for(int i=1;i<=c;i++)
{
scanf("%d%d",&edge[i].x,&edge[i].y);
}
int i,j;
int k=0;
for(i=1;i<=c;i++)
for(j=i+1;j<=c;j++)
{
edge[k].u=i;
edge[k].v=j;
edge[k++].w=sqrt(pow((edge[j].x-edge[i].x),2.0)+pow((edge[j].y-edge[i].y),2.0));
}
//cout<<edge[0].w<<" "<<k<<endl;
kruskal(c,k);
}
return 0;
}
hdoj1162
Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.
Input contains multiple test cases. Process to the end of file.
Input contains multiple test cases. Process to the end of file.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
const int maxn=0x3f3f3f;
bool visit[102];
double map[102][102];
double low[102];
struct point
{
double x,y;
}p[102];
struct edge
{
int u,v;
double w;
}edge[5000];
void prim(int n)
{
double ans=0;
visit[1]=true;
bool ju=true;
int i,j;
for(i=2;i<=n;i++) low[i]=map[1][i];
for(i=2;i<=n;i++)
{
double ma=maxn;
int t=-1;
for(j=1;j<=n;j++)
if(!visit[j]&&ma>low[j])
{
ma=low[j];
t=j;
}
if(ma==maxn) ju=false;
ans+=ma;
visit[t]=true;
for(j=1;j<=n;j++)
if(!visit[j]&&low[j]>map[t][j])
low[j]=map[t][j];
}
if(ju) printf("%0.2lf\n",ans);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(map,maxn,sizeof(map));
memset(visit,false,sizeof(visit));
for(int i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
int i,j;
int k=0;
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
edge[k].u=i;
edge[k].v=j;
edge[k++].w=sqrt(pow((p[j].x-p[i].x),2.0)+pow((p[j].y-p[i].y),2.0));
}
for(i=0;i<k;i++)
{
int x=edge[i].u;
int y=edge[i].v;
map[x][y]=map[y][x]=edge[i].w;
}
prim(n);
}
return 0;
}