博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp
阅读量:5051 次
发布时间:2019-06-12

本文共 4913 字,大约阅读时间需要 16 分钟。

题目链接:

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<=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.

输出

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#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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<<" = "<
<
VI;typedef pair
PII;typedef vector
> 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

转载于:https://www.cnblogs.com/fenice/p/5955496.html

你可能感兴趣的文章
APP接口自动化测试JAVA+TestNG(一)之框架环境搭建
查看>>
php底层--1
查看>>
Servlet生命周期引起的问题
查看>>
关于gulp入门之图片压缩
查看>>
ZOJ 2136 Longest Ordered Subsequence
查看>>
Introduction to my galaxy engine 2: Depth of field
查看>>
shell判断网络主机存活
查看>>
根据时间戳,增量同步数据的解决办法
查看>>
03 SeekBar 音频播放拖拽进度条
查看>>
自定义view实现阻尼效果的加载动画
查看>>
log4net介绍及使用
查看>>
CMS:文章管理之视图(3)
查看>>
清北学堂的小技巧和小收获
查看>>
模型压缩方向一个很牛的paper
查看>>
Android--AsyncTask异步加载详解
查看>>
YARN学习总结
查看>>
C#基础温习(2):温习控制台程序(二)
查看>>
一些文章
查看>>
注解@ResponseBody的作用
查看>>
java main函数不执行?
查看>>