首页 技术 正文
技术 2022年11月6日
0 收藏 906 点赞 567 浏览 3593 个字

畅通工程

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14103    Accepted Submission(s): 5824

Problem Description

省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。

Input

测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。

Output

对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。

Sample Input

3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100

Sample Output

3 ?

Source

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

题解:显然就是求解最小生成树,用prim算法实现。

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define INF 10000001
#define maxn 101
int map[maxn][maxn];
int cost[maxn];
int chosed[maxn]; //
int count; //统计点的个数
long total_cost; //总费用
void init_map(int n){ //初始化二阶矩阵
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=(i==j?0:INF);
}
void init(int n){
int i;
memset( chosed , 0 , sizeof(chosed));
chosed[1]=1; 21: count=1;
for( i = 1 ; i <= n ; i++ )
cost[i] = map[1][i]; 24: } void prim(int n){
int min,min_idx;
int i;
total_cost=0;
while( count <=n ){
min = INF;
for( i = 2 ; i <= n ; i++ ){
if( !chosed[i] && cost[i] < min){ //选择尚未choosed的node中的最小的权重的边
min_idx=i;
min=cost[i];
}
}
if( min == INF )
return;
chosed[min_idx]=1;
total_cost+=min;
count++;
for( i = 1 ; i <= n ; i++) //按照这个node更新cost[]
if(!chosed[i]&& map[min_idx][i]<cost[i])
cost[i] = map[min_idx][i];
}
} int main(){
int road,node,i;
while(scanf("%d %d",&road,&node)!=EOF&& road ){
init_map(node);
count=0;
while(road—){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
map[a][b]=c;
map[b][a]=c;
}
init(node);
prim(node);
if(count==node)
printf("%ld\n",total_cost);
else
printf("?\n");
}
}

<!–
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
–>

另一版本:kruskal算法(边从小到大排序)

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define maxn 10000
#define N 101
struct node{
int u;
int v;
int w;
}edges[maxn];
int total_cost;
int id[N];
int choosed[N];
int comp(const void*p,const void *q){
struct node a=*(struct node *)p;
struct node b=*(struct node *)q;
return a.w-b.w;
}
int find_root(int idx){ if(id[idx]==-1)
return idx;
return id[idx]=find_root(id[idx]);
} void init(int n,int m){
int i;
memset(choosed,0,sizeof(choosed));
qsort(edges,n,sizeof(struct node),comp); for(i=0;i<=m;i++)
id[i]=-1;
total_cost=0;
}
void kruskal(int n){
int i,x,y;
for(i=0;i<n;i++){
x=find_root(edges[i].u);
y=find_root(edges[i].v);
if(x!=y){
id[y]=x;
total_cost+=edges[i].w;
choosed[edges[i].u]=1;
choosed[edges[i].v]=1;
}
}
}
int main(){
int n,m,i,count;
while(scanf("%d %d",&n,&m)!=EOF&&n){
for(i=0;i<n;i++)
scanf("%d %d %d",&edges[i].u,&edges[i].v,&edges[i].w);
init(n,m);
kruskal(n);
count=0;
for(i=1;i<=m;i++){ if(id[i]==-1)
count++;
}
if(count==1)//注意,不能通过统计choosed[]的数量,即使全部被选择,也未必是一棵连通的树。
printf("%d\n",total_cost);
else
printf("?\n");
}
}

<!–
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
–>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287