最小生成树 prime poj1258

题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树

思路:裸最小生成树 prime就可以了

最小生成树专题

AC代码:

#include "iostream"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#define ll long long
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int N=50000;
struct Edge{
    int fr,to;
    int w;
    friend bool operator< (Edge a, Edge b){
        return a.w>b.w;
    }
};
vector<Edge>Map[N];

void Prime(int n){
    int ans=0;
    Edge now;
    bool vis[N];
    mem(vis);
    priority_queue<Edge> Q;
    while(!Q.empty()) Q.pop();
    for(int i=0; i<Map[1].size(); ++i) Q.push(Map[1][i]);
    vis[1]=1;
    n--;
    while(n--){
        now=Q.top();
        Q.pop();
        if(vis[now.to])while(vis[now.to]){
            now=Q.top();
            Q.pop();
        }
        ans+=now.w;
        vis[now.to]=1;
        for(int i=0; i<Map[now.to].size(); ++i)
            if(!vis[Map[now.to][i].to]) Q.push(Map[now.to][i]);
    }
    printf("%d\n",ans);
}

void Add(int u,int v,int w){
    Edge e;
    e.fr=u,e.to=v,e.w=w;
    Map[u].push_back(e);
}
int main(){
    int t,n,u,v,w;
    while(~scanf("%d",&n)){
        mem(Map),mem(p);
        for(u=1; u<=n; ++u){
            for(v=1; v<=n; ++v){
            scanf("%d",&w);
            if(u!=v)
            Add(u,v,w),Add(v,u,w);
            }
        }
        Prime(n);
    }
    return 0;
}
时间: 2024-12-29 12:31:05

最小生成树 prime poj1258的相关文章

poj1258 Agri-Net +hdu 1233 还是畅通工程 (最小生成树Prime算法)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43215   Accepted: 17683 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

hdu 1875 最小生成树 prime版

最小生成树prime版 大致的步骤 首先选取一个到集合最近的点 然后标记起在集合内部 然后更新最短距离 畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24846    Accepted Submission(s): 8035 Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不

最小生成树Prim poj1258 poj2485

poj:1258 Agri-Net Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Farmer John has been elected mayor of his town! One of his campaign promises was to bri

POJ - 1258(最小生成树.prime)

题目链接: http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68462   Accepted: 28369 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet conn

最小生成树 prime kruskal

带权图分为有向和无向 无向图的最短路径又叫做最小生成树,有prime算法和kruskal算法: 有向图的最短路径算法,有dijkstra算法和floyd算法. 生成树的概念:联通图G的一个子图如果是一棵包含G的所有顶点的树,则该子图称为G的生成树 生成树是联通图的极小连通子图.所谓极小是指:若在树中任意增加一条边,则 将出现一个回路:若去掉一条边,将会使之编程非连通图.生成树各边的权 值总和称为生成素的权.权最小的生成树称为最小生成树,常用的算法有prime算法和kruskal算法. 最小生成树

最小生成树 prime poj1287

poj1287 裸最小生成树 AC代码 1 #include "map" 2 #include "queue" 3 #include "math.h" 4 #include "stdio.h" 5 #include "string.h" 6 #include "iostream" 7 #include "algorithm" 8 #define abs(x) x &g

hdu1875(最小生成树prime)

思路:一开始想用贪心来着,发现贪心有缺陷,然后就用了最小生成树来写,这里用了prime算法,首先,先建个图,两点之间的边的权值就是两个点的距离,然后直接prime模板 代码 #include<iostream> #include<algorithm> #include<cstring> #include<math.h> #include<cstdio> const int inf=0x7fffffff; using namespace std;

POJ - 2485(最小生成树.prime)

题目链接: http://poj.org/problem?id=2485 题目: Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36525   Accepted: 16329 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the

poj 2031 Building a Space Station【最小生成树prime】【模板题】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepted: 2855 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar