栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3036 IP

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3036 IP

#include <cstdio>#include <map>#include <string>#include <algorithm>using namespace std;const int MAXN = 2010;const int MAXM = 50010;map<string, int> hash;int cnt;struct EE { int u, v, w;}ee[MAXM];int tot;bool cmp(const EE &cmp1, const EE &cmp2){ return cmp1.w < cmp2.w;}int fa[MAXN];int find(int x){ return fa[x] == x ? x : fa[x] = find(fa[x]);}int main(){ int cn, cns; char str1[110], str2[110]; int cost; string tmp1, tmp2; scanf("%d", &cns); for (cn = 0; cn < cns; cn++) { int n, m; scanf("%d%d", &n, &m); cnt = 0; tot = 0; hash.clear(); for (int i = 0; i < m; i++) { scanf("%s%s%d", str1, str2, &cost); tmp1 = str1; tmp2 = str2; if (hash.find(tmp1) == hash.end()) { hash[tmp1] = cnt++; } if (hash.find(tmp2) == hash.end()) { hash[tmp2] = cnt++; } ee[tot].u = hash[tmp1]; ee[tot].v = hash[tmp2]; ee[tot].w = cost; tot++; } sort(ee, ee + tot, cmp); for (int i = 0; i < cnt; i++) fa[i] = i; int fx, fy; int ans = 0; for (int i = 0; i < tot; i++) { fx = find(ee[i].u); fy = find(ee[i].v); if (fx != fy) { fa[fx] = fy; ans += ee[i].w; } } printf("%dn", ans); } return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/363924.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号