POJ-1258-Agri-Net
来源:互联网 发布:家用网络机顶盒哪个好 编辑:程序博客网 时间:2024/06/17 05:13
Memory Limit: 10000KTotal Submissions: 56603
Accepted: 23473
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
Output
Sample Input
40 4 9 214 0 8 179 8 0 1621 17 16 0
Sample Output
28
解题思路:很简单的一道模板题用prime和kruskal应该都可以这个题是prime
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 105;
const int INF = 0x3f3f3f3f;
int cost[MAXN][MAXN];
int minCost[MAXN];
bool used[MAXN];
int N;
int prime()
{
memset(minCost, INF, sizeof(minCost));
memset(used, false, sizeof(used));
for(int i = 0; i < N; i++){
minCost[i] = cost[0][i];
}
used[0] = true;
minCost[0] = 0;
int ret = 0;
while(true){
int v = -1;
for(int i = 0; i < N; i++){
if(!used[i] && (v == -1 || minCost[v] > minCost[i])){
v = i;
}
}
if(v == -1)
break;
used[v] = 1;
ret += minCost[v];
for(int i = 0; i < N; i++){
//if(!used[i])
{
minCost[i] = min(minCost[i], cost[v][i]);
}
}
}
return ret;
}
int main()
{
while(~scanf("%d", &N))
{
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
scanf("%d", &cost[i][j]);
}
}
int d = prime();
cout << d << endl;
}
return 0;
}
- 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
- 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
- VOR导航原理
- POJ-1860-Currency Exchange(bellman)
- POJ-2253-Frogger
- 学习资料|王江源博客推荐,21篇优…
- POJ-1125-Stockbroker Grapevine
- POJ-1258-Agri-Net
- 卷积神经网络基础总结
- POJ-2485-Highways(kruskal)
- Globle.asax错误:异常详细信息: S…
- HDU-1166-敌兵布阵 (线段树)
- .NET 页面生命周期
- linux下使用python连接redis
- prime + heap
- web打印 window.print()介绍