CSUOJ 1541 There is No Alternative
There is No Alternative
This problem will be judged on Aizu. Original ID: 1350
64-bit integer IO format: %lld Java class name: Main
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.
Figure F.2. No alternative bridges for 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 file contains several test cases, each of them has the following format.
N M
S1 D1 C1
ACM-ICPC Live Archive: 6837 – There is No Alternative 2 / 2
...
SM DM CM
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
For each test case, 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
解题:利用Kruskal算求最小生成树的唯一性,进行判断生成树上的某条边是否唯一
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int u,v,w;
bool used,mark,del;
arc(int x = ,int y = ,int z = ) {
u = x;
v = y;
w = z;
mark = del = used = false;
}
bool operator<(const arc &tmp) const {
return w < tmp.w;
}
} e[maxn];
int uf[maxn],n,m,cnt;
bool nui[maxn];
int Find(int x) {
return uf[x] = uf[x] == x?uf[x]:Find(uf[x]);
}
int kruskal(bool &Unique,bool first) {
int ans = cnt = ;
for(int i = ; i <= n; ++i) uf[i] = i;
for(int i = ; i < m; ++i) {
if(!first && e[i].del) continue;
int fx = Find(e[i].u);
int fy = Find(e[i].v);
if(fx == fy) continue;
uf[fx] = fy;
ans += e[i].w;
if(first) {
e[i].used = true;
if(e[i].mark) Unique = false;
}
if(++cnt == n-) return ans;
}
return ans;
}
int main() {
int u,v,w;
while(~scanf("%d %d",&n,&m)) {
for(int i = ; i < m; ++i) {
scanf("%d %d %d",&u,&v,&w);
e[i] = arc(u,v,w);
}
memset(nui,false,sizeof(nui));
sort(e,e+m);
for(int i = ; i < m; ++i)
if(e[i].w == e[i-].w) e[i].mark = e[i-].mark = true;
bool Unique = true;
int MST = kruskal(Unique,true);
if(Unique) printf("%d %d\n",n-,MST);
else {
int mst = ,ne = ;
for(int i = ; i < m; ++i) {
if(e[i].used && e[i].mark) {
e[i].del = true;
int tmp = kruskal(Unique,false);
if(tmp == MST && cnt == n-) nui[i] = true;
e[i].del = false;
}
if(e[i].used && !nui[i]) {
ne++;
mst += e[i].w;
}
}
printf("%d %d\n",ne,mst);
}
}
return ;
}
CSUOJ 1541 There is No Alternative的更多相关文章
- CSU 1541 There is No Alternative (最小生成树+枚举)
题目链接:传送门 题意: 有n个点.m条边.要使n个点所有连起来且要花费最小.问有哪些边是必需要连的. 分析: 要使花费最小肯定是做最小生成树.可是题目要求哪些边是必需要用的.我们能够 这样思考,我们 ...
- 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)
坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...
- JAVA CDI 学习(4) - @Alternative/@Default/@Any & Extension
前面几节学习到的CDI内容,基本上都是hard-code,以硬编码的方式在代码里指定注入类型,这并非依赖注入的本意,依赖注入的优势之一在于“解耦”,这一节我们将学习如何利用配置来动态注入的类型及属性初 ...
- hdu 1541 Stars
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 思路:要求求出不同等级的星星的个数,开始怎么也想不到用树状数组,看完某些大神的博客之后才用树状数 ...
- CF# 334 Alternative Thinking
A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- an alternative to symmetric multiprocessing
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION 17.5 CLUSTERSAn impor ...
- csuoj 1511: 残缺的棋盘
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec 内存限制: 128 MB 题目描述 输入 ...
- Why one-norm is an agreeable alternative for zero-norm?
[转载请注明出处]http://www.cnblogs.com/mashiqi Today I try to give a brief inspection on why we always choo ...
- 九度oj 1541 二叉树
原题链接:http://ac.jobdu.com/problem.php?pid=1541 简答题如下: #include<algorithm> #include<iostream& ...
随机推荐
- Android面试题目整理与解说(一)
这一篇文章专门整理一下研究过的Android面试题,内容会随着学习不断的添加,假设答案有错误,希望大家能够指正 1.简述Activity的生命周期 当Activity開始启动的时候,首先调用onCre ...
- iOS RegexKitLite 提取匹配的内容
使用RegexKitLite正则表达式需要以下工作: 1.RegexKitLite官方网址(内含使用教程):http://regexkit.sourceforge.net/RegexK ...
- RecyclerView借助ItemTouchHelper实现拖动和滑动删除功能
RecyclerView是官方推荐代替ListView的空间,怎样实现RecyclerView列表元素的拖动呢? 官方提供了ItemTouchHelper类使用过程例如以下: 定义ItemTouchH ...
- [PHP]怎样在SAE的CodeIgniter项目中隐藏掉index.php
第一步:改动项目根文件夹的config.yaml文件.加入例如以下内容: handle: - rewrite: if(!is_dir() && !is_file() && ...
- 【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】
[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with ...
- 智课雅思短语---五、 in contrast / on the contrary
智课雅思短语---五. in contrast / on the contrary 一.总结 一句话总结:相反 in contrast / on the contrary. 1.replace/ su ...
- FZOJ--2221-- RunningMan(水题)
Problem 2221 RunningMan Accept: 4 Submit: 10 Time Limit: 1000 mSec Memory Limit : 32768 KB Pro ...
- Windows10+VS2013+caffe+Python2.7+CUDA8.0 部署配置
所需环境工具: 1. Windows 10 2. VS2013 3. Windows版本的caffe工具包,地址:https://github.com/Microsoft/caffe 4. Anaco ...
- 17.I/O系统访问方式和类型
I/O方式 轮询 中断 DMA 通道
- Hadoop 框架基础(四)
** Hadoop 框架基础(四) 上一节虽然大概了解了一下 mapreduce,徒手抓了海胆,不对,徒手写了 mapreduce 代码,也运行了出来.但是没有做更深入的理解和探讨. 那么…… 本节目 ...