【Uvalive 5834】 Genghis Khan the Conqueror (生成树,最优替代边)
【题意】
一个N个点的无向图,先生成一棵最小生成树,然后给你Q次询问,每次询问都是x,y,z的形式, 表示的意思是在原图中将x,y之间的边增大(一定是变大的)到z时,此时最小生成数的值是多少。最后求Q次询问最小生成树的平均值。 N<=3000 , Q<=10000
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.
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.
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
【分析】
WC这题做了我一整天!!!
哭了!!!!
死在所谓的树形DP上面了,啊啊啊啊啊,好垃圾啊。。
我真的觉得这个TreeDP挺难搞的,虽然不难想,但是实现起来真的各种bug。。
还是看了别人的题解才打出来TAT。。
正题->_->
这题的关键是在求出最小生成树之后求出去掉生成树任意一条边后剩下的两颗树的距离,可以证明这个距离就是最佳替换边的长度,而把原来最小生成树的边换成最佳替换边后所得到的生成树就是原图中去掉那条边的最小生成树,这个用反证法可以证明,如果新得到的树不是最小生成树可以推出原来的树也不是最小生成树。
那么就是看一看是要用最小替换边,还是直接修改当前的边的值了。
问题就是求最小替换边,用TreeDP实现。
1. 用S1[i][j]表示树A中的点i 到 树B(j点所在的树)的最近距离,这个过程可以在一边dfs就可以出来,对于每个 i 的dfs 复杂度是O(n) ,外加一个n的循环求出每个点,这里的总复杂度为 O(n^2)。
2. 通过求出来的S1[i][j] 再用一个dfs 求出 树B 到 树A的最近距离,(方法:枚举树A中的所有点 到 树B的最近距离,取其中的最小值。)显然, 这个求出来的值是我们要的最小替代边,把它保存到一个ANS[i][j]数组里面,(best[i][j]表示去掉边<i,j>后它的最小替代边的值)这里的总复杂度为 O(n^2)。
打法好高级,主要是不能算上删掉的边,而其他的都要算上,这里挺难搞的。。
看图吧,看图好懂一点。。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 3010
#define INF 0xfffffff int dis[Maxn][Maxn]; struct node
{
int x,y,c,next;
}tt[Maxn*Maxn],t[*Maxn]; bool cmp(node x,node y) {return x.c<y.c;} int mymin(int x,int y) {return x<y?x:y;}
int mymax(int x,int y) {return x>y?x:y;}
int n,m; int fa[Maxn];
int ffa(int x)
{
if(fa[x]!=x) fa[x]=ffa(fa[x]);
return fa[x];
} int first[Maxn],len;
void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int s1[Maxn][Maxn],ff[Maxn];
int dfs(int x,int f,int rt)
{
s1[rt][x]=INF;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
int y=t[i].y;
s1[rt][x]=mymin(s1[rt][x],dfs(y,x,rt));
}
if(f!=rt) s1[rt][x]=mymin(s1[rt][x],dis[rt][x]);
return s1[rt][x];
} bool in[Maxn][Maxn];
int ffind(int x,int f,int rt)
{
int now=s1[x][rt];
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
int y=t[i].y;
now=mymin(now,ffind(y,x,rt));
}
return now;
} int ans[Maxn][Maxn];
int sum;
void init()
{
memset(dis,,sizeof(dis));
for(int i=;i<=m;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
x++;y++;
tt[i].x=x;tt[i].y=y;tt[i].c=c;
dis[x][y]=dis[y][x]=c;
}
//kruskal
sort(tt+,tt++m,cmp);
for(int i=;i<=n;i++) fa[i]=i;
int cnt=;
len=;sum=;
memset(first,,sizeof(first));
// memset(ans,63,sizeof(ans));
for(int i=;i<=m;i++)
{
if(ffa(tt[i].x)!=ffa(tt[i].y))
{
fa[ffa(tt[i].x)]=ffa(tt[i].y);
ins(tt[i].x,tt[i].y,tt[i].c);
ins(tt[i].y,tt[i].x,tt[i].c);
cnt++;
sum+=tt[i].c;
}
if(cnt==n-) break;
}
} void query()
{
int q;
double fans=;
scanf("%d",&q);
for(int i=;i<=q;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
x++;y++;
if(in[x][y]) fans+=1.0*mymin(sum+c-dis[x][y],sum+ans[x][y]-dis[x][y]);
else fans+=1.0*sum;
}
printf("%.4lf\n",fans*1.0/q);
} int main()
{
while()
{
scanf("%d%d",&n,&m);
if(n==&&m==) break;
init();
for(int i=;i<=n;i++) dfs(i,,i);
memset(ans,,sizeof(ans));
memset(in,,sizeof(in));
for(int i=;i<=n;i++)
{
for(int j=first[i];j;j=t[j].next)
{
int y=t[j].y;
ans[i][y]=ans[y][i]=mymin(ans[i][y],ffind(y,i,i));
in[i][y]=in[y][i]=;
}
}
query();
}
return ;
}
2016-11-02 20:40:32
哭了====再做一道k度限制生成树就不做了TAT。。
【Uvalive 5834】 Genghis Khan the Conqueror (生成树,最优替代边)的更多相关文章
- uvalive 5834 Genghis Khan The Conqueror
题意: 给出一个图,边是有向的,现在给出一些边的变化的信息(权值大于原本的),问经过这些变换后,MST总权值的期望,假设每次变换的概率是相等的. 思路: 每次变换的概率相等,那么就是求算术平均. 首先 ...
- HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...
- 刷题总结——Genghis Khan the Conqueror (hdu4126)
题目: Genghis Khan(成吉思汗)(1162-1227), also known by his birth name Temujin(铁木真) and temple name Taizu(元 ...
- UVA- 1504 - Genghis Khan the Conqueror(最小生成树-好题)
题意: n个点,m个边,然后给出m条边的顶点和权值,其次是q次替换,每次替换一条边,给出每次替换的边的顶点和权值,然后求出这次替换的最小生成树的值; 最后要你输出:q次替换的平均值.其中n<30 ...
- HDU 4126 Genghis Khan the Conqueror MST+树形dp
题意: 给定n个点m条边的无向图. 以下m行给出边和边权 以下Q个询问. Q行每行给出一条边(一定是m条边中的一条) 表示改动边权. (数据保证改动后的边权比原先的边权大) 问:改动后的最小生成树的权 ...
- HDU 4126 Genghis Khan the Conqueror (树形DP+MST)
题意:给一图,n个点,m条边,每条边有个花费,给出q条可疑的边,每条边有新的花费,每条可疑的边出现的概率相同,求不能经过原来可疑边 (可以经过可疑边新的花费构建的边),注意每次只出现一条可疑的边,n个 ...
- 「日常训练」 Genghis Khan the Conqueror(HDU-4126)
题意 给定\(n\)个点和\(m\)条无向边(\(n\le 3000\)),需要将这\(n\)个点连通.但是有\(Q\)次(\(Q\le 10^4\))等概率的破坏,每次破坏会把\(m\)条边中的某条 ...
- HDU-4126 Genghis Khan the Conqueror 树形DP+MST (好题)
题意:给出一个n个点m条边的无向边,q次询问每次询问把一条边权值增大后问新的MST是多少,输出Sum(MST)/q. 解法:一开始想的是破圈法,后来想了想应该不行,破圈法应该只能用于加边的情况而不是修 ...
- HDU4126Genghis Khan the Conqueror(最小生成树+并查集)
Genghis Khan the Conqueror Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 327680/327680 K ...
随机推荐
- Google Developers中国网站发布!(转)
Google Developers 中国网站是特别为中国开发者而建立的,它汇集了 Google 为全球开发者所提供的开发技术资源,包括 API 文档.开发案例.技术培训的视频.并涵盖了以下关键开发技术 ...
- java log日志的输出。
在Spring框架中添加日志功能: pom.xml <dependency> <groupId>log4j</groupId> <artifactId> ...
- 在.Net中进行跨线程的控件操作(下篇:BackgroundWorker)
在.Net中,如果我们在非UI线程上访问窗体上的控件的时候,会产生一个跨线程调用的异常,那么如何处理这种情况呢?在上一章中,我介绍了使用Control.Invoke方法,如果你不习惯使用委托,那么.N ...
- SQL Server 事务与锁
了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...
- OS X环境下SVN回滚工程到指定版本,回滚指定文件到指定版本
1.打开命令行终端 2.cd + 工程或文件目录 3.svn update 工程目录或文件目录 -r 版本号 在Xcode中选中文件,右键选择''show in finder''(也可以用快捷键,不过 ...
- C# 中使用win32函数 GetScrollInfo返回false 返回引用全是零的问题
最近做一个项目要获得ScrollBar的位置,因为.net找不到此类功能,只好用MFC中的函数了,GetScrollPos只返回listview顶部的位置,此时我找到了GetScrollInfo,觉得 ...
- NET异步调用Webserver
之前,有个同事跑来问我一堆的什么多线程异步进行调用Sap的服务再突然把进程关闭,还说要设置一个循环判断调用的结果,搞得我听的一头雾水,但是我明显感觉到他的设计思路已经渐行渐远了...已经再偏远的山区中 ...
- javax.el.PropertyNotFoundException: Property 'aDesc' not found on type
这个问题是是在我使用jeesite自动代码是产生的,原因是实体类的属性命名规范不合格,我在网上看到类的属性前三个字母不能出现大写 解决办法:将类的属性大小写改一下
- References & the Copy-Constructor
1 There are certain rules when using references: (Page 451) A reference must be initialized when it ...
- sass 入门教程
1.引言 众所周知css并不能算是一们真正意义上的“编程”语言,它本身无法未完成像其它编程语言一样的嵌套.继承.设置变量等工作.为了解决css的不足,开发者们想到了编写一种对css进行预处理的“中间语 ...