dfs + 最小生成树 hdoj4126 Genghis Khan the Conqueror
来源:互联网 发布:java cms垃圾回收 编辑:程序博客网 时间:2024/06/11 02:21
题目传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=4126
题意是有一张图要求最小生成树,然而图中的一条边可能会变大,一共有q种可能,每种可能都是等概率的。求改变一条边后最小生成树权和的期望。
节点至多3000个,而q最大是10000,所以暴力的对这10000种情况求一遍最小生成树是会超时的。可以这样想,先求出当前情况下的最小生成树,那么改变的这条边就有两种情况,一种是这条边本身不在当前最小生成树中,那么最小生成树的权和不变;另一种是这条边在最小生成树中,那么我们可以放弃这条边,这时原生成树中就有了两个连通分量,新的最小生成树显然还包含着剩余的边,这时需要找一条权值最小的边,连接这两个连通分量,使之成为一棵新的最小生成树。
如何找这一条边呢?显然对于断开的每一条边,枚举所有可以连接两个连通分量的边,是会超时的。用dfs可以解决,细节看代码。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int INF = 0x3f3f3f3f;const int N = 3005;const int M = (N - 5) * (N - 5) + 5;int map[N][N], pick[N][N], father[N], dis[N][N];vector<int> e[N];int n,m,q;struct E{ int u,v,w;} edge[M];bool cmp(const E& e1, const E& e2){ return e1.w < e2.w;}int find(int x){ while (x != father[x]) { father[x] = father[father[x]]; x = father[x]; } return x;}void init(void){ memset(dis, 0x3f, sizeof(dis)); memset(map, 0x3f, sizeof(map)); memset(pick, 0, sizeof(pick)); for (int i = 0; i < n; i ++) { e[i].clear(); father[i] = i; }}// dfs(root, u, pre)表示 pre到u的边断开时,以root为pre这棵子树的出口与u的子树相连的最短路径 // 一开始一个连通分量S有n - 1个节点, 另一个T有1个节点,每次S减少一个节点,T增加一个节点,从S经由root到T的最短路径是min(上一个最短路径, 由root直接到新加入节点路径),类似dijkstra算法思想的逆过程int dfs(int root, int u, int pre){ int ret = INF; for (int i = 0; i < e[u].size(); i ++) { int v = e[u][i]; if (v != pre) { int t = dfs(root, v, u); ret = min(ret, t); // 使用dis数组记录,所有情况下的最小值即为最短的边 dis[u][v] = dis[v][u] = min(dis[u][v], t); } } if (pre != root) { ret = min(ret, map[root][u]); } return ret;}int main(){ while (~scanf("%d%d",&n,&m), n || m) { init(); int u, v, w; for (int i = 0; i < m; i ++) { scanf("%d%d%d",&u,&v,&w); map[u][v] = map[v][u] = w; edge[i].u = u; edge[i].v = v; edge[i].w = w; } sort(edge, edge + m, cmp); int cnt = 0, x, y, ret = 0; for (int i = 0; i < m, cnt < n - 1; i ++) { x = find(edge[i].u); y = find(edge[i].v); if (x != y) { father[x] = y; ++ cnt; e[edge[i].u].push_back(edge[i].v); e[edge[i].v].push_back(edge[i].u); pick[edge[i].u][edge[i].v] = pick[edge[i].v][edge[i].u] = 1; ret += edge[i].w; } }// printf("%d\n", ret); for (int i = 0; i < n; i ++) { dfs(i, i, -1); } scanf("%d",&q); double sum = 0; for (int i = 1 ; i <= q; i ++) { scanf("%d%d%d",&u,&v,&w); if (!pick[u][v]) { sum += ret; } else { sum += ret - map[u][v] + min(w, dis[u][v]); } } printf("%.4lf\n", 1.0 * sum / q); } return 0;}
阅读全文
0 0
- dfs + 最小生成树 hdoj4126 Genghis Khan the Conqueror
- Genghis Khan the Conqueror----HDU_4126----最小生成树_DFS_DP
- HDU_4126 Genghis Khan the Conqueror 最小生成树
- hdu 4126 Genghis Khan the Conqueror 最小生成树变形
- hdu4126 Genghis Khan the Conqueror 树形dp+最小生成树
- HDU 4126 Genghis Khan the Conqueror(最小生成树)
- poj 4006 Genghis Khan the Conqueror <最小生成树 + dfs + 插入>
- (intermediate) 最小生成树 UVA 1504 Genghis Khan the Conqueror
- HDU - 4126 Genghis Khan the Conqueror(树形DP + 最小生成树)
- HDU 4126 Genghis Khan the Conqueror(最小生成树+树形DP)
- hdu4126 Genghis Khan the Conqueror--最小生成树 & 树形DP(待解决)
- uva 1504 - Genghis Khan the Conqueror(生成树)
- HDU 4126 Genghis Khan the Conqueror(树形dp,MST,破坏原有最小生成树边后的最小生成树代价)
- hdu4126.Genghis Khan the Conqueror
- hdu4126.Genghis Khan the Conqueror
- Genghis Khan the Conqueror HDU
- HDU 4126 Genghis Khan the Conqueror (生成树,并查集)
- HDU4126Genghis Khan the Conqueror(最小生成树+并查集)
- ogl 第一个三角形
- Java 编程题目 第十一题
- 关于nth-child()伪类选择器选取元素设置transform样式但是无法通过hover改变transform的问题
- 心得
- 深入浅出聊聊企业级API网关
- dfs + 最小生成树 hdoj4126 Genghis Khan the Conqueror
- 基于深度神经网络技术提高产品生产良品率的研究
- 道路 AND 机器学习 OR 软件工程
- ASP.NET服务器控件
- 2017第八届蓝桥杯决赛之旅
- java 文件File 文件的创建 删去 重命名
- 1126. Eulerian Path (25)[欧拉回路]
- 第十四周算法题
- (1)ffmpeg在linux上的编译