P2872 [USACO07DEC]Building Roads S
题目描述
Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.
Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.
给定 �n 个点的坐标,第 �i 个点的坐标为 (��,��)(xi,yi),这 �n 个点编号为 11 到 �n。给定 �m 条边,第 �i 条边连接第 ��ui 个点和第 ��vi 个点。现在要求你添加一些边,并且能使得任意一点都可以连通其他所有点。求添加的边的总长度的最小值。
输入格式
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Two space-separated integers: Xi and Yi
* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.
第一行两个整数 �,�n,m 代表点数与边数。
接下来 �n 行每行两个整数 ��,��xi,yi 代表第 �i 个点的坐标。
接下来 �m 行每行两个整数 ��,��ui,vi 代表第 �i 条边连接第 ��ui 个点和第 ��vi 个点。
输出格式
* Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.
一行一个实数代表添加的边的最小长度,要求保留两位小数,为了避免误差, 请用 6464 位实型变量进行计算。
输入输出样例
输入 #1复制
4 1 1 1 3 1 2 3 4 3 1 4
输出 #1复制
4.00
说明/提示
数据规模与约定
对于 100%100% 的整数,1≤�,�≤10001≤n,m≤1000,1≤��,��≤1061≤xi,yi≤106,1≤��,��≤�1≤ui,vi≤n。
说明
Translated by 一只书虫仔。
题解:
1:先把边求出来
double jl = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
2:把边排序
bool cmp(dian x,dian y)
{
return x.w<y.w;
}
sort(hh+1,hh+cnt+1,cmp);
3:对于已连接的点进行合并
for(int i=1;i<=b;i++)
{
cin>>u>>v;
hp(u,v);
}
4:用kruskal找出路径
代码:
#include<bits/stdc++.h>
using namespace std;
#define pp 1000009
int t[pp],cou=0, x[pp],y[pp];
double sum;
struct dian
{
double u,v;
double w;
}hh[pp];
void csh(int n)
{
for(int i=1;i<=n;i++)
t[i]=i;
}
int find(int a)
{
if(t[a]==a) return a;
else
{
t[a]=find(t[a]);
return t[a];
}
}
int hp(int a,int b)
{
if(find(a)!=find(b)){
t[find(b)]=find(a);
return 1;
}
return 0;
}
bool cmp(dian x,dian y)
{
return x.w<y.w;
}
int main()
{
int cnt=1,a,b,c,d,e,u,v;
cin>>a>>b;
csh(a);
for(int i=1;i<=a;i++)
{
cin>>x[i]>>y[i];
}
for(int i=1;i<=b;i++)
{
cin>>u>>v;
hp(u,v);
}
for(int i = 1; i <= a; i++){
for(int j = i + 1; j <= a; j++){
double jl = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
hh[cnt].u = i;
hh[cnt].v = j;
hh[cnt].w = jl;
cnt++;}}
sort(hh+1,hh+cnt+1,cmp);
for(int i=1;i<=a;i++)
{
if(hp(hh[i].u,hh[i].v))
{
cou++;
sum+=hh[i].w;
}
if(cou>=a) break;
}
printf("%.2f",sum);
}
P1359 租用游艇
题目描述
长江游艇俱乐部在长江上设置了 �n 个游艇出租站 1,2,⋯ ,�1,2,⋯,n。游客可在这些游艇出租站租用游艇,并在下游的任何一个游艇出租站归还游艇。游艇出租站 �i 到游艇出租站 �j 之间的租金为 �(�,�)r(i,j)(1≤�<�≤�1≤i<j≤n)。试设计一个算法,计算出从游艇出租站 11 到游艇出租站 �n 所需的最少租金。
输入格式
第一行中有一个正整数 �n,表示有 �n 个游艇出租站。接下来的 �−1n−1 行是一个半矩阵 �(�,�)r(i,j)(1≤�<�≤�1≤i<j≤n)。
输出格式
输出计算出的从游艇出租站 11 到游艇出租站 �n 所需的最少租金。
输入输出样例
输入 #1复制
3 5 15 7
输出 #1复制
12
说明/提示
�≤200n≤200,保证计算过程中任何时刻数值都不超过 106106。
题解:
1:先了解一下他的图是什么样
0 5 15
0 0 7
0 0 0
2:本来想用kruskal,发现点边处理不好。看了题解知道可以用dp解决
3:我们可以用a数组来存,a[i][j]a[i][j]表示i到j的价钱。
4:用dp数组来记录这个中转站到n号中转站的最小价钱,dp[i]表示中转站i到中转站n的最小价钱。
5:然后i把n上流的中转站从大到小跑一遍。我们先记录中转站2到中转站3的最小价钱,我们要用j跑一遍中转站2下流的所有中转站,记录a[i][j]+dp[j]的最小价钱,记录到dp[i]里面。
代码:
#include<bits/stdc++.h>
using namespace std;
int a[10009][10009],dp[10009];
int main()
{
int n;
cin>>n;
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
{
cin>>a[i][j];
dp[i]=1e9;
}
for(int i=n-1;i>0;i--)
for(int j=i+1;j<=n;j++)
dp[i]=min(dp[i],a[i][j]+dp[j]);
cout<<dp[1];
return 0;
}