前向星式prim模板

来源:互联网 发布:亚加装饰 知乎 编辑:程序博客网 时间:2024/05/29 19:14
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>const int maxn=110;const int INF=0x3f3f3f3f;using namespace std;struct edge{    int to,w,next;} edge[maxn];bool vis[maxn];//maxn是点的个数,p记录状态int dist[maxn];//dist记录最短边长int pre[maxn];//pre记录连接信息int head[maxn];int tot;void init(){    memset(head,-1,sizeof(head));    tot=0;}void addedge(int u,int v,int w){    edge[tot].to=v;    edge[tot].w=w;    edge[tot].next=head[u];    head[u]=tot++;}int prim(int n){    for(int i=1; i<=n; i++) dist[i]=INF;    for(int i=head[1]; i!=-1; i=edge[i].next)        dist[edge[i].to]=edge[i].w,vis[edge[i].to]=false,pre[edge[i].to]=1;    dist[1]=0;    vis[1]=true;    for(int i=1; i<n; i++)    {        int min0=INF,k=0;        for(int j=1; j<=n; j++)        {            if(!vis[j] && min0>dist[j])            {                min0=dist[j];                k=j;            }        }        if(k==0)return 0;        vis[k]=true;        for(int j=head[k]; j!=-1; j=edge[j].next)            if(!vis[edge[j].to] && edge[j].w<dist[edge[j].to])                dist[edge[j].to]=edge[j].w, pre[edge[j].to]=k;    }    int ans=0;    for(int i=1; i<=n; i++)        ans+=dist[i];    return ans;}int main(){    int n,m;    scanf("%d%d",&n,&m);    init();    while(m--)    {        int u,v,w;        scanf("%d%d%d",&u,&v,&w);        addedge(u,v,w);    }    printf("%d\n",prim(n));    return 0;}
1 0