POJ 1258:Agri-Net Prim最小生成树模板题
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 45050 | Accepted: 18479 |
Description
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
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
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
题意简单来说就是给了一个图的邻接矩阵,求这个图的最小生成树的代价。
Prim算法模板题。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#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]<min_all)
{
min_temp=j;
min_all=minidis[j];
}
}
s=min_temp;
stack[s]=1;
result += min_all;
}
return result;
} int main()
{
int i,j; while(cin>>num)
{
for(i=1;i<=num;i++)
{
for(j=1;j<=num;j++)
{
scanf("%d",&map[i][j]);
}
}
cout<<prim()<<endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1258:Agri-Net Prim最小生成树模板题的更多相关文章
- POJ 1258 Agri-Net 【Prime】模板题
题目链接>>> 题目大意: 给你N*N矩阵,表示N个村庄之间的距离.FJ要把N个村庄全都连接起来,求连接的最短距离(即求最小生成树).解析如下: #include <c ...
- POJ 1789 Truck History (Kruskal最小生成树) 模板题
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- POJ 1789 Truck History【最小生成树模板题Kruscal】
题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...
- 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 ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- POJ 1860 Currency Exchange【bellman-Ford模板题】
传送门:http://poj.org/problem?id=1860 题意:给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换回到s,能否使本金增多. 思路:bellman-Ford模板题 ...
- O - 听说下面都是裸题 (最小生成树模板题)
Economic times these days are tough, even in Byteland. To reduce the operating costs, the government ...
- POJ 3624 Charm Bracelet(01背包模板题)
题目链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52318 Accepted: 21912 Descriptio ...
- POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63339 Accepted: 2443 ...
随机推荐
- mpvue + Vant weapp + 微信云服务 打造小程序应用
写在前面的话: 从事小程序开发已经大半年的时间了,但是一直都是再用原生写项目.一直想着用框架自己写一个小程序,但苦于一直没有时间.正好最近项目搁置,有了空闲时间,就研究了下mpvue + Vant w ...
- sprintf与sscanf用法举例
一.sscanf 从tmp中读取a,b,c. int main(){ ]; int a; double b; ]; while(gets(tmp) != NULL){ sscanf(tmp, &quo ...
- cf 764A、762A、764B
颓废题 764A #include<bits/stdc++.h> #define LL long long #define N 100005 #define lowbit(x) x& ...
- Vulkan 之 Debugging
1.可以用validation layers 进行验证: 2.Snapdragon Profiler 使用说明
- java方法中使用js的alert。
out.println("<script type=\"text/javascript\">"); out.println("alert( ...
- 怎么在一个servlet中实现多个功能 ?如何使一个Servlet处理多个请求?
自学javaweb一直不知道一个servelt可以有多个功能!看了别人代码才知道这个可以有! 平时你建立servelt时候你会吧doget和dopost这两个勾上,要想实现多个功能,你不必要勾选dog ...
- 一、REACT概述
1.前端/react概述 <从零react> 1.前端工 程概述 Web跨平台.跨浏览 器的应用开发场景 网页浏览器(Web Browser) 通过 CLI 指令去操作的 Headless ...
- 第一部分 JavaScript语言核心(二)
第四章 表达式和运算符 P66 运算符优先级,从上到下: p68 属性访问表达式和调用表达式的优先级比运算符优先级都要高,eg: typeof my.functions[x](y) //typeof在 ...
- MyBatis 关联查询的实现:一对多
有2个实体:用户.订单,一个用户可以拥有多个订单,同时这多个订单属于一个用户,即一对多. user_tb: order_tb: 在“多”的一方(order)添加“一”的一方(user)的主键(user ...
- 一天一个设计模式——Composite组合模式
一.模式说明 能够使容器与内容物具有一致性,创造出递归结构的模式就是Composite组合模式. 举个例子:计算机中的文件系统中有文件和文件夹的概念,我们知道,文件夹可以包含文件,也可以包含子文件夹, ...