【北大 夏令营笔记-最小生成树】poj1258-Agri-Net

简介:
Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 39063 Accepted: 15817
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
Source

USACO 102




最小生成树裸题
Prim算法

AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
struct Edge
{
   int a;
   int b;
   int len;
}per[10010];
int flag[10010];
int cmp(Edge x,Edge y)
{
   if(x.len!=y.len) return x.len<y.len;
}
int main()
{
    int i,j,n,m,k,sum,num;
    while(scanf("%d",&n)!=EOF)
    {
       memset(per,0,sizeof(per));
       k=0;
       for(i=0;i<n;i++)
         for(j=0;j<n;j++)
         {
            scanf("%d",&per[k].len);
            per[k].a=i+1;
            per[k++].b=j+1;
         }
       sort(per,per+k,cmp);
       for(i=1;i<=n;i++)
       flag[i]=1;
       flag[per[0].a]=0;
       sum=0;
       for(i=0;i<k;i++)
       {
          num=flag[per[i].a]+flag[per[i].b];
          if(num==1)
          {
             sum+=per[i].len;
             flag[per[i].a]=flag[per[i].b]=0;
             i=0;
          }
       }
       printf("%d\n",sum);
    }
    return 0;
}

相关文章
|
3月前
|
JSON 开发框架 JavaScript
【Azure Developer】使用.Net Core解析JSON的笔记
【Azure Developer】使用.Net Core解析JSON的笔记
|
5月前
|
XML 机器学习/深度学习 移动开发
技术笔记:log4net使用详解
技术笔记:log4net使用详解
100 0
|
6月前
|
存储 SQL 开发框架
国产化之路 Linux Mono下的asp.net 开发笔记(三)
国产化之路 Linux Mono下的asp.net 开发笔记(三)
|
6月前
|
存储 SQL 开发框架
国产化之路 Linux Mono下的asp.net 开发笔记(二)
国产化之路 Linux Mono下的asp.net 开发笔记(二)
|
6月前
|
存储 开发框架 .NET
国产化之路 Linux Mono下的asp.net 开发笔记(一)
国产化之路 Linux Mono下的asp.net 开发笔记(一)
|
开发框架 .NET 数据库
ASP.NET Core 个人博客项目搭建笔记
简易个人博客项目搭建笔记 1.概述 项目梗概通过做一个比较简单,通俗易懂的个人博客项目,很简单的增删改查,来更好学习asp.net core,这个项目使用asp.net core webapi+elementui来做。 2.数据库设计文章表ID文章标题文章内容创建时间文章类型ID浏览量点赞量作者ID文章类型表ID类型名作者表ID姓名账号密码 MD5 3.架构设计仓储层服务层 MD5加密pu...
81 1
|
数据可视化 程序员 C#
使用Jupyter记事本记录和制作.NET可视化笔记
对于记录笔记的工具特别多,不过对于程序员来说,记录笔记+程序代码+运行结果演示可以同时存在,无疑会极大增加我们的笔记的可读性和体验感。以前在写python的时候,使用jupyter的体验很好,所以此处做一个基于jupyter的记录C#代码的笔记简易教程,供大家围观。
140 0
使用Jupyter记事本记录和制作.NET可视化笔记
C#编程-64:ADO.NET对象模型复习笔记
C#编程-64:ADO.NET对象模型复习笔记
C#编程-64:ADO.NET对象模型复习笔记
|
.NET 开发框架
asp.net原理笔记----页面控件类型,页面状况和asp.net编译过程
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1010885678/article/details/37543103 通过查看asp.
981 0
|
SQL 开发框架 并行计算
在线课程笔记—.NET基础
在线课程笔记—.NET基础
129 0
在线课程笔记—.NET基础