There is No Alternative CSU - 2097 最小生成树
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 题意是建桥,然后求最小建桥方案中哪些桥是必须要留着的,求这些桥的个数和总花费
先求出最小生成树,然后再去掉一条条边,看哪些边去掉后结果和最小生成树的结果不一样,那么这些边就是要留着的
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll mod = 1e9 + ;
const ll maxn = 1e5 + ;
int n, m, num, cnt, result, pre[maxn], b[maxn], vis[maxn];
struct node {
int x, y, z;
};
node edge[maxn];
bool cmp( node p, node q ) {
return p.z < q.z;
}
void init() {
for( int i = ; i <= n; i ++ ) {
pre[i] = i;
}
}
int find( int x ) {
int r = x;
while( r != pre[r] ) {
r = pre[r];
}
int i = x, j;
while( pre[i] != r ) {
j = pre[i];
pre[i] = r;
i = j;
}
return r;
}
void join( int x, int y ) {
int fx = find(x), fy = find(y);
if( fx != fy ) {
pre[fx] = fy;
}
}
int kruskal( int flag ) {
int sum = ;
for( int i = ; i < m; i ++ ) {
if( vis[i] ) {
continue;
}
int fx = find( edge[i].x );
int fy = find( edge[i].y );
if( fx != fy ) {
sum += edge[i].z;
pre[fx] = fy;
if( !flag ) {
b[cnt++] = i;
}
}
}
return sum;
}
int main() {
std::ios::sync_with_stdio(false);
while( cin >> n >> m ) {
memset( vis, , sizeof(vis) );
for( int i = ; i < m; i ++ ) {
cin >> edge[i].x >> edge[i].y >> edge[i].z;
}
sort( edge, edge + m, cmp );
cnt = , num = , result = ;
init();
int ans = kruskal();
for( int i = ; i < cnt; i ++ ) {
init();
vis[b[i]] = ;
if( kruskal() != ans ) {
result += edge[b[i]].z;
num ++;
}
vis[b[i]] = ;
}
cout << num << " " << result << endl;
}
return ;
}
There is No Alternative CSU - 2097 最小生成树的更多相关文章
- CSU 1541 There is No Alternative (最小生成树+枚举)
题目链接:传送门 题意: 有n个点.m条边.要使n个点所有连起来且要花费最小.问有哪些边是必需要连的. 分析: 要使花费最小肯定是做最小生成树.可是题目要求哪些边是必需要用的.我们能够 这样思考,我们 ...
- CSU 1116 Kingdoms(枚举最小生成树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...
- There is No Alternative~最小生成树变形
Description ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens reque ...
- Codeforces Gym 100803F There is No Alternative 暴力Kruskal
There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles o ...
- 关于ACM,关于CSU
原文地址:http://tieba.baidu.com/p/2432943599 前言: 即将进入研二,ACM的事情也渐渐远去,记忆终将模糊,但那段奋斗永远让人热血沸腾.开个贴讲讲ACM与中南的故事, ...
- CSUOJ 1541 There is No Alternative
There is No Alternative Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Aiz ...
- 最小生成树(Kruskal算法-边集数组)
以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...
- 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)
坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...
- 最小生成树计数 bzoj 1016
最小生成树计数 (1s 128M) award [问题描述] 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一 ...
随机推荐
- oracle的自增序列
因为oracle中的自增序列与mysql数据库是不一样的,所以在这里唠嗑一下oracle的自增序列 1. 创建和修改自增序列 --创建序列的语法 -- create sequence [user.]s ...
- @Value注解 和 @Data注解
@Value注解 service层代码 @Service public class HelloServiceImpl implements HelloService { @Autowired priv ...
- 【POJ - 2385】Apple Catching(动态规划)
Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...
- rpm包制作介绍
RPM(Rpm Package Management)在ReadHat等发行版下被用作软件包管理程序,其将某个软件相关的文件置入一个.rpm包中,用rpm命令,我们可以方便地完成Linux下软件安装. ...
- Consul的反熵
熵 熵是衡量某个体系中事物混乱程度的一个指标,是从热力学第二定律借鉴过来的. 熵增原理 孤立系统的熵永不自动减少,熵在可逆过程中不变,在不可逆过程中增加.熵增加原理是热力学第二定律的又一种表述,它更为 ...
- Leader-Follower线程模型简介
参考58沈剑大神架构师之路上的文章,谈谈Leader-Follower线程模型: 上图就是L/F多线程模型的状态变迁点,共6个关键点: (1)线程有3种状态:领导leading,处理processin ...
- vs 中本地 git 的基本使用
用 svn 有个毛病就是只有在改好了之后,才能提交.当周期比较长的时候,连自己都不知道自己改了什么东西,或者意外断电的时候,vs 中已保持的项目都有可能被 vs 去掉. 这个时候,使用 git 创建 ...
- 行车记+翻车记:.NET Core 新车改造,C# 节能降耗,docker swarm 重回赛道
非常抱歉,10:00~10:30 左右博客站点出现故障,给您带来麻烦了,请您谅解. 故障原因与博文中谈到的部署变更有关,但背后的问题变得非常复杂,复杂到我们都在怀疑与阿里云服务器 CPU 特性有关. ...
- springcloud(九):熔断器Hystrix和Feign的全套应用案例(二)
一.. 创建Eureka-Server 服务中心项目 1. 创建Eureka-Server 服务中心项目架构如下 2. pom.xml <dependencies> <depende ...
- RANSAC简史
前言 在进行泡泡机器人[图灵智库]栏目的翻译的过程中,我发现在2018-2019的顶会中,依然有很多文章(我看到的不少于6篇)对RANSAC进行各种改进,这令我感到很吃惊.毕竟该方法在1981年就被提 ...