Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)

Total Submission(s): 2524    Accepted Submission(s): 760


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.

题意:一个N个点的无向图,先生成一棵最小生成树,边上有权值,然后给你Q次询问,每次询问都是x,y,z的形式,表示的意思是在原图中将x,y之间的边增大(一定是变大的)到z时,此时最小生成树里边的值是多少,让你求Q次询问最小生成树的平均值。 
思路:先求出最小生成树,用dp[i][j]表示如果i,j不连接(前提是i,j是一条边上,且在最小生成树中),每次选择一个点作为根节点,用它和一个点的连线来更新dp[i][j],具体看代码,主要参照http://www.cnblogs.com/-sunshine/archive/2013/01/11/2855689.html这个博客的,写的很详细。
#include <vector>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=3000+5;
const int inf=1000000000;
struct edge{
int u,v,w;
}e[N*N];//所有的边
int n,m,q;
int a,b,c;
int father[N];
int map[N][N];//map[i][j]表示(i,j)边权值
int dp[N][N];//dp[i][j]表示去掉MST上的(i,j)边后的最佳替换边的长度
bool vis[N][N];//标记是否在MST上
vector<int> Edge[N];
int min(int a,int b){return a<b?a:b;}
int find(int x){
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
//用于Kruskal使用
int cmp(edge e1,edge e2){
return e1.w<e2.w;
}
//更新dp[i][j],对于i点为根的除j之外的所有子树中的所有的点到j距离的最小值
//确定这些点和j不在一个集合里
int dfs(int rt,int u,int fa){//求rt点到以u为根的树及其子树的最小距离
int ans=inf;
for(int i=0;i<Edge[u].size();i++){
int v=Edge[u][i];
if(v==fa) continue;
int tmp=dfs(rt,v,u);
ans=min(ans,tmp);
dp[u][v]=dp[v][u]=min(dp[u][v],tmp);//注意,这里更新的是u,v
//通过dfs的返回值来更新dp[i][j]
}
if(rt!=fa) //保证这条边不是生成树的边,不然不能更新
ans=min(ans, map[rt][u]);
return ans;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0) break;
double mst=0,sum=0;
for(int i=0;i<n;i++){
Edge[i].clear();
father[i]=i;
for(int j=0;j<n;j++)
map[i][j]=dp[i][j]=inf,
vis[i][j]=1;
}
for(int i=0;i<m;i++){
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
map[e[i].u][e[i].v]=map[e[i].v][e[i].u]=e[i].w;
}
sort(e,e+m,cmp);
for(int i=0;i<m;i++){
a=find(e[i].u);
b=find(e[i].v);
if(a!=b){
father[a]=b;
mst+=e[i].w;
Edge[e[i].u].push_back(e[i].v),
Edge[e[i].v].push_back(e[i].u);
vis[e[i].u][e[i].v]=vis[e[i].v][e[i].u]=0;
}
}
for(int i=0;i<n;i++){
dfs(i,i,-1);
}
scanf("%d",&q);
for(int i=0;i<q;i++){
scanf("%d%d%d",&a,&b,&c);
if(vis[a][b]==1)
sum+=mst;
else
sum+=mst*1.0-map[a][b]+min(dp[a][b],c);
}
printf("%.4lf\n",sum/(double)q);
}
return 0;
}

hdu4126Genghis Khan the Conqueror (最小生成树+树形dp)的更多相关文章

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

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

  2. HDU4126Genghis Khan the Conqueror(最小生成树+并查集)

    Genghis Khan the Conqueror Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 327680/327680 K ...

  3. hdu4126Genghis Khan the ConquerorGenghis Khan the Conqueror(MST+树形DP)

    题目请戳这里 题目大意:给n个点,m条边,每条边权值c,现在要使这n个点连通.现在已知某条边要发生突变,再给q个三元组,每个三元组(a,b,c),(a,b)表示图中可能发生突变的边,该边一定是图中的边 ...

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

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

  5. hdu 4081 最小生成树+树形dp

    思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...

  6. UVA- 1504 - Genghis Khan the Conqueror(最小生成树-好题)

    题意: n个点,m个边,然后给出m条边的顶点和权值,其次是q次替换,每次替换一条边,给出每次替换的边的顶点和权值,然后求出这次替换的最小生成树的值; 最后要你输出:q次替换的平均值.其中n<30 ...

  7. Install Air Conditioning HDU - 4756(最小生成树+树形dp)

    Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...

  8. HDU 5723 Abandoned country(最小生成树 + 树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5723 [题目大意] n座城市,m条路径,求解: 1.最短的路径和,使得n座城市之间直接或者间接连通 ...

  9. Abandoned country(最小生成树+树形DP)

    #include<bits/stdc++.h> using namespace std; struct node{ int u, v, w, nex; bool gone; node(){ ...

随机推荐

  1. Es5数组新增的方法及用法

    1.forEachforEach是Array新方法中最基本的一个,就是遍历,循环.例如下面这个例子: [1, 2 ,3, 4].forEach(alert);等同于下面这个传统的for循环: var ...

  2. 剑指offer 面试题4:二维数组中的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  3. SpringCloud Gateway快速入门

    SpringCloud Gateway cloud笔记第一部分 cloud笔记第二部分Hystrix 文章目录 SpringCloud Gateway Zull的工作模式与Gateway的对比 Rou ...

  4. 基于腾讯云存储网关 CSG 实现视频在线转码分发

    一.背景 随着越来越多的传统业务云化和云端业务发展,数据上云和云端数据处理领域的需求爆发式增长.腾讯云存储网关CSG提供一键部署开箱即用的便捷模式,深度结合COS对象存储生态,为用户提供方便快捷的数据 ...

  5. python学习笔记 | 列表去重

    ''' @author: 人人都爱小雀斑 @time: 2020/3/10 10:29 @desc: ''' L=[1,5,7,4,6,3,0,5,8,4,4] 方法1:for循环 L1=[] for ...

  6. mysqldump 的-T参数

    /usr/local/mysql/bin/mysqldump -uroot -T /tmp lina xuehao 把lina数据库中的xuehao表在tmp目录下备份出来两个文件,一个是纯数据.tx ...

  7. SDUST数据结构 - chap9 排序

    判断题: 选择题: 编程题: 7-1 排序: 输入样例: 11 4 981 10 -17 0 -20 29 50 8 43 -5 输出样例: -20 -17 -5 0 4 8 10 29 43 50 ...

  8. git创建分支并关联远程分支

    1.新建本地分支: 如图,再输入你的分支名字,然后选择从哪个远程分支拉代码,如选择master 至此本地分支创建完成. 2.关联远程分支: (1).先输入git branch -vv,看看分支与远程分 ...

  9. 24V转5V芯片,高输入电压LDO线性稳压器

    PW6206系列是一个高精度,高输入电压低静态电流,高速,低功耗降线性稳压器具有高纹波抑制.输入电压高达40V,负载电流为在VOUT=5V和VIN=7V时高达300mA.该设备采用BCD工艺制造.PW ...

  10. win 10下Apache和PHP的安装配置

    一.下载Apache 官网下载:https://httpd.apache.org/ 或者百度网盘链接:https://pan.baidu.com/s/17zVFNSfzzwDgFti_fboUSA 提 ...