博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1258:Agri-Net Prim最小生成树模板题
阅读量:5795 次
发布时间:2019-06-18

本文共 2346 字,大约阅读时间需要 7 分钟。

Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45050   Accepted: 18479

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

40 4 9 214 0 8 179 8 0 1621 17 16 0

Sample Output

28

题意简单来说就是给了一个图的邻接矩阵,求这个图的最小生成树的代价。

Prim算法模板题。

代码:

#include 
#include
#include
#include
#include
#include
#pragma warning(disable:4996)using namespace std;int num;int map[102][102];int stack[102];int minidis[102];int prim(){ int i,j,s,result; memset(stack,0,sizeof(stack)); for(i=1;i<=num;i++) { minidis[i]=100005; } stack[1]=1; minidis[1]=0; s=1; result=0; for(i=1;i<=num-1;i++) { int min_all=100005; int min_temp=0; for(j=2;j<=num;j++) { if(stack[j]==0&&minidis[j]>map[s][j]) { minidis[j]=map[s][j]; } if(stack[j]==0&&minidis[j]
>num) { for(i=1;i<=num;i++) { for(j=1;j<=num;j++) { scanf("%d",&map[i][j]); } } cout<
<

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/lightspeedsmallson/p/4785785.html

你可能感兴趣的文章
ReactiveSwift源码解析(三) Signal代码的基本实现
查看>>
(六)Oracle学习笔记—— 约束
查看>>
[Oracle]如何在Oracle中设置Event
查看>>
top.location.href和localtion.href有什么不同
查看>>
02-创建hibernate工程
查看>>
Open Graph Protocol(开放内容协议)
查看>>
Ubuntu18.04中配置QT5.11开发环境
查看>>
Exception的妙用
查看>>
知识图谱在互联网金融中的应用
查看>>
MySQL 到底能不能放到 Docker 里跑?
查看>>
【docker】关于docker 中 镜像、容器的关系理解
查看>>
information_schema系列五(表,触发器,视图,存储过程和函数)
查看>>
瓜子二手车的谎言!
查看>>
[转]使用Git Submodule管理子模块
查看>>
DICOM简介
查看>>
Scrum之 Sprint计划会议
查看>>
List<T> to DataTable
查看>>
[Java]Socket和ServerSocket学习笔记
查看>>
stupid soso spider
查看>>
svn命令在linux下的使用
查看>>