hdu4126_hdu4756_求最小生成树的最佳替换边_Kruskal and Prim
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
Catalog
Problem:
原题目描述在最下面。
第一题:
给定一张n(3000)个点的图,q次询问,每次询问增大一条边的权值后最小生成树的值是多少,求答案期望。
第二题:
给定一张n(1000)个点的图,某两个点间的边可能不存在,问各种情况最小生成树的最大值。
Solution:
第一题用的\(kruskal\),第二题用的\(prim\)。貌似\(prim\)算法效率更高,毕竟都是稠密图。
先求MST,跑一边树形\(dp\),\(dp\)数组记录任意两个路径间不在MST上的最小边权值。
然后就枚举边求解咯。
\]
AC_Code:
hdu4126
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <ctime>
#include <cmath>
#include <iostream>
#define lson(x) cw[x].l
#define rson(x) cw[x].r
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MXN = 3e3 + 5;
const int MXE = 1e7 + 5;
const int INF = 0x3f3f3f3f;
int n, m, tot;
vector<int> son[MXN];
struct lp{
int u, v;
int w;
}edge[MXE];
int fa[MXN], rnk[MXN];
int dis1[MXN][MXN], is[MXN][MXN], ar[MXN][MXN];
void init() {
memset(dis1, 0x3f, sizeof(dis1));
memset(is, 0, sizeof(is));
memset(ar, 0x3f, sizeof(ar));
for(int i = 1; i <= n; ++i) {
fa[i] = i;
rnk[i] = 1;
son[i].clear();
ar[i][i] = dis1[i][i] = 0;
}
}
bool cmp(const lp &a, const lp &b) {
return a.w < b.w;
}
int Fi(int x) {
return fa[x] == x? x: fa[x] = Fi(fa[x]);
}
LL kruskal(int k) {
int CNT = 0;
LL ans = 0;
for(int i = 0; i < k; ++i) {
int x = Fi(edge[i].u), y = Fi(edge[i].v);
if(x == y) continue;
if(rnk[x] > rnk[y]) swap(x, y);
fa[x] = y;
rnk[y] += rnk[x];
++ CNT;
ans += edge[i].w;
son[edge[i].u].push_back(edge[i].v);
son[edge[i].v].push_back(edge[i].u);
is[edge[i].u][edge[i].v] = is[edge[i].v][edge[i].u] = 1;
if(CNT == n-1) break;
}
return ans;
}
int dfs(int u,int ba,int r) {
int len = son[u].size(), mi = INF;
for(int i = 0; i < len; ++i) {
int v = son[u][i];
if(v == ba) continue;
int tmp = dfs(v, u, r);
mi = min(mi, tmp);
dis1[u][v] = dis1[v][u] = min(dis1[v][u], tmp);
}
if(r != ba) mi = min(mi, ar[r][u]);
return mi;
}
void solve() {
init();
for(int i = 0; i < m; ++i) {
scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
edge[i].u ++; edge[i].v ++;
ar[edge[i].u][edge[i].v] = ar[edge[i].v][edge[i].u] = edge[i].w;
}
sort(edge, edge + m, cmp);
int ans = kruskal(m), q;
for(int i = 1; i <= n; ++i) dfs(i, -1, i);
LL sum = 0, tmp;
scanf("%d", &q);
for(int i = 0, u, v, w; i < q; ++i) {
scanf("%d%d%d", &u, &v, &w);
u++; v++;
if(is[u][v]) {
tmp = ans - ar[u][v] + min(dis1[u][v], w);
}else {
tmp = ans;
}
sum += tmp;
}
printf("%.4f\n", sum*1.0/q);
}
int main(int argc, char const *argv[]){
while(~scanf("%d%d", &n, &m)&&(n + m)) solve();
return 0;
}
hdu4756
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <ctime>
#include <cmath>
#include <iostream>
#define lson(x) cw[x].l
#define rson(x) cw[x].r
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MXN = 1e3 + 5;
const int MXE = 1e7 + 5;
const int INF = 0x3f3f3f3f;
int n;
double m;
vector<int> son[MXN];
int is[MXN][MXN];
double ar[MXN][MXN], dis1[MXN][MXN], dis[MXN];
int pre[MXN], vis[MXN];
LL x[MXN], y[MXN];
void init() {
memset(is, 0, sizeof(is));
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) ar[i][j] = dis1[i][j] = INF;
son[i].clear();
ar[i][i] = dis1[i][i] = 0;
}
}
double prim() {
double sum = 0;
for(int i = 1; i <= n; ++i) {
dis[i] = ar[1][i];
vis[i] = 1;
pre[i] = 1;
}
vis[1] = -1;
for(int J = 1; J < n; ++J) {
int p = -1;
double mm = INF;
for(int i = 1; i <= n; ++i) {
if(vis[i] != -1 && dis[i] < mm) {
p = i; mm = dis[i];
}
}
if(p == -1) break;
sum += mm;
son[p].push_back(vis[p]);
son[vis[p]].push_back(p);
is[p][vis[p]] = is[vis[p]][p] = 1;
vis[p] = -1;
for(int i = 1; i <= n; ++i) {
if(ar[p][i] < dis[i] && vis[i] != -1) {
dis[i] = ar[p][i];
vis[i] = p;
}
}
}
return sum;
}
double dfs(int u,int ba,int r) {
int len = son[u].size();
double mi = INF;
for(int i = 0; i < len; ++i) {
int v = son[u][i];
if(v == ba) continue;
double tmp = dfs(v, u, r);
mi = min(mi, tmp);
dis1[u][v] = dis1[v][u] = min(dis1[v][u], tmp);
}
if(r != ba) mi = min(mi, ar[r][u]);
return mi;
}
LL two(LL x) {
return x * x;
}
void solve() {
scanf("%d%lf", &n, &m);
init();
for(int i = 1; i <= n; ++i) {
scanf("%lld%lld", &x[i], &y[i]);
}
for(int i = 1; i < n; ++i) {
for(int j = i + 1; j <= n; ++j) {
ar[i][j] = ar[j][i] = sqrt(two(x[i]-x[j])*1.0+two(y[i]-y[j]));
}
}
double tmp = prim(), ans = tmp;
//printf("MST = %.2f\n", tmp);
for(int i = 1; i <= n; ++i) dfs(i, -1, i);
for(int i = 2; i < n; ++i) {
for(int j = i + 1; j <= n; ++j) {
if(is[i][j])ans = max(tmp - ar[i][j] + dis1[i][j], ans);
}
}
if(ans >= INF) printf("%.2f\n", tmp * m);
else printf("%.2f\n", ans * m);
}
int main(int argc, char const *argv[]){
int tim;scanf("%d", &tim);
while(tim--) solve();
return 0;
}
Problem Description:
一:
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.
二:
As we all know, Nanjing is one of the four hottest cities in China. Students in NJUST find it hard to fall asleep during hot summer every year. They will never, however, suffer from that hot this year, which makes them really excited. NJUST’s 60th birthday is approaching, in the meantime, 50 million is spent to install air conditioning among students dormitories. Due to NJUST’s long history, the old circuits are not capable to carry heavy load, so it is necessary to set new high-load wires. To reduce cost, every wire between two dormitory is considered a segment. Now, known about all the location of dormitories and a power plant, and the cost of high-load wire per meter, Tom200 wants to know in advance, under the premise of all dormitories being able to supply electricity, the minimum cost be spent on high-load wires. And this is the minimum strategy. But Tom200 is informed that there are so many wires between two specific dormitories that we cannot set a new high-load wire between these two, otherwise it may have potential risks. The problem is that Tom200 doesn’t know exactly which two dormitories until the setting process is started. So according to the minimum strategy described above, how much cost at most you'll spend?
Input
The first line of the input contains a single integer T(T ≤ 100), the number of test cases.
For each case, the first line contains two integers n(3 ≤ n ≤ 1000), k(1 ≤ k ≤ 100). n represents n-1 dormitories and one power plant, k represents the cost of high-load wire per meter. n lines followed contains two integers x, y(0 ≤ x, y ≤ 10000000), representing the location of dormitory or power plant. Assume no two locations are the same, and no three locations are on a straight line. The first one is always the location of the power plant.
Output
For each case, output the cost, correct to two decimal places.
Sample Input
2
4 2
0 0
1 1
2 0
3 1
4 3
0 0
1 1
1 0
0 1
Sample Output
9.66
9.00
hdu4126_hdu4756_求最小生成树的最佳替换边_Kruskal and Prim的更多相关文章
- 全连通图求最小生成树边权之积(邻接矩阵/prim/kruskal)
Description 大家都知道最小生成树一般求的是构成最小生成树的边的权值之和. 现在请求构成最小生成树的边的权值之积 S,最终结果请输出 (S % 100003). P.S. 点之间的边为无向边 ...
- Prim算法和Kruskal算法求最小生成树
Prim算法 连通分量是指图的一个子图,子图中任意两个顶点之间都是可达的.最小生成树是连通图的一个连通分量,且所有边的权值和最小. 最小生成树中,一个顶点最多与两个顶点邻接:若连通图有n个顶点,则最小 ...
- (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- HDU-1233 还是畅通工程 (prim 算法求最小生成树)
prim 算法求最小生成树 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- prime算法求最小生成树(畅通工程再续)
连着做了四道畅通工程的题,其实都是一个套路,转化为可以求最小生成树的形式求最小生成树即可 这道题需要注意: 1:因为满足路的长度在10到1000之间才能建路,所以不满足条件的路径长度可以初始化为无穷 ...
- Kruskal和Prim算法求最小生成树
Kruskal算法求最小生成树 测试数据: 5 6 0 1 5 0 2 3 1 2 4 2 4 2 2 3 1 1 4 1 输出: 2 3 1 1 4 1 2 4 2 0 2 3 思路:在保证不产生回 ...
- Kruskal求最小生成树
#include<bits/stdc++.h> using namespace std; ; ; const int inf = 0x3f3f3f3f; ; typedef long lo ...
- poj 2349 求最小生成树里面第m长的边
题目链接:https://vjudge.net/problem/POJ-2349 题意: 题目就是要我们找到一个最小的值D,把图里面所有大于D的边去掉之后剩余的连通分支的数量为S.这个就是找这个图里面 ...
- hdu 3405 删掉某点后 求最小生成树
给出N个点的坐标 边的权值为两点间的距离 删掉其中某点 求最小生成树的权值和 要求这权值最小 因为最多50个点 所以具体是删哪个点 用枚举假如有4个点 就要求4次最小生成树 分别是2 3 4 | 1 ...
随机推荐
- 【leetcode】328. Odd Even Linked List
题目如下: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...
- Docker 部署 nginx 前端项目
docker pull nginx docker run -itd -p 82:80 -v /opt/soft/nginx/conf/nginx.conf:/etc/nginx/nginx.conf ...
- 微信中location.reload失效
var len = window.location.href.indexOf("?"); if(len>0){ window.location.href=window ...
- 如何在Python中让两个print()函数的输出打印在一行内?
1.两个连续的print()函数为什么在输出时内容会分行显示? 解:print()中有两个默认参数sep和end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用‘,’代替空格,且默认末尾加 ...
- Openstack组件部署 — Networking service_Compute Node
目录 目录 前文列表 安装组件 配置通用组件 配置自服务网络选项 配置Linux 桥接代理 配置Nova使用网络 完成安装 验证操作Execute following commands on Cont ...
- 作用域 {}代码块 const修饰符 引用
简单分为:全局作用域.局部作用域.语句作用域 如果希望在局部变量的作用域内使用同名的全局变量,可以在该变量前加上“::” ::aver=20 #include<iostream> usin ...
- JUC源码分析-线程池篇(三)ScheduledThreadPoolExecutor
JUC源码分析-线程池篇(三)ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor 继承自 ThreadPoolExecutor.它主要用来在 ...
- python调用tushare获取股票日线实时行情数据
接口:daily 数据说明:交易日每天15点-16点之间.本接口是未复权行情,停牌期间不提供数据. 调取说明:基础积分每分钟内最多调取200次,每次4000条数据,相当于超过18年历史,具体请参阅本文 ...
- javascript中的insertBefore方法
<SCRIPT LANGUAGE="JavaScript"> window.onload=function(){ var a =document.createEleme ...
- spring的组成模块
spring的核心组件(骨骼架构)—— 共同创建了Bean关系网络 Core:主要定义了资源的访问方式 Context:给spring提供一个运行时的环境 Bean:Bean的定义,创建以及解析 ...