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. Java transientkeyword使用小记

    1. transient的作用及用法 我们都知道一个对象仅仅要实现了Serilizable接口,这个对象就能够被序列化,java的这样的序列化模式为开发人员提供了非常多便利.我们能够不必关系详细序列化 ...

  2. less11 属性合并

    less //+ 合并以后,以逗号分割属性值 .mixin() { box-shadow+: inset 0 0 10px #555 ; } .myclass { .mixin(); box-shad ...

  3. Keyboard input

    Keyboard input Python provides a build-in function called raw_input (in version 2.x) that gets input ...

  4. 17.I/O系统访问方式和类型

    I/O方式 轮询  中断  DMA  通道

  5. DC、CDC及CDC的各个子类

      设备描述表是一个包含设备信息的结构体(物理设备如显示器.打印机),MFC中关于图像操作都需要DC来完成.HDC是Windows的一种数据类型,是设备描述句柄:CDC是MFC封装的Windows 设 ...

  6. 【基础篇】Android中获取Drawable的方法

    public static Drawable getDrawable(Context context,String filename) { BitmapDrawable drawable=null; ...

  7. Kinect 人机交互开发实践

    Kinect for Windows SDK 骨骼追踪 —— 对在Kinect视野范围内移动的一个或两个人进行骨骼追踪,可追踪到人体的20个节点 深度摄像头 —— 通过深度传感器获取到视野内的环境三维 ...

  8. 洛谷 P2147 [SDOI2008]洞穴勘测 LCT

    Code: #include <cstdio> #include <algorithm> #include <string> #include <cstrin ...

  9. [JSOI2018]潜入行动 树形DP_复杂计数

    code #include <cstdio> #include <algorithm> #include <cstring> #include <string ...

  10. js字符串排序方法

    前端开发过程中有时需自己手写排序方法 一般想到数字的字符串排序方法 我们会用到 var newArr = arr. sort(function(a,b){return a - b})来进行排序 但除此 ...