hdu-4126 Genghis Khan the Conqueror(最小生成树+树形dp)
2016-04-13 23:45
531 查看
题目链接:
Memory Limit: 327680/327680 K (Java/Others)
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
[align=left] [/align]
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.
[align=left]Output[/align]
[align=left] [/align]
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.
[align=left]Sample Input[/align]
[align=left] [/align]
3 3
0 1 3
0 2 2
1 2 5
3
0 2 3
1 2 6
0 1 6
0 0
[align=left]Sample Output[/align]
[align=left] [/align]
6.0000
题意:
改变其中一条边的权值时的最小生成树的权值和的平均值;
思路:
求一次最小生成树,再看改变的边是否为最小生成树上的边,不是的话还是取最小生成树的值,在上面的话变成了求两棵子树的最短距离,然后就巴拉巴拉一堆;搞了一夜晚还没怎么搞懂;
AC代码:
Genghis Khan the Conqueror
Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 327680/327680 K (Java/Others)
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
[align=left] [/align]
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.
[align=left]Output[/align]
[align=left] [/align]
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.
[align=left]Sample Input[/align]
[align=left] [/align]
3 3
0 1 3
0 2 2
1 2 5
3
0 2 3
1 2 6
0 1 6
0 0
[align=left]Sample Output[/align]
[align=left] [/align]
6.0000
题意:
改变其中一条边的权值时的最小生成树的权值和的平均值;
思路:
求一次最小生成树,再看改变的边是否为最小生成树上的边,不是的话还是取最小生成树的值,在上面的话变成了求两棵子树的最短距离,然后就巴拉巴拉一堆;搞了一夜晚还没怎么搞懂;
AC代码:
#include <bits/stdc++.h> using namespace std; const int N=9e6+4; typedef long long ll; const ll mod=1e9+7; const int inf=1e9; const double PI=acos(-1.0); int n,m,dis[3002][3002],p[3002],vis[3002],u,v,w,flag[3002][3002],dp[3002][3002]; struct Edge { int l,r,len; }; Edge edge ; int cmp(Edge x,Edge y) { return x.len<y.len; } int findset(int x) { if(x==p[x])return x; return p[x]=findset(p[x]); } int same(int x,int y) { int fx=findset(x),fy=findset(y); if(fx!=fy)p[fx]=fy; } vector<int>vc[3002]; int dfs(int num,int x,int father) { int l=vc[x].size(); int ans=inf; for(int i=0;i<l;i++) { int y=vc[x][i]; if(y!=father) { int temp=dfs(num,y,x);ans=min(ans,temp); dp[x][y]=dp[y][x]=min(dp[y][x],temp); } } if(num!=father) { ans=min(ans,dis[num][x]); } return ans; } int main() { while(1) { scanf("%d%d",&n,&m); if(n==0&&m==0)break; for(int i=0;i<n;i++) { vc[i].clear(); p[i]=i; for(int j=0;j<n;j++) { flag[i][j]=0; dis[i][j]=dp[i][j]=inf; } } for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); edge[i].l=u; edge[i].r=v; edge[i].len=w; dis[u][v]=w; dis[v][u]=w; } sort(edge,edge+m,cmp); double mans=0; for(int i=0;i<m;i++) { if(findset(edge[i].l)!=findset(edge[i].r)) { same(edge[i].l,edge[i].r); flag[edge[i].l][edge[i].r]=1; flag[edge[i].r][edge[i].l]=1; mans+=(double)edge[i].len; vc[edge[i].l].push_back(edge[i].r); vc[edge[i].r].push_back(edge[i].l); } } for(int i=0;i<n;i++) { dfs(i,i,-1); } int q; scanf("%d",&q); double sum=0; for(int i=0;i<q;i++) { scanf("%d%d%d",&u,&v,&w); if(!flag[u][v])sum+=mans; else { sum+=mans-dis[u][v]*1.0+min(dp[u][v],w)*1.0; } } printf("%.4lf\n",sum/(q*1.0)); } return 0; }
相关文章推荐
- 设计模式之Builder模式
- 关于UICollectionView的headerView的问题
- Java continue 、break、标签
- add VNC console to KVM guests
- 奇异值分解(Singular Value Decomposition)
- ART 的 interpret-only模式源码及调用流程 & QuickCompiler后端调用流程
- 【6】mysql数据查询语言DQL(Data Query Language)①
- ios UITapGestureRecognizer 单指单击、单指多击、多指单击、多指多击事件操作
- LeetCode *** 300. Longest Increasing Subsequence
- iOS限定UITextField的输入格式
- (4)Java设计模式-建造者模式(Builder)
- 说说 UINavigationBar 中的 translucent 属性
- hdu5412 CRB and Queries (整体二分)
- IOS UIPanGestureRecognizer 移动图片
- STL常用容器总结——deque双向队列
- Sequence one
- Sequence one
- Rescue The Princess
- 基于 SoapUI 工具测试 Rest 服务
- 设计模式之建造者模式(Builder)