HDU 1233 还是畅通工程(最小生成树)
还是畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41447 Accepted Submission(s): 18920
Description
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最小的公路总长度。
Sample Input
3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0
Sample Output
3
5
prim()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int maxn = 105; const int INF = 0x3f3f3f3f; struct Edge{ int u,v,w,next; bool operator < ( const Edge &x) const { return w > x.w; } }edge[maxn*(maxn-1)]; int tot = 0,head[maxn],dis[maxn]; bool vis[maxn]; void init() { tot = 0; memset (head,-1, sizeof (head)); } void addedge( int u, int v, int w) { edge[tot] = (Edge){u,v,w,head[u] }; head[u] = tot++; } int prim() { int sum = 0; priority_queue<Edge>que; Edge p; memset (dis,INF, sizeof (dis)); memset (vis, false , sizeof (vis)); p.v = 1; que.push(p); dis[1] = 0; while (!que.empty()) { p = que.top(); que.pop(); int u = p.v; if (vis[u]) continue ; vis[u] = true ; sum += dis[u]; for ( int i = head[u];~i;i = edge[i].next) { int v = edge[i].v,w = edge[i].w; if (dis[v] > w) { dis[v] = edge[i].w; p.u = u,p.v = v,p.w = w; que.push(p); } } } return sum; } int main() { //freopen("input.txt","r",stdin); int N; while (~ scanf ( "%d" ,&N) && N) { int u,v,w; int len = N*(N - 1)/2; init(); for ( int i = 0;i < len;i++) { scanf ( "%d%d%d" ,&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } printf ( "%d\n" ,prim()); } return 0; } |
Kruskal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef __int64 LL; const int maxn = 105; struct Edge{ int u,v,next; LL w; }edge[maxn*(maxn-1)<<1]; int tot = 0,head[maxn],fa[maxn]; void init( int N) { tot = 0; memset (head,-1, sizeof (head)); for ( int i = 0;i <= N;i++) fa[i] = i; } bool cmp( struct Edge x, struct Edge y) { return x.w < y.w; } int find( int x) { int r = x; while (r != fa[r]) r = fa[r]; int i = x,j; while (i != r) { j = fa[i]; fa[i] = r; i = j; } return r; } int main() { //freopen("input.txt","r",stdin); int N; while (~ scanf ( "%d" ,&N) && N) { LL sum = 0; int len = N*(N-1)/2; init(N); for ( int i = 0;i < len;i++) { scanf ( "%d%d%I64d" ,&edge[i].u,&edge[i].v,&edge[i].w); } sort(edge,edge+len,cmp); for ( int i = 0;i < len;i++) { int fx = find(edge[i].u),fy = find(edge[i].v); if (fx != fy) { fa[fx] = fy; sum += edge[i].w; } } printf ( "%I64d\n" ,sum); } return 0; } |
┆ 凉 ┆ 暖 ┆ 降 ┆ 等 ┆ 幸 ┆ 我 ┆ 我 ┆ 里 ┆ 将 ┆ ┆ 可 ┆ 有 ┆ 谦 ┆ 戮 ┆ 那 ┆ ┆ 大 ┆ ┆ 始 ┆ 然 ┆
┆ 薄 ┆ 一 ┆ 临 ┆ 你 ┆ 的 ┆ 还 ┆ 没 ┆ ┆ 来 ┆ ┆ 是 ┆ 来 ┆ 逊 ┆ 没 ┆ 些 ┆ ┆ 雁 ┆ ┆ 终 ┆ 而 ┆
┆ ┆ 暖 ┆ ┆ 如 ┆ 地 ┆ 站 ┆ 有 ┆ ┆ 也 ┆ ┆ 我 ┆ ┆ 的 ┆ 有 ┆ 精 ┆ ┆ 也 ┆ ┆ 没 ┆ 你 ┆
┆ ┆ 这 ┆ ┆ 试 ┆ 方 ┆ 在 ┆ 逃 ┆ ┆ 会 ┆ ┆ 在 ┆ ┆ 清 ┆ 来 ┆ 准 ┆ ┆ 没 ┆ ┆ 有 ┆ 没 ┆
┆ ┆ 生 ┆ ┆ 探 ┆ ┆ 最 ┆ 避 ┆ ┆ 在 ┆ ┆ 这 ┆ ┆ 晨 ┆ ┆ 的 ┆ ┆ 有 ┆ ┆ 来 ┆ 有 ┆
┆ ┆ 之 ┆ ┆ 般 ┆ ┆ 不 ┆ ┆ ┆ 这 ┆ ┆ 里 ┆ ┆ 没 ┆ ┆ 杀 ┆ ┆ 来 ┆ ┆ ┆ 来 ┆
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)