还是畅通工程 HDU1233(最小生成树)

2024-08-30 21:08

本文主要是介绍还是畅通工程 HDU1233(最小生成树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

还是畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29254 Accepted Submission(s): 13088


Problem 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

Hint
Hint

Huge input, scanf is recommended.



Source

浙大计算机研究生复试上机考试-2006年


prim 算法

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<cstdlib>
#include<set>
#include<stack>
#include<cstring>
using namespace std;
template<class T>inline T read(T&x)
{char c;while((c=getchar())<=32)if(c==EOF)return -1;bool ok=false;if(c=='-')ok=true,c=getchar();for(x=0; c>32; c=getchar())x=x*10+c-'0';if(ok)x=-x;return 1;
}
template<class T> inline T read_(T&x,T&y)
{return read(x)!=-1&&read(y)!=-1;
}
template<class T> inline T read__(T&x,T&y,T&z)
{return read(x)!=-1&&read(y)!=-1&&read(z)!=-1;
}
template<class T> inline void write(T x)
{if(x<0)putchar('-'),x=-x;if(x<10)putchar(x+'0');else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{write(x);putchar('\n');
}
//-------ZCC IO template------
const int maxn=101;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 10007int G[maxn][maxn];
int vis[maxn];
int mincost[maxn];
int V,E;
void prim()
{For(i,0,V+1){mincost[i]=inf;vis[i]=false;}mincost[1]=0;int ans=0;while(true){int v=-1;For(u,1,V+1)if(!vis[u]&&(v==-1||mincost[v]>mincost[u]))v=u;if(v==-1)break;vis[v]=true;ans+=mincost[v];For(u,1,V+1)mincost[u]=min(mincost[u],G[v][u]);}writeln(ans);
}
int main()
{//#ifndef ONLINE_JUDGE//freopen("in.txt","r",stdin);// #endif // ONLINE_JUDGEint n,m,i,j,k,t;while(read(V)&&V){E=V*(V-1)/2;while(E--){int a,b,c;read__(a,b,c);G[a][b]=G[b][a]=c;}prim();}return 0;
}
Kruskal算法

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<cstdlib>
#include<set>
#include<stack>
#include<cstring>
using namespace std;
template<class T>inline T read(T&x)
{char c;while((c=getchar())<=32)if(c==EOF)return -1;bool ok=false;if(c=='-')ok=true,c=getchar();for(x=0; c>32; c=getchar())x=x*10+c-'0';if(ok)x=-x;return 1;
}
template<class T> inline T read_(T&x,T&y)
{return read(x)!=-1&&read(y)!=-1;
}
template<class T> inline T read__(T&x,T&y,T&z)
{return read(x)!=-1&&read(y)!=-1&&read(z)!=-1;
}
template<class T> inline void write(T x)
{if(x<0)putchar('-'),x=-x;if(x<10)putchar(x+'0');else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{write(x);putchar('\n');
}
//-------ZCC IO template------
const int maxn=101;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 10007
struct edge
{int from,to,cost;edge(){}edge(int a,int b,int c){from=a,to=b,cost=c;}bool operator<(const edge&tmp)const{return cost<tmp.cost;}
}es[maxn*maxn];int f[maxn];
void init(int n)
{For(i,0,n+1)f[i]=i;
}
int find(int x)
{return x==f[x]?x:(f[x]=find(f[x]));
}
void unio(int x,int y)
{x=find(x);y=find(y);if(x==y)return ;f[x]=y;
}
bool same(int x,int y)
{return find(x)==find(y);
}
int main()
{//#ifndef ONLINE_JUDGE//freopen("in.txt","r",stdin);// #endif // ONLINE_JUDGEint n,m,i,j,k,t;int V,E;while(read(V)&&V){E=V*(V-1)/2;for(i=0;i<E;i++){int a,b,c;read__(a,b,c);es[i]=edge(a,b,c);}init(V);sort(es,es+E);int ans=0;for(i=0;i<E;i++){edge e=es[i];if(!same(e.to,e.from)){ans+=e.cost;unio(e.to,e.from);}}writeln(ans);}return 0;
}

这篇关于还是畅通工程 HDU1233(最小生成树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1121929

相关文章

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Python使用python-pptx自动化操作和生成PPT

《Python使用python-pptx自动化操作和生成PPT》这篇文章主要为大家详细介绍了如何使用python-pptx库实现PPT自动化,并提供实用的代码示例和应用场景,感兴趣的小伙伴可以跟随小编... 目录使用python-pptx操作PPT文档安装python-pptx基础概念创建新的PPT文档查看

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Python实现数据可视化图表生成(适合新手入门)

《Python实现数据可视化图表生成(适合新手入门)》在数据科学和数据分析的新时代,高效、直观的数据可视化工具显得尤为重要,下面:本文主要介绍Python实现数据可视化图表生成的相关资料,文中通过... 目录前言为什么需要数据可视化准备工作基本图表绘制折线图柱状图散点图使用Seaborn创建高级图表箱线图热

SQLServer中生成雪花ID(Snowflake ID)的实现方法

《SQLServer中生成雪花ID(SnowflakeID)的实现方法》:本文主要介绍在SQLServer中生成雪花ID(SnowflakeID)的实现方法,文中通过示例代码介绍的非常详细,... 目录前言认识雪花ID雪花ID的核心特点雪花ID的结构(64位)雪花ID的优势雪花ID的局限性雪花ID的应用场景

Django HTTPResponse响应体中返回openpyxl生成的文件过程

《DjangoHTTPResponse响应体中返回openpyxl生成的文件过程》Django返回文件流时需通过Content-Disposition头指定编码后的文件名,使用openpyxl的sa... 目录Django返回文件流时使用指定文件名Django HTTPResponse响应体中返回openp

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2