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

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

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

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

  2. UVA 1395 苗条的生成树(最小生成树+并查集)

    苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...

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

    问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市 ...

  4. HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...

  5. 关于最小生成树(并查集)prime和kruskal

    适合对并查集有一定理解的人.  新手可能看不懂吧.... 并查集简单点说就是将相关的2个数字联系起来 比如 房子                      1   2    3   4  5   6 ...

  6. 【BZOJ4144】[AMPPZ2014]Petrol(最短路+最小生成树+并查集)

    Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油站可以补满. q次询问,每次给出x,y,b,表示出发点是 ...

  7. bzoj 3559: [Ctsc2014]图的分割【最小生成树+并查集】

    读题两小时系列-- 在读懂题意之后,发现M(c)就是c这块最大权割边也就是的最小生成树的最大权边的权值,所以整个问题都可以在MST的过程中解决(M和c都是跟着并查集变的) 不过不是真的最小生成树,是合 ...

  8. Regional Changchun Online--Travel(最小生成树&& 并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  9. UOJ14 UER #1 DZY Loves Graph(最小生成树+并查集)

    显然可以用可持久化并查集实现.考虑更简单的做法.如果没有撤销操作,用带撤销并查集暴力模拟即可,复杂度显然可以均摊.加上撤销操作,删除操作的复杂度不再能均摊,但注意到我们在删除时就可以知道他会不会被撤销 ...

随机推荐

  1. HTTP重定向

    404 Not Found301 Moved Permanently302 Found500 Internal Server ErrorHTTP重定向就是通过301和302两种状态码来实现的. 302 ...

  2. 九度oj 题目1443:Tr A

    题目描述: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. 输入: 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n & ...

  3. 在Python中建立N维数组并赋初值

    在Python中,由于不像C++/Java这样的语言可以方便的用a[i][j]=0的方式,建立二维数组并赋初值,所以需要一个相对巧妙的方法. 可以用列表解析的方式,eg: >>> m ...

  4. 知名游戏引擎公司Havok发布免费3D移动游戏引擎“Project Anarchy”

    自EA发布“寒霜”引擎(Frostbite Engine)移动版后,知名游戏引擎公司Havok也发布了免费的3D移动游戏引擎“Project Anarchy”. 据悉,6月底时候,Intel旗下知名游 ...

  5. 在SAE搭建微信公众账号服务

    让我们回到2014年11月,从公司请假回成都,在天府软件园B区旁边的小区里,那个10多平米的出租屋里,闲来无事,我想找个事情做一做,好让我这漂浮的心静下来.大约在半年前就申请了微信的一个公众账号,一直 ...

  6. hdu5730 Shell Necklace 【分治fft】

    题目 简述: 有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案 题解 设\(f[i]\)表示长度为\(i\)时的方案数 不难得dp方程: \[f[i] = \su ...

  7. BZOJ2653 middle 【二分 + 主席树】

    题目 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个 长度为n的序列s.回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[c ...

  8. fzu 1753 质因数的应用

    Another Easy Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  9. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  10. php validator classes

    <?php /** * 验证类 */ class Validator { /* 函数名称:isNumber 简要描述:检查输入的是否为数字 输入:string 输出:boolean */ pub ...