201412-4-最优灌溉

(http://115.28.138.223/view.page?gpid=T18)

#include
#include
#include
#define INF 0xfffffff
#define maxn 1005
using namespace std;
int dis[maxn],edge[maxn][maxn];
bool vis[maxn];
int n,m,u,v,w,mi;
void Prim()//点归并 
{
    int i,j,k,ans=0;
    memset(vis,0,sizeof(vis));
    for(i=1;i<=n;i++)
       dis[i]=edge[1][i];
    vis[1]=1;
    for(i=1;ifor(j=1;j<=n;j++)
        {
            if(!vis[j]&&dis[j]1;
        ans+=mi;//ans为距离之和 
        for(j=1;j<=n;j++)
           if(!vis[j]&&dis[j]>edge[k][j])
              dis[j]=edge[k][j];
     }
     printf("%d\n",ans);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            edge[i][j]=INF;//非邻接顶点距离用INF标记 
    for(int i=0;iscanf("%d%d%d",&u,&v,&w);
        edge[u][v]=w;
        edge[v][u]=w;
    }   
    Prim();
    return 0;
} 

你可能感兴趣的:(CCF)