【裸最小生成树】 模板 poj 1258

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define MAX 102
void read();
using namespace std;
int map[MAX][MAX],best[MAX],n;
bool visit[MAX];
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF)
    {
        memset(visit,false,sizeof(visit));
        read();
        int ans=0,k;
        for(int i=1;i<=n;i++)
        {
            best[i]=map[1][i];
        }
        visit[1]=true;
        while(1)
        {
            int min=1 << 30;
            k=0;
            for(int i=1;i<=n;i++)
            {
                if(!visit[i] && best[i]<min)
                {
                    min=best[i];
                    k=i;
                }
            }
            if(k==0)break;
            ans+=min;
            visit[k]=true;
            for(int i=1;i<=n;i++)
            {
                if(!visit[i] &&map[k][i]<best[i])
                {
                    best[i]=map[k][i];
                }
            }
        }
         cout << ans << endl;
    }
    return 0;
}
void read()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&map[i][j]);//输入邻接矩阵
        }
    }
}
时间: 2024-10-04 18:12:38

【裸最小生成树】 模板 poj 1258的相关文章

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

题目链接 题意: 给定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 =

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 1863 HDU 1301 POJ 1258

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

最小生成树,POJ和HDU几道题目的解题报告(基于自己写的模板)

首先POJ题目: 链接:1251 Jungle Roads 题目大意:纯求最小生成树,结果为最小权值边的和.采用邻接表 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <queue> 5 using namespace std; 6 7 #define maxn 30 //最大顶点个数 8 int n; //顶点数,边数 9 10 struct arcn

POJ 1258 Agri-Net (最小生成树+Prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39820   Accepted: 16192 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

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

最小生成树模板题 #include<bits/stdc++.h> using namespace std; int n,a; int dist[120],m[120][120]; void prim() {     bool p[1020];     for(int i=2;i<=n;i++)     {         p[i]=false;         dist[i]=m[1][i];     }     dist[1]=0,p[1]=true;     for(int i=1;

poj 1258 Agri-Net(最小生成树果题)

题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

【POJ 1258】 Agri-Net

[POJ 1258] Agri-Net 最小生成树模板 Prim #include #define INF 0x3f3f3f3f using namespace std; int mp[501][501]; int dis[501]; bool vis[501]; int n; int Prim() { int i,j,w,p,sum = 0; memset(dis,-1,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[1] = 0; for(i = 0