还是畅通工程 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

相关文章

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

AI一键生成 PPT

AI一键生成 PPT 操作步骤 作为一名打工人,是不是经常需要制作各种PPT来分享我的生活和想法。但是,你们知道,有时候灵感来了,时间却不够用了!😩直到我发现了Kimi AI——一个能够自动生成PPT的神奇助手!🌟 什么是Kimi? 一款月之暗面科技有限公司开发的AI办公工具,帮助用户快速生成高质量的演示文稿。 无论你是职场人士、学生还是教师,Kimi都能够为你的办公文

pdfmake生成pdf的使用

实际项目中有时会有根据填写的表单数据或者其他格式的数据,将数据自动填充到pdf文件中根据固定模板生成pdf文件的需求 文章目录 利用pdfmake生成pdf文件1.下载安装pdfmake第三方包2.封装生成pdf文件的共用配置3.生成pdf文件的文件模板内容4.调用方法生成pdf 利用pdfmake生成pdf文件 1.下载安装pdfmake第三方包 npm i pdfma

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

poj 1734 (floyd求最小环并打印路径)

题意: 求图中的一个最小环,并打印路径。 解析: ans 保存最小环长度。 一直wa,最后终于找到原因,inf开太大爆掉了。。。 虽然0x3f3f3f3f用memset好用,但是还是有局限性。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#incl

hdu 1102 uva 10397(最小生成树prim)

hdu 1102: 题意: 给一个邻接矩阵,给一些村庄间已经修的路,问最小生成树。 解析: 把已经修的路的权值改为0,套个prim()。 注意prim 最外层循坏为n-1。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstri

【生成模型系列(初级)】嵌入(Embedding)方程——自然语言处理的数学灵魂【通俗理解】

【通俗理解】嵌入(Embedding)方程——自然语言处理的数学灵魂 关键词提炼 #嵌入方程 #自然语言处理 #词向量 #机器学习 #神经网络 #向量空间模型 #Siri #Google翻译 #AlexNet 第一节:嵌入方程的类比与核心概念【尽可能通俗】 嵌入方程可以被看作是自然语言处理中的“翻译机”,它将文本中的单词或短语转换成计算机能够理解的数学形式,即向量。 正如翻译机将一种语言

poj 3723 kruscal,反边取最大生成树。

题意: 需要征募女兵N人,男兵M人。 每征募一个人需要花费10000美元,但是如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱。 给出若干的男女之间的1~9999之间的亲密关系度,征募某个人的费用是10000 - (已经征募的人中和自己的亲密度的最大值)。 要求通过适当的招募顺序使得征募所有人的费用最小。 解析: 先设想无向图,在征募某个人a时,如果使用了a和b之间的关系