There is No Alternative

Time Limit: 3000ms
Memory Limit: 262144KB

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的更多相关文章

  1. CSU 1541 There is No Alternative (最小生成树+枚举)

    题目链接:传送门 题意: 有n个点.m条边.要使n个点所有连起来且要花费最小.问有哪些边是必需要连的. 分析: 要使花费最小肯定是做最小生成树.可是题目要求哪些边是必需要用的.我们能够 这样思考,我们 ...

  2. 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)

    坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...

  3. JAVA CDI 学习(4) - @Alternative/@Default/@Any & Extension

    前面几节学习到的CDI内容,基本上都是hard-code,以硬编码的方式在代码里指定注入类型,这并非依赖注入的本意,依赖注入的优势之一在于“解耦”,这一节我们将学习如何利用配置来动态注入的类型及属性初 ...

  4. hdu 1541 Stars

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 思路:要求求出不同等级的星星的个数,开始怎么也想不到用树状数组,看完某些大神的博客之后才用树状数 ...

  5. CF# 334 Alternative Thinking

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. an alternative to symmetric multiprocessing

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION 17.5 CLUSTERSAn impor ...

  7. csuoj 1511: 残缺的棋盘

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec  内存限制: 128 MB 题目描述 输入 ...

  8. 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 ...

  9. 九度oj 1541 二叉树

    原题链接:http://ac.jobdu.com/problem.php?pid=1541 简答题如下: #include<algorithm> #include<iostream& ...

随机推荐

  1. 空暇时候思考之const

    对于多数人来说那些const的使用方法比方修饰返回值和修饰參数都应该是十分好理解的下来我要讨论 对于C语言中 #include <stdio.h> void main() { const ...

  2. ASIHTTPRequest 框架的导入

    刚接触ios 对一切都不熟悉  记录一下ASIHTTPRequest 框架的导入 步骤 以便日后再用 1.首先下载ASIHTTPRequest:点击下载 2.在project中导入下面文件: 导入方式 ...

  3. 一起talk C栗子吧(第九十五回:C语言实例--使用共享内存进行进程间通信一)

    各位看官们,大家好,上一回中咱们说的是SystemV IPC结构概述的样例,这一回咱们说的样例是:使用共享内存进行进程间通信. 闲话休提.言归正转.让我们一起talk C栗子吧! 共享内存是Syste ...

  4. Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD

    Build.VERSION.SDK_INT是系统的版本,Build.VERSION_CODES.GINGERBREAD是版本号. 到VERSION.SDK_INT不禁诧异,这是何物?! 看API的定义 ...

  5. 将一个文件夹纳入library或者移除remove

    https://support.microsoft.com/en-us/help/4026298/windows-show-libraries-in-file-explorer To show lib ...

  6. PHP+MySQL登录注册,完整版,详细注释

    纯手写打造. 下载地址:http://download.csdn.net/detail/qq_33599520/9779970 项目结构: 下面是代码: <!DOCTYPE html> & ...

  7. Cordova Android项目如何做代码混淆

    我想修改build.gradle配置 可是这个文件明确写了// GENERATED FILE! DO NOT EDIT!可是还是试了试: if (cdvReleaseSigningProperties ...

  8. OpenGL编程(三)让矩形动起来

    上次实现了在窗口中添加一个了一个矩形.这次的任务是在上次代码的基础上,让那个矩形动起来. 1.思路 要看到动态的效果,首先添加一个定时器,规定的时间刷新一次窗口:不断修改矩形的位置,使其在时间轴上达到 ...

  9. css 添加滚动条

    代码: <template> <div class="w" style="scrollbar-arrow-color:yellow;scrollbar- ...

  10. Bayes++ Library入门学习之熟悉UKF相关类

    UKF-SLAM是一种比较流行SLAM方案.相比EKF-SLAM,UKF利用unscented transform代替了EKF的线性化趋近,因而具有更高的精度.Bayes++库中的unsFlt.hpp ...