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条边,即每两个城镇之间的边,包含起始点,终点,修建 ...
随机推荐
- Dynamics CRM2013 去除界面顶部黄色的CRM For Outlook条框
Dynamics CRM2013中每次打开系统页面上方都会有个黄条看着很是烦人,效果如下图 庆幸的是系统提供了关闭的开关,设置-管理-系统设置,把"设置CRM For Outlook消息是否 ...
- 谈谈Ext JS的组件——布局的使用方法续二
绝对布局(Ext.layout.container.Absolute) 绝对布局让我回想到了使用Foxpro开发的时候,哪时候的界面布局就是这样,通过设置控件的左上角坐标(x,y)和宽度来进行的,因为 ...
- (NO.00002)iOS游戏精灵战争雏形(十一)
为了在子弹触碰到目标时做一些事情,我们必须要设置碰撞回调. 首先在MainScene.h的类接口中添加碰撞协议: @interface MainScene : CCNode <CCPhysics ...
- 分布式进阶(十二)Docker固定Container IP
使用pipework工具. 前提:每个Container所做的工作现在还很少,可以不用save.commit. 为了便于通信,自定义一个网桥(192.168.1.180/24),使之IP与宿主主机IP ...
- Protobuf-java maven配置
Protobuf-java maven配置 1. maven pom片断 <!-- protobuf-java for maven plugin http://stackoverflow.com ...
- Mahout 算法
Mahout 包括协同过滤,基于User和Item的推荐:kmeans.Fuzzy-kmeans .Mean shift .Dirichlet process .LDA聚类:奇异值分解:并行频繁项集挖 ...
- python判断类型:想知道一个对象(实例或者变量)是什么类型,什么结构的
用type和isinstance 例子: ta={} ta['1']="a" ta={'2':"b"} ta['3']="c" #使用两个函 ...
- 69个Spring面试题
Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring ...
- Android For JNI(五)——C语言多级指针,结构体,联合体,枚举,自定义类型
Android For JNI(五)--C语言多级指针,结构体,联合体,枚举,自定义类型 我们的C已经渐渐的步入正轨了,基础过去之后,就是我们的NDK和JNI实战了 一.多级指针 指针的概念我们在前面 ...
- 【Android 应用开发】Android - 时间 日期相关组件
源码下载地址 : -- CSDN : http://download.csdn.net/detail/han1202012/6856737 -- GitHub : https://github.co ...