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 直接或间接连通的 最少 ...
随机推荐
- spring boot中的底层配置文件application.yam(application.property)的装配原理初探
*在spring boot中有一个基础的配置文件application.yam(application.property)用于对spring boot的默认设置做一些改动. *在spring boot ...
- Can you answer these queries?-HDU4027 区间开方
题意: 给你n个数,两个操作,0为区间开方,1为区间求和 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 思路: 如果当该区间的数都为1,我们没必要 ...
- ch8 让div居中--使用外边距
假设有一个布局,希望让其中的容器div在屏幕上水平居中,则只需要定义div的宽度,然后将水平外边距设置为auto <body> <div class="wrapper&qu ...
- 「Luogu4556」Vani有约会-雨天的尾巴
「Luogu4556」Vani有约会-雨天的尾巴 传送门 很显然可以考虑树上差分+桶,每次更新一条链就是把这条链上的点在桶对应位置打上 \(1\) 的标记, 最后对每个点取桶中非零值的位置作为答案即可 ...
- window查看连接过的无线密码
for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show profiles') do @echo %j | find ...
- 三 HTML框架标签
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 以及Hibernate和Mybatis区别
ORM框架? Object Relational Mapping,对象-关系映射. 项目中的业务实体有两种表现形式:对象和关系数据,即在内存中表现为对象,在数据库中表现为关系数据. 为什么需要ORM框 ...
- IDEA JSP中报错cannot resolve method println的解决方案
原因是没有导入Tomcat 库, 在 Project structure 添加
- LeetCode206. Reverse Linked List(反转链表)
题目链接:https://leetcode.com/problems/reverse-linked-list/ 方法一:迭代反转 https://blog.csdn.net/qq_17550379/a ...
- vscode git 提交数据到分支
1.vscode菜单--终端--新建终端 git config --global user.name "your name" git config --global ...