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. Invalid project description.

    1.错误描写叙述 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/ ...

  2. C++友元(Friend)简介

    相对Java而言,友元是C++中特有的一种元素,再加上<C++ Primer>也并没有太具体的样例,所以刚接触这个概念的时候懵了非常久,即是自己总结一下,也希望能帮到大家,以下来讲讲友元的 ...

  3. poj_3468,线段树成段更新

    参考自http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #include& ...

  4. m_Orchestrate learning system---二十五、复制类的时候最容易出现的错误是什么

    m_Orchestrate learning system---二十五.复制类的时候最容易出现的错误是什么 一.总结 一句话总结:命名空间错误导致Analyze类虽然继承了Base类,但是没有执行里面 ...

  5. TurtleWorld Exercises

    1. Write a function called square that takes a parameter named t, which is a turtle. It should use t ...

  6. java9新特性-14-多分辨率图像 API

    1.官方Feature 251: Multi-Resolution Images 263: HiDPI Graphics on Windows and Linux 2.产生背景 在Mac上,JDK已经 ...

  7. VS2013+PTVS,python编码问题

    1.调试,input('中文'),乱码2.调试,print('中文'),正常3.不调试,input('中文'),正常4.不调试,print('中文'),正常 页面编码方式已经加了"# -- ...

  8. dijkstra STL 堆优化

    Code: #include<iostream> #include<algorithm> #include<vector> #include<queue> ...

  9. oracle创建静态监听

    [oracle@localhost admin]$ pwd /u01/app/oracle/product/11.2.0/dbhome_1/network/admin [oracle@localhos ...

  10. Perl模块利用CPAN在线安装自动化

    需要解决2个问题: 1.  如何与CPAN交互:利用perl –MCPAN –e ‘install 模块’ 2.  如何安装指定的版本:作者/模块-版本.tar.gz How to install a ...