【prim + kruscal 】 最小生成树模板

来源:dlut oj

1105: Zhuo’s Dream

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 40 Solved: 14
[Submit][Status][Web Board]

Description

Zhuo is a lovely boy and always make day dream. This afternoon he has a dream that he becomes the king of a kingdom called TwoBee.

In the dream Zhuo faced a hard situation: he should make a traffic construction plan for the kingdom. We have known that the kingdom consists of N cities and M dirt roads, it’s very uncomfortable when travelling in the road. Now he would choose some roads to rebuild to concrete road. To avoid called TwoBee by the common people, Zhuo wants to show some excellent thing to this world while it needs your help. He wants his plan achieve two goals:

1. We should choose just N-1 roads to rebuild. After rebuild we can also travel between any two cities by the concrete roads.

2. We want to minimize the longest length among the chosen roads.

So you should write a program to calculate the total length of the chosen roads.

Input

There are multiple test cases.

The first line contains two integers N and M. (1 <= N <= 100, 1 <= M <= 10000)

In the next M lines, each line contains three integers a, b, w, indicating that there is a dirt road between city a and city b with length w. (1 <= a, b <= N, 1 <= w <= 1000)

Output

For each test case, output an integer indicating the total length of the chosen roads. If there is no solution, please output -1.

Sample Input

3 3
1 2 1 1 3 1 2 3 3

Sample Output

2

HINT

Source

Rainy

prim算法

#include <cstdio>
#include <iostream>
#include <memory.h>
using namespace std;
const int maxn=102;
const int inf=1<<30;
int map[maxn][maxn];
int dis[maxn];
bool flag[maxn];
int a,b,c,n,m,ans;

void init()
{
    memset(map,-1,sizeof(map));
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        if(map[a][b]==-1 || c< map[a][b])
        {
            map[a][b]=map[b][a]=c;
        }
    }
}

void prim()
{
    memset(flag,false,sizeof(flag));
    for(int i=1;i<=n;i++)dis[i]=inf;
    dis[1]=0;
    ans=0;
    for(int j=1;j<=n;j++)
    {
        int now,value=inf;
        for(int i=1;i<=n;i++)
        {
            if(flag[i]==false && dis[i]<value)
            {
                value=dis[i];
                now=i;
            }
        }
        if(value==inf)
        {
            cout << "-1" <<endl;
            return;
        }
        flag[now]=true;
        ans+=dis[now];
        for(int i=1;i<=n;i++)
        {
            if(!flag[i] && map[now][i]!=-1 && dis[i]>map[now][i])
            dis[i]=map[now][i];
        }
    }
     cout << ans <<endl;
}

int main()
{
    //‘freopen("in.txt","r",stdin);
    while(cin >> n >> m)
    {
        init();
        prim();
    }
    return 0;
}

下面是最小生成树的Kruskal算法,这个算法原理看起来很复杂,但实现起来很简单:开始的时候是每个顶点一棵树,并将边按权重升序排列。然后从前到后按循序选边,如果当前选择的边的两个顶点分在两棵不同的树中,则将该边加入到最小生成树中,并合当前边连接的两棵树,如果边得两个顶点在相同的树中,则不做任何处理,需要注意的是这个算法是针对无向连通图的,如果是有限图,则需要在算法中做些处理,但算法原理是一样的。

kruskal算法:

#include <cstdio>
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
const int maxn=10002;
int m,n;
int cnt,length;
int parent[maxn];

struct node
{
    int from;
    int to;
    int val;
}s[maxn];

bool cmp(const node &l,const node &r)
{
    return l.val<r.val;
}

void init()
{
    for(int i=0;i<maxn;i++)
    {
        parent[i]=i;
    }
}

int find(int x)
{
    while(x!=parent[x])
    {
        x=parent[x];
    }
    return x;
}

void merge(int a,int b,int val)
{
    int roota=find(a);
    int rootb=find(b);
    if(roota!=rootb)
    {
        cnt++;
        parent[rootb]=roota;
        length+=val;
    }
}

void kruskal()
{
    sort(s,s+n,cmp);
    for(int i=0;i<n;i++)
    {
        merge(s[i].from,s[i].to,s[i].val);
        if(cnt==m-1)break;
    }
    if(cnt<m-1)
    {
        length=-1;
    }
}

int main()
{
   // freopen("in.txt","r",stdin);
    while(cin >> m >> n)
    {
        init();
        cnt=0,length=0;
        memset(s,0,sizeof(s));
        for(int i=0;i<n;i++)
        {
              cin >> s[i].from >> s[i].to >> s[i].val;
        }
       kruskal();
        cout << length <<endl;
    }
    return 0;
}
时间: 2024-11-06 04:08:46

【prim + kruscal 】 最小生成树模板的相关文章

HDU 1233 prim kruskal最小生成树模板题

A - 还是畅通工程 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小.请计算最小的公路总长度. Input 测试输入包含若干测试

最小生成树模板

//最小生成树模板 /* kruskal算法,把所有的边从小到大排序,接下来从小到大考查每条边(u,v); 1.u和v在同一个连通分量中,那么加入(u,v)后会形成环,因此不能选择. 2.如果u和v在不同的联通分量中,那么加入(u,v)一定是最优的. */ #include<iostream> #include<cstring> #include<algorithm> using namespace std; int fat[102];//存放父节点 struct Lu

(heu step 6.1.1)Constructing Roads(最小生成树模板题:求让n个点连通的最小费用)

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

Agri-Net POJ 1258(最小生成树模板)

原题 题目链接 题目分析 比较明显的最小生成树模板题,题目给的输入是邻接矩阵,处理一下用prim算法就可以算出最小生成树了. 代码 1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio> 5 #include <cmath> 6 #include <cstring> 7 #include <string> 8

hdu 4009 Transfer water(最小树形图:有向图的最小生成树模板)

题目: 链接:点击打开链接 题意: 有n个村庄,要求使得每个村庄都能得到水的最小费用.每个村庄可以通过挖井或从其他村庄修水路获得水.挖井的费用是房子的高度乘以X,修水道的费用和有向图边的起点和终点的高度有关. 思路: 代码: #include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define inf 0x3f3f3f3f

还是畅通工程——最小生成树模板题

题目链接 题意: 给定n个村庄,m=(n*(n-1)/2)条关系  u,v,w 表示  u到 v之间的距离是 w 题解: 裸最小生成树模板题 代码: #include<iostream> #include<stdio.h> #include<math.h> #include<algorithm> #include<vector> using namespace std; typedef long long ll; const int maxn =

最小生成树--prim+优先队列优化模板

prim+优先队列模板: 1 #include<stdio.h> //大概要这些头文件 2 #include<string.h> 3 #include<queue> 4 #include<vector> 5 #include<algorithm> 6 using namespace std; 7 typedef pair<int,int> pii; 8 9 int head[30],next[200],point[200],val[2

最小生成树模板【kruskal &amp; prim】

CDOJ 1966 Kruskal 解法 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 8 const int N=2002; 9 const int M=2e5+2; 10 int n,m,tot=0,num=0; 11 LL ans=

hdu 1863 畅通工程 最小生成树模板入门题 prim+kruskal两种算法AC。

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18866    Accepted Submission(s): 8012 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出