博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSUOJ 1541 There is No Alternative
阅读量:4950 次
发布时间:2019-06-11

本文共 4921 字,大约阅读时间需要 16 分钟。

There is No Alternative

Time Limit: 3000ms
Memory Limit: 262144KB
This problem will be judged on 
Aizu. Original ID: 
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算求最小生成树的唯一性,进行判断生成树上的某条边是否唯一

1 #include 
2 using namespace std; 3 const int maxn = 500000; 4 struct arc { 5 int u,v,w; 6 bool used,mark,del; 7 arc(int x = 0,int y = 0,int z = 0) { 8 u = x; 9 v = y;10 w = z;11 mark = del = used = false;12 }13 bool operator<(const arc &tmp) const {14 return w < tmp.w;15 }16 } e[maxn];17 int uf[maxn],n,m,cnt;18 bool nui[maxn];19 int Find(int x) {20 return uf[x] = uf[x] == x?uf[x]:Find(uf[x]);21 }22 int kruskal(bool &Unique,bool first) {23 int ans = cnt = 0;24 for(int i = 0; i <= n; ++i) uf[i] = i;25 for(int i = 0; i < m; ++i) {26 if(!first && e[i].del) continue;27 int fx = Find(e[i].u);28 int fy = Find(e[i].v);29 if(fx == fy) continue;30 uf[fx] = fy;31 ans += e[i].w;32 if(first) {33 e[i].used = true;34 if(e[i].mark) Unique = false;35 }36 if(++cnt == n-1) return ans;37 }38 return ans;39 }40 int main() {41 int u,v,w;42 while(~scanf("%d %d",&n,&m)) {43 for(int i = 0; i < m; ++i) {44 scanf("%d %d %d",&u,&v,&w);45 e[i] = arc(u,v,w);46 }47 memset(nui,false,sizeof(nui));48 sort(e,e+m);49 for(int i = 1; i < m; ++i)50 if(e[i].w == e[i-1].w) e[i].mark = e[i-1].mark = true;51 bool Unique = true;52 int MST = kruskal(Unique,true);53 if(Unique) printf("%d %d\n",n-1,MST);54 else {55 int mst = 0,ne = 0;56 for(int i = 0; i < m; ++i) {57 if(e[i].used && e[i].mark) {58 e[i].del = true;59 int tmp = kruskal(Unique,false);60 if(tmp == MST && cnt == n-1) nui[i] = true;61 e[i].del = false;62 }63 if(e[i].used && !nui[i]) {64 ne++;65 mst += e[i].w;66 }67 }68 printf("%d %d\n",ne,mst);69 }70 }71 return 0;72 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4358265.html

你可能感兴趣的文章
用Entity Framework 来创建MySql数据库和表结构
查看>>
TensorFlow随机值:tf.random_normal函数
查看>>
poj 1733 Parity game(种类并查集)
查看>>
SQL Server2008函数
查看>>
课堂随笔3月8日下午
查看>>
ORM之F查询和Q查询
查看>>
BIOS编程相关常用外设介绍
查看>>
springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源
查看>>
NO 3 ,人生苦短,我学python之python 元祖tuple魔法
查看>>
Spring-Boot Banner
查看>>
876-链表的中间结点
查看>>
BZOJ 3781 莫队
查看>>
BZOJ 3674/BZOJ 3673 主席树
查看>>
JAVA的String类
查看>>
wtforms 简单使用
查看>>
flume介绍
查看>>
eclipse优化总结
查看>>
java异常处理
查看>>
【操作系统】主存空间的分配和回收
查看>>
JZOJ 4.1 B组 俄罗斯方块
查看>>