题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4126

Genghis Khan the Conqueror

Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 327680/327680 K (Java/Others)
#### 问题描述
> 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.
#### 输入
> 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
> The next line contains an integer Q (1 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.
####样例输入
> 3 3
> 0 1 3
> 0 2 2
> 1 2 5
> 3
> 0 2 3
> 1 2 6
> 0 1 6
> 0 0

样例输出

6.0000

题意

给你一个无相图,要你求最小生成树,现在有q个查询,每个查询会改变一条边的权值,然后再改回去,要你求出每种情况下的最小生成树,最后求一下平均。

题解

1、如果改变的边不在我们一开始求的mst上,那么答案就是mst。

2、否则,把指定的树边删了,我们得到两个顶点集,那么代替原先那条边的一定是这两个集合的最短距离。所以我们只要处理出best[u][v](既u所在的集合和v所在的集合的最短距离)就能处理第2种情况了。做法是两次树dp,具体看代码。

dp[u][v]:顶点u到集合v的最短距离。

best[u][v]:集合u到集合v的最短距离。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=3030; struct Edge {
int u,v,w;
Edge(int u,int v,int w):u(u),v(v),w(w) {}
Edge() {}
bool operator < (const Edge& tmp) const {
return w<tmp.w;
}
} egs[maxn*maxn]; int n,m;
int G[maxn][maxn],dp[maxn][maxn],best[maxn][maxn];
bool used[maxn][maxn];
VPII tre[maxn]; int fa[maxn];
int find(int x) {
return fa[x]=fa[x]==x?x:find(fa[x]);
} void dfs(int u,int fa,int rt) {
if(u!=rt&&!used[u][rt]) dp[rt][u]=min(dp[rt][u],G[rt][u]);
rep(i,0,tre[u].sz()) {
int v=tre[u][i].X;
if(v==fa) continue;
dfs(v,u,rt);
dp[rt][u]=min(dp[rt][u],dp[rt][v]);
}
} int dfs2(int u,int fa,int rt) {
if(best[u][rt]<100000000) return best[u][rt];
best[u][rt]=min(best[u][rt],dp[u][rt]);
for(int i=0; i<tre[u].sz(); i++) {
int v=tre[u][i].X;
if(v==fa) continue;
dfs2(v,u,rt);
best[u][rt]=min(best[u][rt],best[v][rt]);
}
return best[u][rt];
} ///最小生成树
double kruskal() {
sort(egs,egs+m);
double mst=0;
for(int i=0; i<m; i++) {
int u=egs[i].u,v=egs[i].v,w=egs[i].w;
int pu=find(u);
int pv=find(v);
if(pu!=pv) {
fa[pv]=pu;
tre[u].pb(mkp(v,w));
tre[v].pb(mkp(u,w));
used[u][v]=used[v][u]=1;
mst+=w;
}
}
return mst;
} void init() {
clr(G,0x3f);
clr(dp,0x3f);
clr(best,0x3f);
clr(used,0);
for(int i=0; i<n; i++) fa[i]=i,tre[i].clear();
} int main() {
while(scf("%d%d",&n,&m)==2&&n) {
init();
for(int i=0; i<m; i++) {
int u,v,w;
scf("%d%d%d",&u,&v,&w);
G[u][v]=G[v][u]=w;
egs[i]=Edge(u,v,w);
} double mst=kruskal(); ///树形dp,dp[u][v]表示u到以v为根的子树的最短距离,既点到集合的距离(以u为根开始遍历,且只用非树边更新)
for(int i=0; i<n; i++) dfs(i,-1,i); ///记忆化搜索,best[u][v]表示以u为根的子树和以v为根的子树之间的最短距离,既集合到集合的距离
///对于子树v,我们已经求出了所有的点u到它的最短距离,现在只要遍历所有的u,求出最小值即可。
for(int i=0; i<n; i++) {
for(int j=0; j<tre[i].sz(); j++) {
int v=tre[i][j].X;
best[i][v]=best[v][i]=dfs2(i,v,v);
}
} int q;
scf("%d",&q);
double ans=0;
rep(i,0,q) {
int u,v,w;
scf("%d%d%d",&u,&v,&w);
if(used[u][v]) {
///在树边上
ans+=mst-G[u][v]+min(w,best[u][v]);
} else {
///不在树边上
ans+=mst;
}
} prf("%.4lf\n",ans/q);
}
return 0;
} //end-----------------------------------------------------------------------

HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp的更多相关文章

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

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

  2. hdu4126Genghis Khan the Conqueror (最小生成树+树形dp)

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

  3. HDU 4126 Genghis Khan the Conqueror (树形DP+MST)

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. C++程序设计--实验二

    第二次实验主要内容是函数重载,快速排序及其模板实现,简单的user类实现. 实验结论: 一.函数重载编程练习 /*编写重载函数add(),实现对int型,double型和Complex型数据的加法.在 ...

  2. Go语言连接Oracle(就我这个最全)

    综合参考了网上挺多的方案 倒腾了半天终于连接好了 Go都出来这么多年了 还没有个Oracle的官方驱动... 过程真的很蛋疼..一度想放弃直接连ODBC 首先交代一下运行环境和工具版本: WIN10 ...

  3. 20155327 java第四周学习笔记

    20155327 java第四周学习笔记 五六章知识整理 1子类与父类 父类是接口或者是抽象类,子类必须继承自父类. 2子类的继承性 在Java中,通过关键字extends继承一个已有的类,被继承的类 ...

  4. noip2017 TG 游记

    嗨小朋友们大家好,还记得我是谁吗?我就是为GG代言的蒟蒻--xzz 今天呐我特别的要向HN的dalao们ZJ的巨佬们还有全国的神犇们问声好 为什么呢因为我们在2017年11月份来到了吔屎的长沙理工大学 ...

  5. java两年工作经验有什么经验

    这两年里,了解了完整项目的开发过程,知道如何快速入手一个完全没接触过的项目:譬如先了解数据库关系后,马上熟悉一个功能从前端到后端的实现过程,自己再写一个功 能,这样子就能马上上手开发项目,之后在慢慢了 ...

  6. Qt-网易云音乐界面实现-7 消息中心实现,主要是QListWidget 自定义Item 和QTabwidget使用

    最近写的有点烦躁, 感觉内容真的很多!很多!很多. 目前真的想知道网易官方在出这款产品是,用了多少人和多长时间. 今天写的这个消息中心,有点糙,只是原理实现了没有完全复制过来,心里有团火,不想写了. ...

  7. JDBC多表操作

    一.一对多关系:部门管理系统 1.数据库 create table department ( id ) primary key, name ) ); create table employee ( i ...

  8. 零基础学python之构建web应用(入门级)

    构建一个web应用 前面的学习回顾: IDLE是Python内置的IDE,用来试验和执行Python代码,可以是单语句代码段,也可以是文本编辑器中的多语句程序. 四个内置数据结构:列表.字典.集合和元 ...

  9. 【Python学习笔记】正则表达式

    Ref:https://deerchao.net/tutorials/regex/regex.htm#greedyandlazy 1. 常用元字符 2.字符转义 查找元字符本身时,需要使用\来取消这些 ...

  10. python破解网吧收费系统,远控网吧电脑设备!

      我今天呢 , 我就没事跟着朋友喝酒喝酒啊.喝了很多啊.晚上到旁边的酒店开了一个房间,到了酒店才十点! 感觉没啥事情干的,那就去网吧走走看把,看到是一个嘟嘟牛的,和上次是一样的.还是照常用MS170 ...