POJ-1258-Agri-Net (prime + heap)
来源:互联网 发布:基站三角定位算法 编辑:程序博客网 时间:2024/05/19 20:20
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 56671
Accepted: 23500
Memory Limit: 10000KTotal Submissions: 56671
Accepted: 23500
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.
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
这道题其实不难,我用prime一次过,但想学一下prime+heap,所以就写了这篇博客,其中还有c++里面的知识,因为没学c++,
都是自己在网上找的,泪奔。。。。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 105;
const int INF = 0x3f3f3f3f;
// int cost[MAXN][MAXN];
// int minCost[MAXN];
struct Edge {
int to,cost;
Edge (int toS = 0, int costS = INF) : to(toS), cost(costS) {} //c++里面的初始化操作
};
vector<Edge> edge[MAXN];
typedef pair<int, int> Dot;
bool used[MAXN];
int costMin[MAXN];
int N;
int prime()
{
int ret = 0;
priority_queue<Dot, vector<Dot>, greater<Dot> > que; //优先队列,最小值优先
memset(costMin, INF, sizeof(costMin));
memset(used, false, sizeof(used));
costMin[0] = 0;
//used[0] = true;
que.push(Dot(0, 0));
// for (int i = 0; i < N; i++) {
// Dot dot = que.top();
// while(used[dot.second]) {
// que.pop();
// dot = que.top();
// }
// que.pop();
// int v = dot.second;
// used[v] = true;
// ret += dot.first;
// Edge es;
// for (int j = 0; j < edge[v].size(); j++) {
// es = edge[v][j];
// if (costMin[es.to] > es.cost) {
// costMin[es.to] = es.cost;
// que.push(Dot(costMin[es.to], es.to));
// }
// }
// }
while (!que.empty()) {
Dot dot = que.top(); que.pop();
int v = dot.second;
if (costMin[v] < dot.first) {
continue;
}
ret += dot.first;
for (int i = 0; i < edge[v].size(); i++) {
Edge es = edge[v][i];
if (costMin[es.to] > es.cost) {
costMin[es.to] = es.cost;
que.push(Dot(costMin[es.to], es.to));
}
}
}
return ret;
}
int main()
{
while(~scanf("%d", &N))
{
for (int i = 0; i < N; i++) { //因为多组数据,所以要清空vector,不写的话wa
edge[i].clear();
}
Edge es;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
es.to = j;
scanf("%d", &es.cost);
edge[i].push_back(es);
}
}
int d = prime();
cout << d << endl;
}
return 0;
}
0 0
- POJ-1258-Agri-Net (prime + heap)
- poj 1258 Agri-Net(最小生成树 prime算法)
- POJ 1258 Agri-Net(最小生成树--prime 水)
- poj 1258Agri-Net(prime,Kruskal )
- POJ 1258 Agri-Net 【MST,Prime算法】
- POJ-1258--Agri-Net (最小生成树 Prime)
- 1258:Agri-Net Prime()水题
- Agri-Net(POJ 1258)(裸最小生成树)(Prime算法+Kruskal算法)
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj 1258-Agri-net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- POJ+1258++Agri-Net
- Poj 1258 Agri-Net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- HDU-1166-敌兵布阵 (线段树)
- .NET 页面生命周期
- linux下使用python连接redis
- prime + heap
- web打印 window.print()介绍
- POJ-1258-Agri-Net (prime + heap)
- 最佳加法表达式(dp)
- 设计模式之工厂模式
- 图片缩略图显示模式变成图标
- phaser 监控线程工具类
- Zedboard---实验一点亮一盏LED
- org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer
- Maven手动添加jar包到本地仓库
- 如果没有微软雅黑自适应就是宋体
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
法爷永远是你大爷
浅沼丽子
沉默的证人
沈伟
油价
羊肉汤的做法
鲫鱼豆腐汤
水痘
水泥罐车撞倒摩托车
水木社区
水库捕获104斤鳡鱼
比熊
朱雨玲4比0索尔佳
比熊犬
毒液
母亲泄身喷射大叫
诸天试武
正当防卫
正体
经典老歌
欧美momax在线vide
长安欧尚x7
欧蓝德
樱桃私人影院
人体模特
榴莲
榜样
楼兰
大久保麻梨子
梦见蛇是什么意思
梦幻图片
篮坛梗王
桔梗
桑葚的功效与作用
桑葚
莫海谢雨桐
桃胶的做法
桂花茶
桂圆的功效与作用
核桃
树叶粘贴画