HDU 4081 Peach Blossom Spring (最小生成树+dfs)
题意:给定一个 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)的更多相关文章
- HDU 4085 Peach Blossom Spring
斯坦纳树. 最后可以是森林,在计算出每个联通状态的最小费用后,还需要进行一次$dp$. #include<bits/stdc++.h> using namespace std; const ...
- 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] ...
- hdu4085 Peach Blossom Spring
Peach Blossom Spring http://acm.hdu.edu.cn/showproblem.php?pid=4085 Time Limit: 10000/5000 MS (Java/ ...
- HDOJ 4085 Peach Blossom Spring
discriptionTao Yuanming(365-427) was a Chinese poet of Eastern Jin dynasty. One of his most famous w ...
- Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)
Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- HDU 2489 Minimal Ratio Tree 最小生成树+DFS
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 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 ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
随机推荐
- 从jvm角度来解析java语法糖
java有很多语法糖,比如自动拆箱,自动装箱,foreach等等,这些原理相信每一个入门教程里都有讲,但是我相信不是每一个人 都通过查看这些语法糖的字节码来确认这些原理,因为我也是现在才想看一下. 1 ...
- Spring缓存源码剖析:(一)工具选择
从本篇开始对Spring 4.3.6版本中Cache部分做一次深度剖析.剖析过程中会对其中使用到的设计模式以及原则进行分析.相信对设计内功修炼必定大有好处. 一.环境及工具 IntelliJ IDEA ...
- 1100 Mars Numbers
题意:进制转换. 思路:注意当数字是13的倍数时,只需高位叫法的单词.比如26,是“hel”,而不是“hel tret”.我被坑在这里了!对应语句1的处理.另外,在输入n和n个字符串之间需要一个吸收字 ...
- PHP字符串的处理(三)-字符串的输出
1.echo() echo()实际不是一个函数,是一个语言结构,不需要使用括号 <?php $str = "test"; echo $str."<br> ...
- Python Twisted网络编程框架与异步编程入门教程
原作出处:twisted-intro-cn 作者:Dave 译者:杨晓伟 luocheng likebeta 转载声明:版权归原作出处所有,转载只为让更多人看到这部优秀作品合集,如果侵权,请留言告知 ...
- oracle 函数中,一定要注意出现空记录和多条记录的处理方法
今天折腾了3个小时,为一个以前不知道的oracle函数机制: 在sql查询中,如果一个查询未能获取记录,oracle不会报错 如select aa from bb where 1=2; 但在oracl ...
- flask系列五之flask_script
1.一个简单的例子 在工程里面新建一个Python文件命名为manage.py (1)安装flask_script包,然后引入 from flask_script import Manager fro ...
- Rest Framework 认证源码流程
一.请求到来之后,都要先执行dispatch方法,dispatch方法方法根据请求方式的不同触发get/post/put/delete等方法 注意,APIView中的dispatch方法有很多的功能 ...
- elastic(10) 基本查询
# 指定index名以及type名的搜索 GET /library/books/_search?q=title:elasticsearch # 指定index名没有type名的搜索 GET /libr ...
- AngularJS学习(一)
参考文章:http://blog.csdn.net/dc_726/article/details/17010325 1.HelloWorld篇 1.1 环境 下载:angular-1.2.5min.j ...