HDU4126Genghis Khan the Conqueror(最小生成树+并查集)

Genghis Khan the Conqueror

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)

Total Submission(s): 1687    Accepted Submission(s): 501

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.

Source

2011 Asia Fuzhou Regional Contest

#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;

const int N = 3005;
const double inf = 0x3f3f3f3f;
struct EDG
{
    int u,v;
    double c;
};
struct TO
{
    int v;
    double c;
};

vector<TO>tmap[N];
EDG edg[N];
int n,treeEdg[N][N];
double node[N];
EDG tedg[N*N];
int fath[N];

int cmp(EDG a,EDG b)
{
    return a.c<b.c;
}
int findfath(int x)
{
    if(x==fath[x])
        return fath[x];
    fath[x]=findfath(fath[x]);
    return fath[x];
}
double MST(int m)
{
    double sum=0;
    int k=0;
    for(int i=0; i<m; i++)
    {
        int x=findfath(tedg[i].u);
        int y=findfath(tedg[i].v);
        if(x!=y)
        {
            k++;
            treeEdg[tedg[i].u][tedg[i].v]=treeEdg[tedg[i].v][tedg[i].u]=k;
            edg[k].u=tedg[i].u; edg[k].v=tedg[i].v; edg[k].c=tedg[i].c;
            fath[x]=y; sum+=tedg[i].c;
            if(k==n-1)
                break;
        }
    }
    return sum;
}
int main()
{
    int m,q,a,b;
    double c,ans,sum,tc;
    TO ss;

    while(scanf("%d%d",&n,&m)>0&&n+m!=0)
    {
        for(int i=0;i<=n;i++)
        {
            fath[i]=i;
            for(int j=0;j<=n;j++)
                treeEdg[i][j]=N;
        }

        for(int i=0;i<m;i++)
        {
            scanf("%d%d%lf",&a,&b,&c);
            tedg[i].u=a;
            tedg[i].v=b;
            tedg[i].c=c;
        }
        sort(tedg,tedg+m,cmp);
        sum=MST(m);
        scanf("%d",&q);
        ans=0;
        for(int j=0;j<q;j++)
        {
            scanf("%d%d%lf",&a,&b,&c);

            if(treeEdg[a][b]==N)
                ans+=sum;
            else
            {
                for(int i=0;i<=n;i++)
                    fath[i]=i;
                for(int i=1;i<n;i++)
                {
                    if(treeEdg[a][b]==i)
                    {
                       tc=edg[i].c; continue;
                    }
                    int x=findfath(edg[i].u);
                    int y=findfath(edg[i].v);
                    fath[x]=y;
                }
                int flag=0;
                for(int i=0;i<m&&tedg[i].c<c;i++)
                {
                    if(treeEdg[tedg[i].u][tedg[i].v]!=N)
                        continue;

                        int x=findfath(tedg[i].u);
                        int y=findfath(tedg[i].v);
                        fath[x]=y;
                         x=findfath(a);
                         y=findfath(b);
                        if(x==y)
                        {
                            ans=ans+sum-tc+tedg[i].c; flag=1; break;
                        }
                }
                if(flag==0)
                    ans=ans+sum-tc+c;
            }
        }
        printf("%.4lf\n",ans/(q*1.0));
    }
}

时间: 2024-11-07 07:19:52

HDU4126Genghis Khan the Conqueror(最小生成树+并查集)的相关文章

最小生成树-并查集-Kruskal-zoj-2048-special judge

Highways description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting som

洛谷P1547 Out of Hay 最小生成树 并查集

洛谷P1547 Out of Hay 最小生成树 并查集 路径压缩 #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <string> #include <algorithm> #include <iostream> #include <iomanip> using namespace std ;

CSP 201703-4 地铁修建【最小生成树+并查集】

问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁. 地铁由很多段隧道组成,每段隧道连接两个交通枢纽.经过勘探,有m段隧道作为候选,两个交通枢纽之间最多只有一条候选的隧道,没有隧道两端连接着同一个交通枢纽. 现在有n家隧道施工的公司,每段候选的隧道只能由一个公司施工,每家公司施工需要的天数一致.而每家公司最多只能修

bzoj 1050: [HAOI2006]旅行comf(最小生成树+并查集)

1050: [HAOI2006]旅行comf Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2405  Solved: 1282 [Submit][Status][Discuss] Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T ,求一条路径,使得路径上最大边和最小边的比值最小.如果S和T之间没有路径,输出"IMPOSS

hdu-4126 Genghis Khan the Conqueror(最小生成树+树形dp)

题目链接: Genghis Khan the Conqueror Time Limit: 10000/5000 MS (Java/Others)   Memory Limit: 327680/327680 K (Java/Others) Problem Description Genghis Khan(成吉思汗)(1162-1227), also known by his birth name Temujin(铁木真) and temple name Taizu(元太祖), was the fo

最小生成树+并查集(洛谷P1991 无线通讯网)

题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都?有卫星电话)均可以通话,无论 他们相距多远.而只通过无线电收发器通话的哨所之间的距离不能超过 D,这是受收发器 的功率限制.收发器的功率越高,通话距离 D 会更远,但同时价格也会更贵. 收发器需要统一购买和安装,所以全部哨所只能选择安装一种型号的收发器.换句话 说,每一对哨所之间的通话距离都是同一

HDU ACM : 1875 畅通工程再续-&gt;最小生成树(并查集)

解析:最小生成树:Kruskal 算法:并查集实现. 1.首先找出符合要求的边: 2.对找出的边排序: 3.并查集找出n-1条边,无法修通n-1条路则无法实现要求. #include<iostream> #include<cmath> #include<algorithm> using namespace std; struct Point { int x,y; } point[102]; struct Edge { int a,b; double v; bool op

畅通工程-最小生成树+并查集

原题链接:https://vjudge.net/problem/23261/origin 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本.现请你编写程序,计算出全省畅通需要的最低成本. Input 测试输入包含若干测试用例.每个测试用例的第1行给出评估的道路条数 N.村庄数目M ( < 100 ):随后的 N 行对应村庄间道路的成本,每行给

畅通工程再续-最小生成树+并查集

原题链接:https://vjudge.net/problem/15740/origin 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米.当然,为了节省资金,只要求实现任意2个小