CSUOJ 1541 There is No Alternative
There is No Alternative
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算求最小生成树的唯一性,进行判断生成树上的某条边是否唯一
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步