专题四 · 1006
来源:互联网 发布:苏州激光切割编程招聘 编辑:程序博客网 时间:2024/05/29 03:15
代码及解释
#include <iostream>#include <algorithm>// 天真的把上一道题稍微改了改,// 以为能过,结果 WA 了 (》:o 」∠)_// 然后又老老实实的重新写了using std::cin;using std::cout;using std::endl;char set[1000];int n,k;struct edge { char s,e; int w;} e[1000];inline bool cmp(edge a,edge b) { return a.w<b.w;}char find_f(char x) { return set[x] == x ? x : set[x] = find_f(set[x]);}int Kruskal() { int sum = 0; for(int i = 0; i < k; ++i) { char x = find_f(e[i].s); char y=find_f(e[i].e); if(x != y) { set[x] = y; sum += e[i].w; } } return sum;}int main() { while(cin >> n && n) { char rs, re; int m, rw; k=0; n--; while(n--) { cin >> rs >> m; while(m--) { cin >> re >> rw; e[k].s = rs; e[k].e = re; e[k].w = rw; k++; } } for(char i = 'A'; i <= 'Z'; ++i) set[i] = i; std::sort(e, e + k, cmp); cout << Kruskal() << endl; } return 0;}
0 0
- 专题四 · 1006
- 专题四1006
- 专题四1006
- 1006-F专题四
- 专题四 · 1001
- 专题四 · 1002
- 专题四 · 1003
- 专题四 · 1004
- 专题四 · 1005
- 专题四 · 1008
- 专题四 · 1009
- 专题四 · 1010
- 专题四 · 1011
- 专题四——1006
- 杭电专题四1006
- 专题四-1006-典型Kruskal算法应用
- 2016sdau课程练习专题四 1006
- 3G专题四
- windows下安装JDK和Tomcat
- nodejs的module.exports 与 exports
- 剑指offer系列-T11_2数值的整数次方(时间复杂度为o(logn))
- 2、MySQL安装和配置
- oracle安装问题总结
- 专题四 · 1006
- leetcode 91-120记录
- 安装android studio
- 2016-7-2 杂感
- Linux netstat命令详解
- 关于类成员函数中访问同类对象的私有成员
- 前篇:自定义(扩展性能强!)的下拉刷新和上拉加载控件
- Throwing and Catching Exceptions
- xcode快速创建代码模版