WA了好多次,注意语言和数据范围

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 connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.

Input

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

题目大意:一个人当村长,想共享光纤,输入的是图,求最小生成树;

思路: 按无向图处理;

1.图转化成点到点的距离并储存起来

2.按权有小到大排序

3.Kruskal算法+并查集 构建最小生成树

 #include<stdio.h>
#include<stdlib.h>
struct node
{
int i,j;
int weight;
} s[];
int cmp(const void*a,const void *b)
{
return (*(node*)a).weight-(*(node*)b).weight;
}
int v[];
int findl(int n)
{
return v[n]==n?n:findl(v[n]);
}
int main()
{
int n,i,j,k,a,sum;
while(~scanf("%d",&n))
{
n++,k=,sum=;
for(i=; i<; i++)
v[i]=i;
for(i=; i<n; i++)
{
for(j=; j<n; j++)
{
scanf("%d",&a);
if(j>i)//0及以下的数字直接忽略
{
s[k].weight=a;
s[k].i=i,s[k].j=j,k++;
}
}
}
qsort(s,k,sizeof(node),cmp);//按权值从小到大排序
for(i=; i<k; i++)
{
if(findl(s[i].i)!=findl(s[i].j))//简单的并查集
{
sum+=s[i].weight;
v[findl(s[i].i)]=s[i].j;
}
}
printf("%d\n",sum);
}
return ;
}

Agri-Net poj 1258的更多相关文章

  1. 最小生成树 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 // 顶点的最大个数 ( ...

  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. POJ 1258 Agri-Net|| POJ 2485 Highways MST

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

  4. POJ 1258

    http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...

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

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

  6. POJ 1258 Agri-Net(Prim算法求解MST)

    题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...

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

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

  8. Prim算法求权数和,POJ(1258)

    题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...

  9. poj 1258 Agri-Net 解题报告

    题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 ...

  10. POJ 1258 Agri-Net(Prim)

    题目网址:http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

随机推荐

  1. hive运行query语句时提示错误:org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOException:

    hive> select product_id, track_time from trackinfo limit 5; Total MapReduce jobs = 1 Launching Jo ...

  2. android 03 TableLayout

    MainActivity.java(默认的,什么都没有) package com.sxt.day02_02; import android.os.Bundle; import android.app. ...

  3. 查看pid

    可以使用ps -ef | grep httpd查看PID 然后kill –l PID

  4. isAssignableFrom与instanceof的区别

    1.isAssignableFrom针对的是class对象: 2.instanceof是实例. isAssignableFrom是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的 ...

  5. instanceof的用法①

    public class typeof1{ private String a="zzw"; public void instance(){ if(a instanceof Stri ...

  6. 多线程lock(instance)中instance的选择.

    如我的提问:http://bbs.csdn.net/topics/390496351?page=1#post-394837834 拥有类原子功能的类: class ShareState { //原子功 ...

  7. 01.WPF中制作无边框窗体

    [引用:]http://blog.csdn.net/johnsuna/article/details/1893319   众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormB ...

  8. 解决c#处理excel时故障 找不到可安装的 isam

    直接拷贝的以前代码,但因软件版本,系统环境的变化,导致提示“找不到可安装的 isam”. 我目前新的软件环境:win8.1+office2010+vs2013 解决办法是修改连接字符串: 处理exce ...

  9. (java)从零开始之--装饰者设计模式

    装饰者设计模式:简单定义:增强一个类的功能,而且还可以让这些装饰类互相装饰. 应用场景:当要在某个功能的基础上扩充功能,并且扩充的功能具有大量排列组合,通过继承关系会衍生出大量子类,这时候用装饰者模式 ...

  10. 最新xgboost python32位下安装xgboost

    网上很多windows python下安装xgboost都是很简单的几步无非是visual studio2013以上版本编译,安装.但现在最新的xgboost已经移除了c++工程文件,找到旧版本的也多 ...