HDU-4126(Genghis Khan the Conqueror)

news/2024/11/16 12:32:32/

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;
}

http://www.ppmy.cn/news/966733.html

相关文章

hdu4126Genghis Khan the Conqueror

链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid4126 题意&#xff1a;给定n个点的m条边&#xff0c;其中有q条边中的一条一定会变大&#xff08;每一条变大的概率相同&#xff09;&#xff0c;求变大之后最小生成树边权和的期望。 分析&#xff1a;最小生成树树…

HDU 4126 Genghis Khan the Conqueror

传送门 “在必须不要一条mst边的情况下&#xff0c;怎么快速组织新的mst?” &#xff08;HDU 4081&#xff0c;HDU 4126&#xff09; 无向图&#xff0c;保证每两点之间最多有一条直接相连的边&#xff08;这题保证的东西还挺多的&#xff09;&#xff0c;然后Q次更新&…

Khan Academy Chapter 2 —— Vector

typora-copy-images-to: Khan Academy 文章目录 typora-copy-images-to: Khan Academy@[toc] Khan AcademyChapter 2 —— VectorsVector BasisEquivalent VectorsComponents of vectorsMagnitude of VectorsScalar multiplicationAdding and subtracting vectorsGraphically A…

Khan Academy Precalculus Chapter 1 —— Trigonomety

typora-copy-images-to: Khan Academy typora-copy-images-to: khan academy 文章目录 typora-copy-images-to: Khan Academytypora-copy-images-to: khan academyKhan AcademyPrecalculusChapter 1 —— TrigonometryIntroduction to the trigonometric angle addition identi…

HDU - 4126 Genghis Khan the Conqueror

HDU - 4126 Genghis Khan the Conqueror 树形dpMST 题目描述 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. Af…

Notes for Computer Science of Khan Academy

可汗学院里&#xff0c;Computing板块的内容大致分为&#xff1a;计算机科学原理、信息理论&#xff08;information theory&#xff09;、互联网&#xff08;Internet&#xff09;、数据分析&#xff08;analysis&#xff09;、编程&#xff08;programming&#xff09;、算法…

图的拓扑排序--Khan算法

大家在上大学的时候&#xff0c;应该都遇到过这样的情况&#xff0c;有些高级的课程需要你先完成基础课程后才可以学习。在「图11. 课程关系图」中&#xff0c;如果你想选课程 C&#xff0c;那你需要先完成课程 B&#xff0c;如果你想选课程 B&#xff0c;那么你需要先完成课程…

Khan Academy

Khan Academy是一个免费的学院。 致力于教育改革。 百度百科&#xff1a;ohn Resig 百度百科有记者采访&#xff0c;采访内容比较有意思。 转载于:https://www.cnblogs.com/Tpf386/p/9934158.html