Day5 - C - Agri-Net POJ - 1258
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
Output
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28 思路:简单的最小生成树板子题(prim)
const int maxm = ; struct Node {
int u, v, w;
Node(int _u, int _v, int _w):u(_u), v(_v), w(_w){}
bool operator<(const Node &a)const{
return a.w < w;
}
}; vector<Node> Edge;
vector<int> G[maxm]; void addedge(int u, int v, int w) {
Edge.push_back(Node(u, v, w));
G[u].push_back(Edge.size() - );
} int N, vis[maxm]; void init() {
for(int i = ; i <= N; ++i)
G[i].clear();
memset(vis, , sizeof(vis));
Edge.clear();
} int main() {
while(scanf("%d", &N) != EOF) {
init();
int val, ans, siz, times;
for(int i = ; i <= N; ++i)
for(int j = ; j <= N; ++j) {
scanf("%d", &val);
if(i != j)
addedge(i, j, val);
}
ans = times = ;
vis[] = ;
priority_queue<Node> q;
siz = G[].size();
for(int i = ; i < siz; ++i) {
int num = G[][i];
q.push(Node(, Edge[num].v, Edge[num].w));
}
while(!q.empty() && times < N) {
Node now = q.top();
q.pop();
int u = now.v;
if(vis[u]++) continue;
ans += now.w;
siz = G[u].size();
times++;
for(int i = ; i < siz; ++i) {
int num = G[u][i];
if(!vis[Edge[num].v])
q.push(Node(u, Edge[num].v, Edge[num].w));
}
}
printf("%d\n", ans);
}
return ;
}
Day5 - C - Agri-Net POJ - 1258的更多相关文章
- 最小生成树 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 // 顶点的最大个数 ( ...
- 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 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- POJ 1258
http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...
- poj - 1258 Agri-Net (最小生成树)
http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...
- POJ 1258 Agri-Net(Prim算法求解MST)
题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...
- (最小生成树)Agri-Net -- POJ -- 1258
链接: http://poj.org/problem?id=1258 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- Prim算法求权数和,POJ(1258)
题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...
- poj 1258 Agri-Net 解题报告
题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 ...
随机推荐
- github日常的基本命令
git 常用命令 git clone 仓库地址 -从远端克隆项目 git pull -从远端拉取代码 git pull -p -从远端拉取代码和分支 提交代码流程: git add xxx -添加到暂 ...
- requests库 代理
import requests proxy = { 'http': '125.123.137.2208:9999' } res = requests.get('http://httpbin.org/i ...
- ANSYS-MFC生成APDL
目录 1. 简介 2. APDL生成 3. 调用ANSYS批处理 1. 简介 对于ANSYS-MFC二次开发,两者之间的关系非常明确,从界面中读取参数并转换成APDL语言,然后调用批处理操作. 对于简 ...
- Ionic3记录之核心代码分析
app.module.ts app的根模块,一些插件的引用需要在这里声明,告诉APP如何组装应用: app.componet.ts app的根组件,主要用来APP启动时和启动后的操作;
- Linux Kernel 5.5 最终删除 SYSCTL 系统调用
导读 Linux Kernel 5.5 最终消除了支持sysctl系统调用的代码,该代码已被弃用了大约十年,目前对任何体系结构的现代系统都没有影响. 长期以来,Linux sysctl系统调用都不建议 ...
- Spark教程——(8)本地执行spark-sql程序
在程序中设定Spark SQL的运行模式: //.setMaster("local")设置本地运行模式 val conf = new SparkConf().setAppName( ...
- AVL-Tree (平衡二叉树)
看到网上AVL-Tree大多数都是用相同的实现方式 —— 递归进行插入.删除.维护平衡等,而我比较喜欢用带父指针的数据结构,于是想了一下午,用C实现了一个迭代版的. 由于没有暂时没有好的画二叉树的工具 ...
- VUe for循环if 的使用和函数的使用 (笔记)
结果如图: 代码html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- HTML的几个注意点
一.HTML 1.HTML5有哪些新特性?新增的标签有哪些? 新特性: 语义标签——语义化标签使得页面的内容结构化,见名知义 增强型表单——拥有多个新的表单 Input 输入类型.这些新特性提供了更好 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 安装
所有平台的 MySQL 下载地址为: MySQL 下载:https://dev.mysql.com/downloads/mysql/ 注意:安装过程我们需要通过开启管理员权限来安装,否则会由于权限不足 ...