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条边,即每两个城镇之间的边,包含起始点,终点,修建 ...
随机推荐
- 批量替换数据库中所有用户数据表中字段数据类型为char和varchar到nvarchar的脚本
解决问题:字段类型为char的总是占用指定字节长度(末尾好多空白符号),varchar数据类型长度一个汉字占2个字节,内容存储为中文的字段个人建议全部使用nvarchar. 操作说明:打开SQL Se ...
- UITableViewBase UI_09
1.UITableView API文档总结: 1.UITableView的父类时,UIScrollView,所以它是可以滚动的,但是只能在竖直方向滚动. 2.UITableView是iOS中 ...
- 检查一个二叉树是否平衡的算法分析与C++实现
今天面试一个实习生,就想既然是未出校园,那就出一个比较基础的题吧,没想到答的并不如人意,对于树的操作完全不熟悉,因此此题算是未作答.原来我想看一下他分析问题的思路,优化代码的能力.接下来会把最近半年我 ...
- Touch Handling in Cocos2D 3.x(七)
在touchMoved方法中寻找触摸在父节点(CCScene)中的位置并且移动CCDragSprite到其父节点的相应位置中去. 在我们在动作中观赏拖放机制之前,我们需要使用这个新实现的类.打开Mai ...
- 【Django】优化小技巧之清除过期session
事情是这样的,大概也就几万注册用户的站点(使用django1.6), session 存储在关系型数据库,这次上线之后发现session表几十万数据了,过期session没有被自动删除 思考 官网 s ...
- Asp.net中JQuery、ajax调用后台方法总结
通过上一篇文章实例的实现,整个过程当中学习到很多知识点,了解了Jquery.Ajax在asp.net中的运用,加以总结,其实原理都是一样的,理解了一种,其他的注意很少的区别就可以了.灵活运用: 1.有 ...
- Dynamics CRM2013 Server2012下部署ADFS和IFD遇到的问题No Organization were retrived
最近一直在折腾Windows Server2012下的IFD部署,其中各种纠结啊错误百出,要想顺利的一步到位只能说看你的RP怎么样了,具体的操作过程推荐看下勇哥的博客:http://luoyong02 ...
- 放yy直播点赞动画
最近在做直播相关的东西,这个动画是IOS先撸出来的,后来android这边要模仿,大部分直播应用都有很炫酷的点赞动画,所以也没什么好稀奇的.如果有现成的轮子了,就没必要自己再造了,后来参照了程序亦非猿 ...
- Using Integrated SOA Gateway in Oracle EBS
FROM:http://blog.csdn.net/pan_tian/article/details/10159935 Oracle EBS如何与第三方系统相集成?比如这样的需求,X系统知道物料编码, ...
- DiskLruCache硬盘缓存技术详解
上次讲了使用内存缓存LruCache去加载很多图片而不造成OOM,而这种缓存的特点是在应用程序运行时管理内存中的资源(图片)的存储和释放,如果LruCache中有一张图片被释放了,再次加载该图片时需要 ...