题意:给定一个 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. 积累 ---- PHP可能会遇到的面试题

    1.白盒测试和黑盒测试的区别 2.Bootstrap是什么 3.OOP是什么意思 4.git和svn的使用 5.常用的git命令 6.lamp开发环境 7.高内聚,低耦合

  2. jmeter中50%70%80%90%代表的含义

    参考 http://www.cnblogs.com/jackei/archive/2006/11/11/557972.html 我的理解是: 在4.08秒响应时间内有50%的用户达到这个4.08的标准 ...

  3. java图形用户界面添加背景颜色不成功的解决方案

    总结:背景颜色不成功,那么使用这个方法试试.getContentpane(); package clientFrame; import java.awt.Color; import java.awt. ...

  4. Java 数组的三种创建方法,数组拷贝方法

    public static void main(String[] args) {//创建数组的第一种方法int[] arr=new int[6];int intValue=arr[5];//Syste ...

  5. 位数问题(dp 递推)

    位数问题 时间限制: 1 Sec  内存限制: 128 MB提交: 35  解决: 19[提交][状态][讨论版][命题人:quanxing] 题目描述 在所有的N位数中,有多少个数中有偶数个数字3? ...

  6. C#获取程序代码执行时长

    ArrayList list = new ArrayList(); long startTicks = DateTime.Now.Ticks; for (int i = 0; i < 10000 ...

  7. mysql安装及基本操作(mysql作业)

    1 官网下载,链接  https://www.mysql.com/downloads/ Download MySQL Community Server 默认为你选好了Mac OS X 平台 选择的是. ...

  8. javascript第三节

    面向对象的程序设计 1.属性类型 ECMAScript中有两种属性:数据属性和访问器属性 数据属性: configurable设置为false,表示不能从对象中删除属性. 访问器属性: 支持定义多个属 ...

  9. Delphi Help

    http://docwiki.embarcadero.com/CodeExamples/Seattle/en/Category:Content_by_Version

  10. android 标签页<include /> 的使用

    在android页面布局设计中,有时候需要用到很多相同的布局设计.如果每个用到该布局的xml里都写那个相同布局的话,会造成语句冗余,而且可读性很差. 为了解决这个问题的话,我们可以把相同布局的代码单独 ...