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 ...
随机推荐
- PyQt 5菜单和工具栏
QMainWindow类提供主要应用程序的窗口,有添加状态栏.工具栏.菜单栏等功能 状态栏 self.statusBar().showMessage('Ready') # 创建一个状态栏 # 状态栏显 ...
- Maven的学习
Maven是Apache的一个项目,它使用对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具.它集项目的构建,清理,编译等于一体,以前我们在使用IDE时,基本上一 ...
- Html 上手
<!-- ctrl + / 注释 ctrl + D 复制整行 html:超文本标记语言标记 标签 元素格式<标签名>标签内容</标签名>标签的写法标签的正确嵌套标签特性 ...
- Futures
Futures is a framework for expressing asynchronous code in C++ using the Promise/Future pattern. Ove ...
- input标签存在的兼容问题?
当input标签在type为text时,在Firefox和Safari中的默认高度为22像素(包括上下边框)宽度为146像素(包括左右边框),而在IE中的默认高度为24像素,而宽度却和Firefox和 ...
- nrm操作
nrm操作 nrm use cnpm // 选择镜像nrm ls //查看镜像
- AngularJS的 $resource服务 关于CRUD操作
AngularJs 的CRUD 操作 是指对数据库的操作: CRUD其实是数据库基本操作中的Create(创建).ReadRetrieve(读取).Update(更新).Delete(删除).而这里的 ...
- shell编程——内部变量
常用的内部变量有:echo, eval, exec, export, readonly, read, shift, wait, exit 和 点(.) echo:将变量名指定的变量显示到标准输出 [r ...
- 解决linux下root运行Elasticsearch异常
如果以root身份运行将会出现以下问题 root@yxjay:/opt/elasticsearch-2.3.5/bin# ./elasticsearchException in thread &quo ...
- 安装atop笔记
atop 官网: https://www.atoptool.nl/downloadatop.php 1.直接下载源码安装: https://www.atoptool.nl/download/atop- ...