无向图最小生成树

1212 无向图最小生成树 
基准时间限制:1 秒 空间限制:131072 KB 分值: 0  难度:基础题
 收藏
 关注
N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树。
Input
第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量。(2 <= N <= 1000, 1 <= M <= 50000)
第2 - M + 1行:每行3个数S E W,分别表示M条边的2个顶点及权值。(1 <= S, E <= N,1 <= W <= 10000)
Output
输出最小生成树的所有边的权值之和。
Input示例
9 14
1 2 4
2 3 8
3 4 7
4 5 9
5 6 10
6 7 2
7 8 1
8 9 7
2 8 11
3 9 2
7 9 6
3 6 4
4 6 14
1 8 8
Output示例
37

Prime算法

#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int cost[1020][1020];//cost[u][v]表示边e=(u,v)的权值(不存在的情况下设为INF)
int mincost[1020];//从集合X出发的边到每个顶点的最小权值
bool used[1020];//顶点i是否包含在集合X中
int N,M,S,E,W;
int prime()
{
    for(int i=1;i<=N;i++)
    {
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[1]=0;
    int res=0;
    while(true)
    {
        int v=-1;//从不属于X的顶点中选取从X到其权值最小的顶点
        for(int u=1;u<=N;u++)
        {
            if(!used[u]&&(v==-1||mincost[u]>N>>M;
    memset(cost,INF,sizeof(cost));
    for(int i=0;i>S>>E>>W;
        cost[S][E]=min(W,cost[S][E]);
        cost[E][S]=min(W,cost[E][S]);
    }
    cout<

Kruskal算法

#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
struct edge{int u,v,cost;};
bool cmp(const edge& e1,const edge& e2){
    return e1.cost>V>>E;
    for(int i=0;i>es[i].u>>es[i].v>>es[i].cost;
    cout<

你可能感兴趣的:(无向图最小生成树)