题意:给一图,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. 手游[追忆之青]动画导演:2D动画制作技巧

    转自:http://www.gamelook.com.cn/2016/09/264591 GameLook报道/由一般法人计算机娱乐协会(CESA)主办的CEDEC2016日前在日本横滨举行,诸多开发 ...

  2. DjangoORM执行原生sql

    在Django中使用原生Sql主要有以下几种方式: 一:extra:结果集修改器,一种提供额外查询参数的机制 二:raw:执行原始sql并返回模型实例 三:直接执行自定义Sql   这种方式完全不依赖 ...

  3. mysql连接慢,修改配置文件

    修改配置 port= skip-locking skip-name-resolve

  4. cookies,sessionStorage,localStorage的区别

    sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...

  5. congst与指针

    指向const的指针 //a pointer to const int;指针指向常量对象,相对本指针而言,不能指针指向的对象的常量,不能通过本指针修改常量对象指针,实际的对象不一定的常量 const指 ...

  6. MySQL系统时间函数NOW(),CURRENT_TIMESTAMP(),SYSDATE()的区别

    CURRENT_TIMESTAMP是NOW的同义词,也就是说两者是相同的. SYSDATE函数返回的是执行到当前函数时的时间,而NOW返回的是执行SQL语句时的时间. 测试语句: SELECT NOW ...

  7. 4.2 最邻近规则分类(K-Nearest Neighbor)KNN算法应用

    1 数据集介绍:   虹膜     150个实例   萼片长度,萼片宽度,花瓣长度,花瓣宽度 (sepal length, sepal width, petal length and petal wi ...

  8. Javascript —— 有向图广度优先搜索

    用Javascript实现有向图的广度优先搜索 刚好遇到一个需求,对于一个有向图,指定一个节点 i 作为起点,输出从 i 出发,可以到达的所有节点,也就是图中以 i 作为起点的子连通片,思考了一下,可 ...

  9. 第2章地址Address(WCF全面解析3)

    WCF顾明思义,就是在Windows平台下解决通信(C,Communication)的基础框架(F,Foundation)问题. 终结点是WCF最为核心的对象,因为它承载了所有通信功能.服务通过相应的 ...

  10. java算法 第七届 蓝桥杯B组(题+答案) 6.方格填数

    6.方格填数  (结果填空) 如下的10个格子 (如果显示有问题,也可以参看[图1.jpg]) 填入0~9的数字.要求:连续的两个数字不能相邻.(左右.上下.对角都算相邻) 一共有多少种可能的填数方案 ...