HDU 4126 Genghis Khan the Conqueror (树形DP+MST)
题意:给一图,n个点,m条边,每条边有个花费,给出q条可疑的边,每条边有新的花费,每条可疑的边出现的概率相同,求不能经过原来可疑边
(可以经过可疑边新的花费构建的边),注意每次只出现一条可疑的边,n个点相互连通的最小花费的期望。
析:要想连通先让他们连通起来,先构造出一个MST,然后再暴力,如果这个边不在这里面,那么花费不变,如果在里面,那我们需要知道是用原来的边最少,
还是再找一条边使他们连通起来,这里就要先预处理了,dp[i]j[i] 表示 左边的那个一半 i 和 右边那一半 j 的最长距离,如果我们知道了,就可以用这个来比较了,
我们要对MST进行 n 次更新,每次遍历是 n,所以时间复杂度是 O(n*n),可以实现。
代码如下:
#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 <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 3e3 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
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 int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int u, v, val;
Node() { }
Node(int uu, int vv, int va) : u(uu), v(vv), val(va) { }
bool operator < (const Node &p) const {
return val < p.val;
}
};
struct Edge{
int to, next;
};
Edge edge[maxn<<1];
Node a[maxn*maxn];
int p[maxn], dist[maxn][maxn], head[maxn];
int dp[maxn][maxn];
bool is_tree[maxn][maxn];
int cnt, sum; int Find(int x) { return x == p[x] ? x : p[x] = Find(p[x]); } void add(int u, int v){
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
} void Kruskal(){
sort(a, a+m);
int cnt = 0;
sum = 0;
for(int i = 0; i < m; ++i){
int x = Find(a[i].u);
int y = Find(a[i].v);
if(x != y){
p[y] = x;
add(a[i].u, a[i].v);
add(a[i].v, a[i].u);
is_tree[a[i].u][a[i].v] = is_tree[a[i].v][a[i].u] = true;
sum += a[i].val;
++cnt;
}
if(cnt == n-1) break;
}
} int dfs(int u, int fa, int root){
int ans = fa == root ? INF : dist[root][u]; //i d scf h for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == fa) continue;
int tmp = dfs(v, u, root);
ans = Min(ans, tmp);
dp[u][v] = dp[v][u] = Min(dp[u][v], tmp);
}
return ans;
} int main(){
while(scanf("%d %d", &n, &m) == 2 && m+n){
for(int i = 0; i < n; ++i) p[i] = i;
int u, v, c;
memset(dist, INF, sizeof dist);
for(int i = 0; i < m; ++i){
scanf("%d %d %d", &u, &v, &c);
a[i] = Node(u, v, c);
dist[u][v] = dist[v][u] = c;
} memset(head, -1, sizeof head);
memset(is_tree, false, sizeof is_tree);
cnt = 0;
Kruskal();
memset(dp, INF, sizeof dp);
for(int i = 0; i < n; ++i) dfs(i, -1, i); scanf("%d", &m);
double ans = 0.0;
for(int i = 0; i < m; ++i){
scanf("%d %d %d", &u, &v, &c);
if(!is_tree[u][v]) ans += sum;
else ans += sum - dist[u][v] + Min(c, dp[u][v]);
}
printf("%.4f\n", ans/m);
}
return 0;
}
HDU 4126 Genghis Khan the Conqueror (树形DP+MST)的更多相关文章
- HDU-4126 Genghis Khan the Conqueror 树形DP+MST (好题)
题意:给出一个n个点m条边的无向边,q次询问每次询问把一条边权值增大后问新的MST是多少,输出Sum(MST)/q. 解法:一开始想的是破圈法,后来想了想应该不行,破圈法应该只能用于加边的情况而不是修 ...
- HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...
- HDU 4126 Genghis Khan the Conqueror MST+树形dp
题意: 给定n个点m条边的无向图. 以下m行给出边和边权 以下Q个询问. Q行每行给出一条边(一定是m条边中的一条) 表示改动边权. (数据保证改动后的边权比原先的边权大) 问:改动后的最小生成树的权 ...
- 刷题总结——Genghis Khan the Conqueror (hdu4126)
题目: Genghis Khan(成吉思汗)(1162-1227), also known by his birth name Temujin(铁木真) and temple name Taizu(元 ...
- HDU 1520.Anniversary party 基础的树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 3586 Information Disturbing(二分+树形dp)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...
- HDU 5682 zxa and leaf 二分 树形dp
zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...
- HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...
- hdu 4612 Warm up 双连通+树形dp思想
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total S ...
随机推荐
- Linux系统性能检测
转自:http://www.cnblogs.com/itech/archive/2011/06/08/2075145.html 一 .uptime uptime命令用于查看服务器运行了多长时间以及有多 ...
- 解决: Project facet Java version 1.8 is not supported
背景 从别处Import一个Java project之后,Eclipse提示“Project facet Java version 1.8 is not supported”. 分析 从错误的描述来看 ...
- docker中部署mongodb副本集
1.基本信息如下 服务器地址 192.168.73.129 副本集名称 rs 容器节点及端口映射 m0 37017:27017 m1 47017:27017 ...
- 仅用CSS3创建h5预加载跳动圈
<head> <meta charset="UTF-8"> <title></title> <style type=" ...
- spring 声明式事务的坑 @Transactional 注解
1.首先环境搭建,jar 我就不写了,什么一些spring-core.jar spring-beans.jar spring-content.jar 等等一些包 省略..... 直接上图: sprin ...
- 为什么stm32有的外设在进行初始化的时候需要将寄存器重设为缺省值?不设置会怎么样?
首先,缺省值就是默认值的意思,默认值可以理解为设计芯片的人认为用这个参数,比较适中,起码不可能耽误你对某一模块进行驱动.然后,为什么除了默认值(缺省值),还有这么多其他的参数可以进行选择呢,那就要看你 ...
- 浅谈AVL树,红黑树,B树,B+树原理及应用(转)
出自:https://blog.csdn.net/whoamiyang/article/details/51926985 背景:这几天在看<高性能Mysql>,在看到创建高性能的索引,书上 ...
- Window虚拟内存管理(转)
内存管理是操作系统非常重要的部分,处理器每一次的升级都会给内存管理方式带来巨大的变化,向早期的8086cpu的分段式管理,到后来的80x86 系列的32位cpu推出的保护模式和段页式管理.在应用程序中 ...
- STL - Vector迭代器简单应用之计算元素和
Description 用vector向量容器装入10个整数,然后,使用迭代器iterator和accumulate算法统计出这10个元素的和 Solution #include "stda ...
- Python的常见异常处理
一.异常处理 1.异常的概念 异常是错误发生的信号,一旦程序出错,并且程序没有处理这个错误,那个就会抛出异常,并且程序的运行随即终止. 2.错误种类 分两种,第一种是:语法错误,这种错误,根本过不了p ...