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 ;
}

最新文章

  1. 跌倒了,再爬起来:ASP.NET 5 Identity
  2. Leetcode: Sort Transformed Array
  3. prompt弹框返回输入的文本
  4. Horseman - 让你更轻松的使用 PhantomJS
  5. 【NOIP 2004】虫食算
  6. jquery-cookie插件怎么读写json数据
  7. postgresql数据库的yum安装方法
  8. JSON操作,转载
  9. A child container failed during start 解决方案
  10. HDU1181 变形课 (回溯法)
  11. html5 人物行走
  12. TFS体系结构和概念
  13. vultr机房vps价格20%优惠,赶紧来抢!
  14. BcdTool(系统启动菜单管理器) v1.0912 绿色版
  15. CAAnimation动画--(旋转/缩放/移动/闪烁)
  16. 最全DOS的CMD命令,程序员必会
  17. 半透明边框与background-clip
  18. Spring @AfterReturning 总是返回null
  19. Windows 7安装Tensorflow
  20. Java基础——Ajax(一)

热门文章

  1. 高速掌握Lua 5.3 —— I/O库 (1)
  2. iOS 报错:(子线程中更新UI)This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
  3. thinkphp5项目--个人博客(八)
  4. [Poj 1015] Jury Compromise 解题报告 (完全背包)
  5. 21.hash_map(已被废弃不再使用 被unordered_map代替)
  6. OLTP 与 OLAP
  7. CSS之基于不同场景的垂直居中解决方案
  8. CSS3动画闪跳
  9. 使用sysbench 对mysql进行性能测试
  10. bzoj1025 [SCOI2009]游戏 动态规划