(最小生成树) Networking -- POJ -- 1287

链接:

http://poj.org/problem?id=1287

代码:

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

const int N = 210;
const int INF = 0xfffffff;

int n;
int J[N][N], dist[N];
bool vis[N];

int Prim()
{
    int i, j, ans=0;
    dist[1]=0;
    memset(vis, 0, sizeof(vis));
    vis[1]=1;

    for(i=1; i<=n; i++)
        dist[i]=J[1][i];

    for(i=1; i<n; i++)
    {
        int index=1;
        int MIN=INF;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]<MIN)
            {
                index=j;
                MIN=dist[j];
            }
        }
        vis[index]=1;
        ans += MIN;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]>J[index][j])
                dist[j]=J[index][j];
        }
    }
    return ans;
}

int main ()
{
    while(scanf("%d", &n), n)
    {
        int m, i, j, a, b, t;
        scanf("%d", &m);

        for(i=1; i<=n; i++)
        for(j=1; j<=i; j++)
          J[i][j]=J[j][i]=INF;

        for(i=1; i<=m; i++)
        {
            scanf("%d%d%d", &a, &b, &t);
            J[a][b]=J[b][a]=min(J[a][b], t);
        }

        int ans=Prim();

        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-01 22:38:26

(最小生成树) Networking -- POJ -- 1287的相关文章

Networking POJ - 1287 最小生成树板子题

#include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge{ int a,b,w; }e[N]; bool cmp(edge a,edge b) { return a.w<b.w; } int p[N]; int find(int x) { if(p[x]!=x) p[x]=find(p[x]); return p[x]; } int main() { int

B - Networking - poj 1287

有一些地方需要铺盖电缆,这些地方两点间的路可能不止一条,需要求出来至少需要多少电缆才能让所有的点都连接起来,当然,间接连接也算. ///////////////////////////////////////////////////////////////////////// #include<iostream>#include<cstring>#include<cstdio>#include<queue>#include<vector>usin

POJ 1287 Networking (最小生成树)

Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1287 Appoint description:  System Crawler  (2015-06-02) Description You are assigned to design network connections between certain poin

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

题目链接:ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible

[2016-04-14][POJ][1287][Networking]

时间:2016-04-14 14:48:44 星期四 题目编号:[2016-04-14][POJ][1287][Networking] 题目大意:求最小生成树 分析:直接prim算法,更新边的时候,重边取最小值 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 50 + 10; const int inf = 0x3f3f3f3f; i

最小生成树,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 1287 Networking(最小生成树)

题意  给你n个点 m条边  求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 55, M = 3000; int par[N], n, m, ans; struct edge{int u, v, w;} e[M]; bool cmp(edge a, edge b){return a.w < b

POJ 1287:Networking(最小生成树Kruskal)

Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5976   Accepted: 3231 Description You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of po

POJ 1287 Networking (最小生成树模板题)

Description You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between