题意:给一图,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)的更多相关文章

  1. HDU-4126 Genghis Khan the Conqueror 树形DP+MST (好题)

    题意:给出一个n个点m条边的无向边,q次询问每次询问把一条边权值增大后问新的MST是多少,输出Sum(MST)/q. 解法:一开始想的是破圈法,后来想了想应该不行,破圈法应该只能用于加边的情况而不是修 ...

  2. HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...

  3. HDU 4126 Genghis Khan the Conqueror MST+树形dp

    题意: 给定n个点m条边的无向图. 以下m行给出边和边权 以下Q个询问. Q行每行给出一条边(一定是m条边中的一条) 表示改动边权. (数据保证改动后的边权比原先的边权大) 问:改动后的最小生成树的权 ...

  4. 刷题总结——Genghis Khan the Conqueror (hdu4126)

    题目: Genghis Khan(成吉思汗)(1162-1227), also known by his birth name Temujin(铁木真) and temple name Taizu(元 ...

  5. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. HDU 3586 Information Disturbing(二分+树形dp)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...

  7. HDU 5682 zxa and leaf 二分 树形dp

    zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...

  8. HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...

  9. hdu 4612 Warm up 双连通+树形dp思想

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total S ...

随机推荐

  1. 【做题记录】USACO gold * 50(第一篇)

    orz xhk 5/50 1597: [Usaco2008 Mar]土地购买 $ f[i]=min(f[j]+x[i]*y[j+1]) $ 然后斜率优化 1699: [Usaco2007 Jan]Ba ...

  2. 2_python之路之多级菜单

    python之路之多级菜单 1.使用知识点 (1)列表,字典的使用 (2)if条件判断语句 (3)for/while循环的使用 2.代码详细 #!/usr/bin/env python # _*_ c ...

  3. Python Twisted系列教程21: Twisted和Haskell

    作者:dave@http://krondo.com/twisted-and-haskell/  译者: Cheng Luo 你可以从”第一部分 Twist理论基础“开始阅读:也可以从”Twisted ...

  4. 获取lable选中时触发事件

    通常做网页时不会用radio和checkbox的原有样式, 会进行样式美化. 如何在点击checkbox时触发一个事件呢? <div class="main-checkbox" ...

  5. objective c to pas

    https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol ...

  6. Linux ALSA声卡驱动之一:ALSA架构简介

    声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢! 一.  概述 ALSA是Advanced Linux Sound Architecture ...

  7. 反射与dynamic

    反射 var a = Assembly.GetExecutingAssembly(); Type type = a.GetType("CLRTest.ReflectClass"); ...

  8. go_变量定义

    package main import "fmt" var( aa =3 bb ="kkk" cc =true )//go语言中,变量可以定义在函数外面,并不是 ...

  9. Ajax工作原理和原生JS的ajax封装

    前言: 之所以用ajax作为博客的开篇,是因为无论从ajax的出现还是从它的作用上来说,ajax对于前端无疑是意义重大的.甚至可以说,是ajax带来了前端这个行业.当然,历史并不能说明当下,曾经的辉煌 ...

  10. 本地yum源构建以及Docker离线安装

    Docker离线安装以及本地yum源构建 在docker的使用过程中有时候会遇到一些私有化部署的问题,就是在一些无法上网的机器上面安装使用dokcer,这就引出了docker的离线安装的问题,dock ...