zoukankan      html  css  js  c++  java
  • Prime

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;

    const int max_v=100;
    const int INF=1<<20;
    int V,E;
    int d[max_v];
    bool used[max_v];
    int cost[max_v][max_v];

    int prim()
    {
        fill(used,used+V,false);
        fill(d,d+V,INF);
        d[0]=0;
        int ans=0;
        while(true)
        {
            int v=-1;
            for(int u=0;u<V;u++)
            {
                if(!used[u]&&(v==-1||d[u]<d[v]))
                    v=u;
            }
            if(v==-1)
                break;
            used[v]=true;
            ans+=d[v];
            for(int u=0;u<V;u++)
            {
                d[u]=min(d[u],cost[v][u]);
            }
        }
        return ans;
    }
    int main()
    {
        cin>>V>>E;
        for(int i=0;i<V;i++)
            for(int j=0;j<V;j++)
                cost[i][j]=INF;
        for(int i=0;i<E;i++)
        {
            int x,y,val;
            cin>>x>>y>>val;
            cost[x][y]=cost[y][x]=val;
        }
        int res=prim();
        cout<<res<<endl;
        return 0;
    }

  • 相关阅读:
    第五次作业
    第四次作业
    第三次作业
    第二次作业
    第5次作业
    4
    第三次
    2
    11
    第五次作业
  • 原文地址:https://www.cnblogs.com/unknownname/p/7790073.html
Copyright © 2011-2022 走看看