Problem Description
Genghis Khan(成吉思汗)(1162-1227), also known by his birth name Temujin(铁木真) and temple name Taizu(元太祖), was the founder of the Mongol Empire and the greatest conqueror in Chinese history. After uniting many of the nomadic tribes on the Mongolian steppe, Genghis Khan founded a strong cavalry equipped by irony discipline, sabers and powder, and he became to the most fearsome conqueror in the history. He stretched the empire that resulted in the conquest of most of Eurasia. The following figure (origin: Wikipedia) shows the territory of Mongol Empire at that time.

Our story is about Jebei Noyan(哲别), who was one of the most famous generals in Genghis Khan’s cavalry. Once his led the advance troop to invade a country named Pushtuar. The knights rolled up all the cities in Pushtuar rapidly. As Jebei Noyan’s advance troop did not have enough soldiers, the conquest was temporary and vulnerable and he was waiting for the Genghis Khan’s reinforce. At the meantime, Jebei Noyan needed to set up many guarders on the road of the country in order to guarantee that his troop in each city can send and receive messages safely and promptly through those roads.

There were N cities in Pushtuar and there were bidirectional roads connecting cities. If Jebei set up guarders on a road, it was totally safe to deliver messages between the two cities connected by the road. However setting up guarders on different road took different cost based on the distance, road condition and the residual armed power nearby. Jebei had known the cost of setting up guarders on each road. He wanted to guarantee that each two cities can safely deliver messages either directly or indirectly and the total cost was minimal.

Things will always get a little bit harder. As a sophisticated general, Jebei predicted that there would be one uprising happening in the country sooner or later which might increase the cost (setting up guarders) on exactly ONE road. Nevertheless he did not know which road would be affected, but only got the information of some suspicious road cost changes. We assumed that the probability of each suspicious case was the same. Since that after the uprising happened, the plan of guarder setting should be rearranged to achieve the minimal cost, Jebei Noyan wanted to know the new expected minimal total cost immediately based on current information.

Input
There are no more than 20 test cases in the input.
For each test case, the first line contains two integers N and M (1<=N<=3000, 0<=M<=N×N), demonstrating the number of cities and roads in Pushtuar. Cities are numbered from 0 to N-1. In the each of the following M lines, there are three integers xi, yi and ci(ci<=107), showing that there is a bidirectional road between xi and yi, while the cost of setting up guarders on this road is ci. We guarantee that the graph is connected. The total cost of the graph is less or equal to 109.

The next line contains an integer Q (1<=Q<=10000) representing the number of suspicious road cost changes. In the following Q lines, each line contains three integers Xi, Yi and Ci showing that the cost of road (Xi, Yi) may change to Ci (Ci<=107). We guarantee that the road always exists and Ci is larger than the original cost (we guarantee that there is at most one road connecting two cities directly). Please note that the probability of each suspicious road cost change is the same.

Output
For each test case, output a real number demonstrating the expected minimal total cost. The result should be rounded to 4 digits after decimal point.

Sample Input

3 3
0 1 3
0 2 2
1 2 5
3
0 2 3
1 2 6
0 1 6
0 0

Sample Output

6.0000

Hint

The initial minimal cost is 5 by connecting city 0 to 1 and city 0 to 2. In the first suspicious case, the minimal total cost is increased to 6;the second case remains 5; the third case is increased to 7. As the result, the expected cost is (5+6+7)/3 = 6.

题目大意:给出一些边,然后我们可以增大一条边的权值,然后对于每个增加边的权值之后,我们得到的最小生成树的和,然后得到所有询问边权的平均值。
解题思路:修改一条边之后,如果这条边不是原图中的最小生成树中的边,那么不会改变原最小生成树的权值和,如果这条边是原图中的最小生成树的边,那么我们就先删掉这条边,然后可以得到两个连通分量,然后再加上可以连通着这两个连通分量的最小边,那么我们要怎么去求得这个最小边呢?我们可以使用树形DP的方法去求得每个点与其他点的除了最小生成树的最小边。
我们可以使用dp[i][j]记录i点到j点的最小距离,我们先考虑一个点root,我们假设该点位于是一个子树的根,然后我们遍历另一个子树的根,然后我们先求出点v的子树vv结点到root的最大距离,那么此时点root到v的最大距离就等于dis[root][v]和dp[root][vv]的最大值。我们遍历点root,遍历完之后就可以得到所有的dp信息啦。
代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=3100;
const int inf=0x3f3f3f3f;
struct node {int u,v,w;bool operator <(node a)const {return w<a.w;}
} e[maxn*maxn];
struct edge {int v,next;
} t[maxn];
int head[maxn],cnt;
void add(int a,int b) {t[++cnt]=edge{b,head[a]};head[a]=cnt;
}
int mst,n,m,fa[maxn],dis[maxn][maxn],use[maxn][maxn],dp[maxn][maxn];
int findfa(int x) {return fa[x]=(fa[x]==x?x:findfa(fa[x]));
}
void eurl() {sort(e+1,e+m+1);mst=0;int tot=0;for(int i=1; i<=m; i++) {int u=e[i].u,v=e[i].v,w=e[i].w;int tx=findfa(u),ty=findfa(v);if(tx!=ty) {fa[tx]=ty;mst+=w;tot++;use[u][v]=use[v][u]=1;add(u,v);add(v,u);}if(tot>=n-1)return ;}
}
int dfs(int root,int u,int f)
{int minn=inf;for(int i=head[u];i;i=t[i].next){int v=t[i].v;if(v==f)continue;int tmp=dfs(root,v,u);minn=min(tmp,minn);dp[u][v]=dp[v][u]=min(dp[u][v],tmp);}if(f!=root) minn=min(minn,dis[u][root]);return minn;
}
int main() {while(~scanf("%d%d",&n,&m)&&n&&m) {memset(dis,inf,sizeof dis);for(int i=1; i<=n; i++) {fa[i]=i;head[i]=0;dis[i][i]=0;}cnt=0;memset(head,0,sizeof head);memset(dp,inf,sizeof dp);memset(use,0,sizeof use);for(int i=1;i<=m;i++){int x,y,w;scanf("%d%d%d",&x,&y,&w);x++,y++;e[i]=node{x,y,w};dis[x][y]=dis[y][x]=w;}eurl();for(int i=1;i<=n;i++)dfs(i,i,i);int q;scanf("%d",&q);int l=q;double ans=0;while(l--){int x,y,w;scanf("%d%d%d",&x,&y,&w);x++,y++;if(use[x][y]) ans+=mst-dis[x][y]+min(dp[x][y],w);else ans+=mst;}printf("%.4f\n",ans/q);}return 0;
}

HDU-4126(Genghis Khan the Conqueror)相关推荐

  1. HDU 4126 Genghis Khan the Conqueror MST + 树形DP 2011年福州现场赛F题

    题目大意: 就是现在给出一个图, N个点, M条边, N <= 3000, M <= N*N, 但是没有重边, 每条边都有一个权值, 图中没有自环 现在给出Q次询问, 每次询问表示如果更改 ...

  2. 「日常训练」 Genghis Khan the Conqueror(HDU-4126)

    题意 给定\(n\)个点和\(m\)条无向边(\(n\le 3000\)),需要将这\(n\)个点连通.但是有\(Q\)次(\(Q\le 10^4\))等概率的破坏,每次破坏会把\(m\)条边中的某条 ...

  3. 图论500题 ---- 并查集+树形dp+枚举 求解动态的最小生成树 HDU 4126

    题目链接 题目大意: 给一图,n个点,m条边,每条边有个花费,给出q条可疑的边,每条边有新的花费,每条可疑的边出现的概率相同,求不能经过原来可疑边(可以经过可疑边新的花费构建的边),注意每次只出现一条 ...

  4. 杭电OJ分类题目(4)-Graph

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...

  5. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  6. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  7. 一系列图论问题[转]

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  8. 【转】并查集MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 ...

  9. kk_想要学习的知识

    2018/4/27 计算几何 一.简介 计算几何属于ACM算法中比较冷门的分类,在省赛中只在前几年考察过,这两年还没有考过,而且和高精度计算一样,遇到题目主要靠套模板,因此对题意的理解至关重要,而且往 ...

  10. 图论练习题(存起来练)

    =============================以下是最小生成树+并查集======================================  [HDU]  1213 How Man ...

最新文章

  1. AWS攻略——使用CodeBuild进行自动化构建和部署静态网页
  2. P4492-[HAOI2018]苹果树【dp】
  3. 2060. 同源字符串检测
  4. Intellij插件之JRebel
  5. 2021李宏毅机器学习课程笔记——Domain Adaptation
  6. babel 配置整理
  7. 海湾标准汉字码表查询_标准汉字码表
  8. 联想笔记本怎么进入pe系统_lenovo怎么进入pe系统
  9. Bootstrap4与Bootstrap3的区别
  10. 谷歌hosts laod
  11. 便携智能音箱小问mini发布,李志飞:要做苹果一样的公司
  12. kindle亚马逊个人文档不显示_Kindle个人文档服务
  13. 心流:最优体验心理学 1
  14. git操作提示please tell me who you are问题
  15. html预览dwg文件,大佬救命!有关dwg文件预览的问题
  16. 馈线自动化终端(FTU)
  17. 微信小程序canvas生成图片为空白问题
  18. CSS3干货11:页面中的公共CSS代码
  19. linux安装为知笔记
  20. 国产化之x64平台安装银河麒麟操作系统

热门文章

  1. Codewars 刷题笔记(Python)6.Multiples of 3 or 5
  2. FCPX插件:PremiumVFX Ink Title(墨水标题字幕条动画插件)
  3. 北漂IT男返乡2年的三线楼市观察(宜昌夷陵篇)-原创
  4. 红米android刷机在哪,红米手机怎么刷机 红米手机刷机教程大全
  5. 180127 逆向-JarvisOJ(BrokenDriver)(静态分析解法)
  6. 【面试个人成长】2021年过半,社招和校招的经验之谈
  7. 阿里云三网手机号实名认证和印刷文字识别_身份证识别
  8. android自定义导航,Android 高德地图之自定义导航
  9. Linux 中task_struck
  10. 郑捷《机器学习算法原理与编程实践》学习笔记(第二章 中文文本分类(一))...