There is No Alternative~最小生成树变形
Description
ICPC (Isles of Coral Park City) consist of several beautiful islands.
The citizens requested construction of bridges between islands to resolve inconveniences of using boats between islands, and they demand that all the islands should be reachable from any other islands via one or more bridges.
The city mayor selected a number of pairs of islands, and ordered a building company to estimate the costs to build bridges between the pairs. With this estimate, the mayor has to decide the set of bridges to build, minimizing the total construction cost.
However, it is difficult for him to select the most cost-efficient set of bridges among those connecting all the islands. For example, three sets of bridges connect all the islands for the Sample Input 1. The bridges in each set are expressed by bold edges in Figure F.1.
Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1
As the first step, he decided to build only those bridges which are contained in all the sets of bridges to connect all the islands and minimize the cost. We refer to such bridges as no alternative bridges. In Figure F.2, no alternative bridges are drawn as thick edges for the Sample Input 1, 2 and 3.
Write a program that advises the mayor which bridges are no alternative bridges for the given input.
Input
The input consists of several tests case.
Figure F.2. No alternative bridges for Sample Input 1, 2 and 3
For each test, the first line contains two positive integers N and M . N represents the number of islands and each island is identified by an integer 1 through N. M represents the number of the pairs of islands between which a bridge may be built.
Each line of the next M lines contains three integers Si, Di and Ci (1 ≤ i ≤ M) which represent that it will cost Ci to build the bridge between islands Si and Di. You may assume 3 ≤ N ≤ 500, N − 1 ≤ M ≤ min(50000, N(N − 1)/2), 1 ≤ Si < Di ≤ N, and 1 ≤ Ci ≤ 10000. No two bridges connect the same pair of two islands, that is, if i ≠ j and Si = Sj , then Di ≠ Dj. If all the candidate bridges are built, all the islands are reachable from any other islands via one or more bridges.
Output
Output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.
Sample Input
4 4
1 2 3
1 3 3
2 3 3
2 4 3 4 4
1 2 3
1 3 5
2 3 3
2 4 3 4 4
1 2 3
1 3 1
2 3 3
2 4 3 3 3
1 2 1
2 3 1
1 3 1
Sample Output
1 3
3 9
2 4
0 0 可以组成多种最小生成树,求他们的公共边,和权值和;
这个n ,可以直接暴力枚举;
暴力出奇迹
暴力枚举一下就好了;
先求出一个最小生成树,记录边;
依次删边,看新的最小生成树的权值是否相等
不相等则证明,必须有的边,
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
using namespace std;
const int maxn = 5e4 + ;
const int INF = 1e9 + ;
int fa[], vis[maxn];
struct node {
int u, v, w;
} qu[maxn];
int cmp(node a, node b) {
return a.w < b.w;
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int combine(int x, int y) {
int nx = find(x);
int ny = find(y);
if(nx != ny) {
fa[nx] = ny;
return ;
}
return ;
}
int kruskal(int num, int flag, int x) {
int sum = , k = ;
for(int i = ; i < num; i++) {
if(x == i) continue;
if(combine(qu[i].u, qu[i].v)) {
sum += qu[i].w;
if(flag) vis[k++] = i;
}
}
return sum;
}
int main() {
// freopen("DATA.txt", "r", stdin);
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
for (int i = ; i < m ; i++) {
scanf("%d%d%d", &qu[i].v, &qu[i].u, &qu[i].w);
}
sort(qu, qu + m, cmp);
int temp = kruskal(m, , -);
int ans1 = , ans2 = ;
for (int i = ; i <= n ; i++) fa[i] = i;
for (int i = ; i < n - ; i++ ) {
for (int j = ; j <= n ; j++) fa[j] = j;
int sum = kruskal(m, , vis[i]);
if (sum != temp) {
ans1++;
ans2 += qu[vis[i]].w;
}
}
printf("%d %d\n", ans1, ans2 );
}
return ;
}
There is No Alternative~最小生成树变形的更多相关文章
- bzoj 2753 最小生成树变形
我们根据高度建图,将无向边转化为有向边 首先对于第一问,直接一个bfs搞定,得到ans1 然后第二问,我们就相当于要求找到一颗最小生成树, 满足相对来说深度小的高度大,也就是要以高度为优先级 假设现在 ...
- hdu 4081 最小生成树变形
/*关于最小生成树的等效边,就是讲两个相同的集合连接在一起 先建立一个任意最小生成树,这条边分开的两个子树的节点最大的一个和为A,sum为最小生成树的权值和,B为sum-当前边的权值 不断枚举最小生成 ...
- POJ1789&ZOJ2158--Truck History【最小生成树变形】
链接:http://poj.org/problem?id=1789 题意:卡车公司有悠久的历史,它的每一种卡车都有一个唯一的字符串来表示,长度为7,它的全部卡车(除了第一辆)都是由曾经的卡车派生出来的 ...
- poj 2253 Frogger【最小生成树变形】【kruskal】
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30427 Accepted: 9806 Descript ...
- UVa 1395 - Slim Span(最小生成树变形)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- HDU 4786 最小生成树变形 kruscal(13成都区域赛F)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- poj2377Bad Cowtractors (最小生成树变形之——最大生成树)
题目链接:http://poj.org/problem?id=2377 Description Bessie has been hired to build a cheap internet netw ...
- UESTC 918 WHITE ALBUM --生成树变形
最小生成树变形. 题目已经说得很清楚,要求到达每个房间,只需求一个最小生成树,这时边权和一定是最小的,并且那k个房间一定与所有点都有通路,即一定都可以逃脱. 但是有可能当所有点都有了该去的安全房间以后 ...
- pta7-20 畅通工程之局部最小花费问题(Kruskal算法)
题目链接:https://pintia.cn/problem-sets/15/problems/897 题意:给出n个城镇,然后给出n×(n-1)/2条边,即每两个城镇之间的边,包含起始点,终点,修建 ...
随机推荐
- LOV里的值直接引用系统里定义的值集的值,且具有值集的安全性控制
fnd_flex_server.check_value_security(p_security_check_mode => 'YH', p_flex_value_set_id => p_f ...
- EBS DBA指南笔记(二)
第三章 监控和诊断 本章涵盖以下几个主题:监测的方法,数据库的监测,apache的监测,forms的监测,并发管理器的监测,服务器的监测,网络的监测,其它的一些监测和诊断方法. 1.监测的方法:主 ...
- Coco2dx制作一个3D旋转的效果
建了工程之后修改HelloWorldScene.cpp文件,修改部分为 // on "init" you need to initialize your instance bool ...
- Hive操作语句实例讲解(帮助你了解 桶 bucket)
http://blog.sina.com.cn/s/blog_66474b16010182yu.html这篇可以较好地理解什么是外部表external #创建表人信息表 person(String ...
- 文件I/O实践(2) --文件stat
功能:获取文件元数据 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int st ...
- 结构体:探析C#文件方式读写结构体
最近直在研究Net Micro Framework字体文件(tinyfnt)由于tinyfnt文件头部有段描述数据所以很想 定义个结构体像VC样直接从文件中读出来省得用流个个解析很是麻烦 没有想到在中 ...
- H5学习之旅-H5与Php交互(12)
1.首先介绍PHP开发环境的搭建 ,在Google搜apachefriends,会有xampp的下载链接,这个工具集成了apache的很多服务 2.搭建php的编辑环境,选取eclipse安装php插 ...
- Android进阶(二十一)创建Android虚拟机
创建Android虚拟机
- 数据cube的schema与sql的对应的关系
用schema workbench 设置cube的维度结构 saiku 使用的cube,会将不同维度的查询转化为sql语句. schema中, cube的事实表和dimension表进行自然连接,具体 ...
- SpriteBuilder中物理对象能否被缩放
我前面早些时候提到物理形状不能被缩放. 现在我却说可以缩放它们,这是为啥呢? 好吧,拥有物理物体节点的缩放属性真心不能被动画化或改变在运行的时候; 但是你可以在SpriteBuilder中设置启用物理 ...