23333333333

完全是道水题。因为是偶自己读懂自己做出来的。。T_T、prim的模板题水过。

DESCRIPTION:
John竞选的时候许诺会给村子连网。现在给你任意两个村子之间的距离。让你求任意两个村庄是连通的所需要的网线。就是求最小生成树的权值。

附代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#define inf 0x1f1f1f1f
using namespace std;

int map[210][210];
int n;

int prim()
{
    int low[210], vis[210];
    int min, res = 0, p;
    memset(vis, 0, sizeof(vis));
    vis[0] = 1;
    for (int i=1; i<n; ++i)
    {
        low[i] = map[0][i];
    }
    for (int i=1; i<n; ++i)  // 循环n-1次 找最小生成树的n-1个点
    {
        min = inf;
        p = -1;
        for (int j=0; j<n; ++j)
        {
            if (vis[j] == 0 && min > low[j])
            {
                min = low[j];
                p = j;
            }
        }
        if (res == inf)
        return -1;
        res += min;
        vis[p] = 1;
        for (int j=0; j<n; ++j)
        {
            if (vis[j] == 0 && low[j] > map[p][j])
            {
                low[j] = map[p][j];
            }
        }
    }
    return res;
}

int main()
{
    while(cin >> n)
    {
        memset(map, 0, sizeof(map));
        for (int i=0; i<n; ++i)
        {
            for (int j=0; j<n; ++j)
            cin >> map[i][j];
        }
        int ans = prim();
        cout << ans << endl;
    }
    return 0;
}

POJ 1258 最小生成树的更多相关文章

  1. poj 1258 最小生成树 模板

    POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #in ...

  2. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  3. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  4. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

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

    http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...

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

    链接: http://poj.org/problem?id=1258 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  7. poj 1258 Agri-Net 最小生成树 kruskal

    点击打开链接 Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33733   Accepted: 13539 ...

  8. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

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

    Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...

随机推荐

  1. 新浪博客地址 http://blog.sina.com.cn/u/2145079955

    原来 新浪博客地址 http://blog.sina.com.cn/u/2145079955

  2. Xstream 学习地址

    http://forestqqqq.iteye.com/category/301129

  3. select into from 和 insert into select 的区别和用法及 SQL SELECT INTO 中Undeclared variable错误解决办法

    今天试了一下数据表中的数据备份到另一个空的数据表,然后使用了SQL SELECT INTO语句,然后提示Undeclared variable......错误,现在在这里做下总结并给出解决办法. 应用 ...

  4. POJ2676,HDU4069解决数独的两种实现:DFS、DLX

    搜索实现:解决数独有两种思考策略,一种是枚举当前格能填的数字的种数,这里有一优化策略就是先搜索能填入种数小的格子:另一种是考虑处理某一行(列.宫)时,对于某一个没用过的数字,若该行(列.宫)只有一个可 ...

  5. JavaSE复习_6 枚举类

    △单例类是指只有一个实例,而枚举类实际上就是有有限个实例的类,在类里已经把实例定义好了. △枚举类的三种创建形式: 1) enum Week { MON,TUE,WED;//枚举类有默认构造函数创建的 ...

  6. js控制只能输入数字

    onkeyup=clearNoNum(this) function clearNoNum(obj) {    obj.value = obj.value.replace(/[^\d.]/g," ...

  7. activiti jbpm相关资源

    Activiti 5.16 用户手册 http://www.mossle.com/docs/activiti/index.html jBPM 4.4开发指南 http://www.mossle.com ...

  8. 聊天界面之气泡文本cell(一)

    在实现qq聊天界面的过程中,使用UITableViewCell碰到了不少问题,这里还是记录一下以免遗忘. 气泡聊天cell的实现,网上最多的方法还是: 1.手动计算设置frame的值,文本的size使 ...

  9. iOS开发 UIPanGestureRecognizer手势抽象类

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@sel ...

  10. commonJS — 数组操作(for Array)

    for Array github: https://github.com/laixiangran/commonJS/blob/master/src/forArray.js 代码 /** * Creat ...