HDU 1102 Constructing Roads (最小生成树+Kruskal算法入门)

【题目链接】click here~~

【题目大意】:已知某几条道路已经修完,求全部道路要通路的最小花费

【解题思路】:基础的Kruskal算法了,按照边的权值从小到大排序一遍,符合条件加入到生成树中

代码:

/*
Author:HRW
kruskal+并查集
*/
#include <bits/stdc++.h>
using namespace std;
const int max_v=105;
const int inf=0x3f3f3f3f;
int u,v,n,m,a,b;
int father[max_v];
int find(int x){
    if(x==father[x]) return x;
    return father[x]=find(father[x]);
}
struct Edge{
    int u,v,w;
} edge[max_v*max_v];
bool cmp(Edge a,Edge b){
    return a.w<=b.w;
}
int kruskal(int n,int m){
    sort(edge,edge+m,cmp);//按照边的权值从小到大排序
    int x,y;
    int res=0;
    for(int i=0; i<m; i++){
        x=edge[i].u;
        y=edge[i].v;
        x=find(x);//判断是否属于同一连通分量
        y=find(y);
        if(x!=y)
        {
            father[y]=x;
            res+=edge[i].w;
        }
    }
    return res;
}
void init(){
    for(int i=1; i<=n; i++)
        father[i]=i;
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        int aa;
        int p=0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++){
                scanf("%d",&aa);
                if(i>=j) continue;//!注意
                edge[p].u=i;
                edge[p].v=j;
                edge[p].w=aa;
                p++;
            }
        init();
        scanf("%d",&m);
        while(m--){
            scanf("%d%d",&a,&b);
            a=find(a);
            b=find(b);
            father[b]=a;
        }
        printf("%d\n",kruskal(n,p));
    }
    return 0;
}
时间: 2024-11-06 07:58:49

HDU 1102 Constructing Roads (最小生成树+Kruskal算法入门)的相关文章

hdu 1102 Constructing Roads 最小生成树Prim算法AC 水~~~

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

hdu 1102 Constructing Roads (Prim算法)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21947    Accepted Submission(s): 8448 Problem Description There are N villa

HDU 1102 Constructing Roads (最小生成树)

最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1表示原图不连通 */ const int INF = 0x3f3f3f3f; const int MAXN = 110; bool vis[MAXN]; int lowc[MAXN]; int map[MAXN][MAXN]; int Prim(int cost[][MAXN], int n) {

hdu 1102 Constructing Roads(最小生成树 Prim)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are conne

HDU 1102 Constructing Roads, Prim+优先队列

题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are conne

HDU 1102 Constructing Roads (裸的并查集)

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

hdu 1102 Constructing Roads (最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14172    Accepted Submission(s): 5402 Problem Description There are N villa

HDU 1102 Constructing Roads【简单最小生成树,Prime算法+Kruskal算法】

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

HDU - 1102 - Constructing Roads (最小生成树--prim算法!!)

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