题意:给定一个 n 个点和相应的权值,要求你用 n-1 条边连接起来,其中一条边是魔法边,不用任何费用,其他的边是长度,求该魔法边的两端的权值与其他边费用的尽量大。

析:先求出最小生成树,然后再枚举每一条边,求出最大值,任意两点之间的距离可以通过预处理来解决,最小生成树时,要用prime算法,要不然可能会超时。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r > 0 && r <= n && c > 0 && c <= m;
} int cost[maxn][maxn];
int lowc[maxn];
int x[maxn], y[maxn], z[maxn];
bool vis[maxn];
int pre[maxn];
int dp[maxn][maxn]; vector<int> G[maxn]; void add(int u, int v){
G[u].push_back(v);
G[v].push_back(u);
} int dist(int i, int j){
return ((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
} double solve(){
double res = 0;
memset(vis, 0, sizeof vis);
memset(pre, -1, sizeof pre);
vis[0] = 1;
int p;
int minc;
int pr = 0;
for(int i = 1; i < n; ++i) lowc[i] = cost[0][i]; for(int i = 1; i < n; ++i){
minc = INF; p = -1;
for(int j = 0; j < n; ++j)
if(!vis[j] && minc > lowc[j]){
minc = lowc[j];
p = j;
}
for(int j = 0; j < n; ++j) if(vis[j]){
if(minc == cost[p][j]){
pre[p] = j; break;
}
} res += sqrt(minc); vis[p] = 1;
for(int j = 0; j < n; ++j)
if(!vis[j] && lowc[j] > cost[p][j])
lowc[j] = cost[p][j]; }
return res;
} void dfs(int u, int fa, int rt, int mmax){
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(v == fa) continue;
dp[rt][v] = max(mmax, cost[u][v]);
dfs(v, u, rt, max(mmax, cost[u][v]));
}
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d %d %d", x+i, y+i, z+i);
G[i].clear();
} for(int i = 0; i < n; ++i)
for(int j = i+1; j < n; ++j)
cost[i][j] = cost[j][i] = dist(i, j); double sum = solve();
for(int i = 0; i < n; ++i){
if(pre[i] == -1) continue;
add(i, pre[i]);
} memset(dp, 0, sizeof dp);
for(int i = 0; i < n; ++i) dfs(i, -1, i, 0);
double ans = 0.0;
for(int i = 0; i < n; ++i)
for(int j = i+1; j < n; ++j){
double x = z[i] + z[j];
double t = sum - sqrt(dp[i][j]);
double xxx = x / t;
ans = max(ans, xxx);
}
printf("%.2f\n", ans);
}
return 0;
}

  

HDU 4081 Peach Blossom Spring (最小生成树+dfs)的更多相关文章

  1. HDU 4085 Peach Blossom Spring

    斯坦纳树. 最后可以是森林,在计算出每个联通状态的最小费用后,还需要进行一次$dp$. #include<bits/stdc++.h> using namespace std; const ...

  2. HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA

    状态压缩dp+spfa解斯坦纳树 枚举子树的形态 dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][l]) 当中k和l是对j的一个划分 依照边进行松弛 dp[i][j]  ...

  3. hdu4085 Peach Blossom Spring

    Peach Blossom Spring http://acm.hdu.edu.cn/showproblem.php?pid=4085 Time Limit: 10000/5000 MS (Java/ ...

  4. HDOJ 4085 Peach Blossom Spring

    discriptionTao Yuanming(365-427) was a Chinese poet of Eastern Jin dynasty. One of his most famous w ...

  5. Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)

    Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...

  6. HDU 5723 Abandoned country (最小生成树 + dfs)

    Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

  7. HDU 2489 Minimal Ratio Tree 最小生成树+DFS

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. hdu4085 Peach Blossom Spring 斯坦纳树,状态dp

    (1)集合中元素表示(1<<i), i从0开始 (2)注意dp[i][ss] = min(dp[i][ss], dp[i][rr | s[i]] + dp[i][(ss ^ rr) | s ...

  9. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

随机推荐

  1. 如何禁用 FastAdmin 双击编辑功能?

    如何禁用 FastAdmin 双击编辑功能? 新版 (1.0.0.20180513_beta)增加一个新功能,可以禁止双击编辑. 很多人还是喜欢双击选中复制,默认的双击编辑还是不怎么习惯. 可以以下文 ...

  2. RelativeLayout里的gravity不能居中的解决方法

    最近在遇到RelativeLayout里的gravity属性给它复制center_horizontal或者center都不能居中它的子组件,后来找到了替代方法,只要在它的每个子组件里加上android ...

  3. sqlserver sql语句附加 分离数据库

    当使用 sp_attach_db 系统存储过程附加数据库时- - Tag: 当使用 sp_attach_db 系统存储过程附加数据库时 //附加数据库 sp_attach_db 当使用 sp_atta ...

  4. Android 控件之Spinner

    Spinner用来显示列表项,类似于一组单选框RadioButton.下面瞥一下它的效果. 源码下载 一.概述 Spinner是一个每次只能选择所有项的一个项的控件.它的项来自于与之相关联的适配器中. ...

  5. python 类实例化,修改属性值

    class User(object): def __init__(self, first_name, last_name, login_attempts): self.first_name = fir ...

  6. Java面试(二)

    1 同步方法 VS 同步代码块: java中,每一个对象都有一把锁,线程用synchronized获取对象上的锁. 非静态同步方法:锁是类的对象的锁. 静态同步方法:锁的是类本身. 同步方法块:锁是可 ...

  7. mysql索引原理与慢查询优化1

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  8. 【转】Context.getExternalFilesDir()和Context.getExternalCacheDir()方法

    应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的.大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中.这样当该应用被卸载后,这些数据还 ...

  9. js中,清空对象(删除对象的属性)

    在项目中,有些对象用完后需要重置,下面简单介绍下JS中清除对象的方法.方法如下: 方法一:字面量定义对象 第一步,定义一个空对象并打印出来,代码和效果: 代码: var student = {};co ...

  10. Springboot项目打成jar包运行 和 打成war包 外部tomcat运行

    Jar打包方式运行 类型为jar时 <packaging>jar</packaging> 1.使用命令mvn clean  package 打包 2.使用java –jar 包 ...