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 [问题描述] 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一 ...
随机推荐
- Spring JdbcTemplate之使用详解
最近在项目中使用到了 Spring 的 JdbcTemplate, 中间遇到了好多坑, 所以花一些时间对 JdbcTemplate 的使用做了一个总结, 方便以后自己的查看.文章中贴出来的API都是经 ...
- RocketMQ中Broker的启动源码分析(一)
在RocketMQ中,使用BrokerStartup作为启动类,相较于NameServer的启动,Broker作为RocketMQ的核心可复杂得多 [RocketMQ中NameServer的启动源码分 ...
- 使用sublime调试node.js
安装node相关 从node官网下载node的安装文件,我下的版本是node-v0.10.22-x64.exe,安装完node,node相关工具应该都加都环境变量path中了. 命令行下安装node- ...
- 史上最全面的SignalR系列教程-3、SignalR 实现推送功能-集线器类实现方式
1.概述 通过前两篇 史上最全面的SignalR系列教程-1.认识SignalR 史上最全面的SignalR系列教程-2.SignalR 实现推送功能-永久连接类实现方式 文章对SignalR的介绍, ...
- c# http Post Get 方法
/// <summary> /// get方式访问webapi /// </summary> /// <param name="url">< ...
- Multiple dex files define Lokhttp3/internal/wsWebSocketProtocol
Multiple dex files define Lokhttp3/internal/wsWebSocketProtocol 老套路,先晒图 图一:如题,在编译打包时遇到了如上错误,很明显这是一个依 ...
- 【模板】珂朵莉树(ODT)(Codeforces 896C Willem, Chtholly and Seniorious)
题意简述 维护一个数列,支持区间加,区间赋值,区间求第k小,区间求幂和 数据随机 题解思路 ODT是一种基于std::set的暴力数据结构. 每个节点对应一段区间,该区间内的数都相等. 核心操作spl ...
- 使用flash2print 代替 printflash 将office文档 转为flash 在页面中播放
前一些日子公司需求把用户上传的一些word等 文档 能像百度文库那样 显示给用户, 但是如果是直接显示office文档的话就需要 些控件的支持 .非常的不友好,所以 一开始我就想能不能转成pdf 来 ...
- Vue的冒泡事件
由于业务需求需要,需要在一个元素中的子元素添加一个点击事件. 但是刚好父元素也有一个点击事件.这个时候我们就需要使用到Vue中的阻止事件冒泡了.
- Node.js中的自定义模块化
打造步骤: 1. 创建模块 [ Function / Object / String 2. 导出模块 - module.exports = 模块名称 导出一个 - module.exports = ...