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

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 2524    Accepted Submission(s): 760

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.

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.

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
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 <vector>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=3000+5;
const int inf=1000000000;
struct edge{
    int u,v,w;
int n,m,q;
int a,b,c;
int father[N];
int map[N][N];//map[i][j]表示(i,j)边权值
int dp[N][N];//dp[i][j]表示去掉MST上的(i,j)边后的最佳替换边的长度
bool vis[N][N];//标记是否在MST上
vector<int> Edge[N];
int min(int a,int b){return a<b?a:b;}
int find(int x){
    return father[x];
int cmp(edge e1,edge e2){
    return e1.w<e2.w;
int dfs(int rt,int u,int fa){//求rt点到以u为根的树及其子树的最小距离
    int ans=inf;
    for(int i=0;i<Edge[u].size();i++){
        int v=Edge[u][i];
        if(v==fa) continue;
        int tmp=dfs(rt,v,u);
    if(rt!=fa) //保证这条边不是生成树的边,不然不能更新
        ans=min(ans, map[rt][u]);
    return ans;
int main(){
        if(n==0&&m==0) break;
        double mst=0,sum=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)
        for(int i=0;i<m;i++){
        for(int i=0;i<m;i++){
        for(int i=0;i<n;i++){
        for(int i=0;i<q;i++){
    return 0;



©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客