Genghis Khan the Conqueror HDU - 4126(MST)

news/2024/11/16 10:16:44/

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 x i, y i and c i(c i<=10 7), showing that there is a bidirectional road between x i and y i, while the cost of setting up guarders on this road is c i. We guarantee that the graph is connected. The total cost of the graph is less or equal to 10 9.

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 X i, Y i and C i showing that the cost of road (X i, Y i) may change to C i (C i<=10 7). We guarantee that the road always exists and C i 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.

不断向大牛学习

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
#define INF 0x3f3f3f3f
#define N 3003
using namespace std;
int pre[N];
int dis[N][N];
bool vis[N][N];
int dp[N][N];
struct edge
{int a,b;int w;
}e[N*N];
int n,m;
int cmp(edge e1,edge e2)
{return e1.w<e2.w;
}
vector<int> graph[N];
void init()
{for(int i=0;i<n;i++)pre[i]=i;for(int i=0;i<n;i++)graph[i].clear();memset(dp,INF,sizeof(dp));memset(dis,INF,sizeof(dis));memset(vis,false,sizeof(vis));
}
int findd(int x)
{//cout<<x<<" "<<pre[x]<<endl;return pre[x]==x?x:pre[x]=findd(pre[x]);
}
int prime()
{int aa,bb;int ans=0;int total=n;for(int i=0;i<m;i++){aa=findd(e[i].a);bb=findd(e[i].b);if(aa!=bb){total--;pre[aa]=bb;ans+=e[i].w;//cout<<ans<<endl;vis[e[i].a][e[i].b]=vis[e[i].b][e[i].a]=true;graph[e[i].a].push_back(e[i].b);graph[e[i].b].push_back(e[i].a);}if(total==1)break;}return ans;
}
int DFS(int pos,int now,int last)
{int ans=INF;int v;int mn;for(int i=0;i<graph[now].size();i++){v=graph[now][i];if(v==last)continue;mn=DFS(pos,v,now);dp[v][now]=dp[now][v]=min(dp[now][v],mn);ans=min(ans,mn);}if(last!=pos)ans=min(dis[pos][now],ans);return ans;
}
int main()
{int a,b,w;int q;for(;;){scanf("%d%d",&n,&m);if(!n&&!m)break;init();for(int i=0;i<m;i++){scanf("%d%d%d",&a,&b,&w);dis[a][b]=dis[b][a]=w;e[i].a=a;e[i].b=b;e[i].w=w;}sort(e,e+m,cmp);int ori=prime();for(int i=0;i<n;i++)DFS(i,i,-1);//cout<<ori;scanf("%d",&q);long long sum=0;int countt=q;while(q--){scanf("%d%d%d",&a,&b,&w);if(!vis[a][b])sum+=ori;else{int minn=min(dp[a][b],w);sum+=ori-dis[a][b]+minn;}}printf("%.4lf\n",(double)sum/countt);}return 0;
}

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

相关文章

Khan Academy - Statistics and Probability - Unit 4 MODELING DATA DISTRIBUTIONS

MODELING DATA DISTRIBUTIONS PART 1 Percentiles PART 2 Z-scores PART 3 Effects of linear transformation PART 4 Density curves PART 5 Normal distribution and the empirical rule

Salman Khan加入 NFT 潮流,即将推出数字收藏品

印度新德里&#xff1a;在宝莱坞巨星 Amitabh Bachchan 加入元宇宙之后&#xff0c;另一位宝莱坞演员 Salman Khan 周三宣布&#xff0c;他将很快与 BollyCoin 合作推出NFT。 元宇宙是指物理、增强和虚拟现实在共享在线空间中的融合。 为了推出NFT&#xff0c;BollyCoin.com …

我摊牌了我不装了_不装了,我是FPX.Khan,我摊牌了!

1.Khan现身Doinb直播间&#xff0c;不装了&#xff0c;我是FPX.Khan&#xff0c;我摊牌了 Khan加盟FPX的消息早已被实锤&#xff0c;但FPX迟迟没有进行官宣。 不过在今日凌晨Doinb直播过程中&#xff0c;失踪人口Khan出现在了他的直播间&#xff0c;并让Doinb帮他点外卖。给观众…

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

题目&#xff1a; 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 …

Khan Academy - Statistics and Probability - Unit 7 PROBABILITY

PROBABILITY PART 1 Basic theoretical probability PART 2 Basic set operations PART 3 Experimental probability PART 4 Multiplication rule for independent events PART 5 Multiplication ru

khan - linear algebra - Orthogonal complements

文章目录 Orthogonal complements V ⊥ V^\perp V⊥ is a subspace. N ( A ) ( R ( A ) ) ⊥ N(A)(R(A))^\perp N(A)(R(A))⊥ N ( B T ) ( C ( B ) ) ⊥ N(B^T)(C(B))^\perp N(BT)(C(B))⊥ d i m ( V ) d i m ( V ⊥ ) n dim(V)dim(V^\perp)n dim(V)dim(V⊥)nRepresenting …

HDU-4126(Genghis Khan the Conqueror)

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 …

hdu4126Genghis Khan the Conqueror

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