POJ2421 Constructing Roads

Constructing Roads

这道题很水,就是一个裸的最小生成树,最不过把已经连接的节点的值再设为0。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #define N 1010
 4 int dis[N][N],minn[N],n,m,a,b;
 5 bool u[N];
 6 int main(){
 7     scanf("%d",&n);
 8     for(int i=1;i<=n;++i)
 9         for(int j=1;j<=n;++j)
10             scanf("%d",&dis[i][j]);
11     scanf("%d",&m);
12     for(int i=1;i<=m;++i){
13         scanf("%d%d",&a,&b);
14         dis[a][b]=0;
15         dis[b][a]=0;
16     }
17     memset(minn,0x7f,sizeof(minn));
18     minn[1]=0;
19     memset(u,1,sizeof(u));
20     for(int i=1;i<=n;++i){
21         int k=0;
22         for(int j=1;j<=n;++j)
23             if(u[j]&&(minn[j]<minn[k]))
24                 k=j;
25         u[k]=0;
26         for(int j=1;j<=n;j++)
27             if(u[j]&&(dis[k][j]<minn[j]))
28                 minn[j]=dis[k][j];
29     }
30     int total=0;
31     for(int i=1;i<=n;++i)
32         total+=minn[i];
33     printf("%d",total);
34     return 0;
35 }

时间: 2024-11-02 23:31:47

POJ2421 Constructing Roads的相关文章

POJ2421 &amp; HDU1102 Constructing Roads(最小生成树)

嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告诉你每个村庄之间的距离,和几组已经联通的村庄,求使所有村庄联通所要建的道路的最短距离. 很简单,用最小生成树的Prim算法,相当于邻接矩阵已知,把已联通的村庄之间的距离改为零即可. 附上AC代码: 1 #include <stdio.h> 2 #include <string.h> 3

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e

Constructing Roads In JGShining&#39;s Kingdom HDU - 1025

JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities)

Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 274 Accepted Submission(s): 172   Problem Description There are N villages, which are numbered from 1 to N, and you should build

hdu-1025 Constructing Roads In JGShining&#39;s Kingdom(二分查找)

题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21045    Accepted Submission(s): 5950 Problem Description JGShining's kingdom consists of 2n(n is

Constructing Roads (MST)

Constructing Roads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1102 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two vill

hdu 1102 Constructing Roads Kruscal

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更适合用Prim来做. 用Kruscal的话要做一些变化. /*Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s)

hdu Constructing Roads(最小生成树,kuskal算法)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14569    Accepted Submission(s): 5530 Problem Description There are N villages, which are numbered from 1 to N, and you should

hdu oj1102 Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13995    Accepted Submission(s): 5324 Problem Description There are N villages, which are numbered from 1 to N, and you should