Agri-Net(最小生成樹)
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.
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
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
思路:
很直接的prime題,克魯斯卡爾也可以。
程式碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
#define INF 0x3f3f3f3f
int N;
int map[105][105];
bool book[105];
int lenth[105];
int Prime()
{
memset(book,false,sizeof(book));
int sum = 0;
book[0] = true;
for(int i=1 ; i<N ; i++)
{
lenth[i] = map[0][i];
}
for(int i=1 ; i<N ; i++)
{
int min = INF;
int node;
for(int j=1 ; j<N ; j++)
{
if(book[j] == false && lenth[j]<min)
{
min = lenth[j];
node = j;
}
}
sum += min;
book[node] = true;
for(int i=1 ; i<N ; i++)
{
if(book[i] == false && lenth[i]>map[node][i])
{
lenth[i] = map[node][i];
}
}
}
return sum;
}
int main()
{
while(cin>>N)
{
for(int i=0 ; i<N ; i++)
{
for(int j=0 ; j<N ; j++)
{
scanf("%d",&map[i][j]);
}
}
cout<<Prime()<<endl;
}
return 0;
}
相關推薦
Agri-Net(最小生成樹)
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet co
[ACM] poj 1258 Agri-Net (最小生成樹)
call pack 地圖 rest width none minimum logic 最小值 版權聲明:本文為博主原創文章,未經博主同意不得轉載。 https://blog.csdn.ne
洛谷Oj-最短網路 Agri-Net-最小生成樹(模板題)
問題描述: 約翰已經給他的農場安排了一條高速的網路線路,他想把這條線路共享給其他農場。為了用最小的消費,他想鋪設最短的光纖去連線所有的農場。 你將得到一份各農場之間連線費用的列表,你必須找出能連線所
poj 1258 Agri-Net (最小生成樹)
Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all far
poj1258 Agri-Net 最小生成樹,kruskal演算法
思路:原始的最小生成樹。第一次寫kruskal演算法,一開始老是RE,還不明白怎麼了,原來連kruskal的過程都忘了。對不起資料結構老師。。。 ///2014.7.7 ///poj1258 /* *最小生成樹,kruskal演算法 */ #include <
POJ 1258 Agri-Net(最小生成樹prim演算法)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45283 Accepted: 18599 Descri
POJ 1258 Agri-Net 最小生成樹
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the ar
POJ 1258 Agri-Net [最小生成樹] 《挑戰程式設計競賽》2.5
給定各個村子之間的距離, 求最小生成樹 題解: 按題目給的輸入資料來說Prim可能更合適。但我更熟悉Kruskal, 所以強行用Kruskal。 程式碼: #includ
poj 1258 Agri-Net 最小生成樹模板
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; #d
POJ 題目1258 Agri-Net(最小生成樹)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42310 Accepted: 17287 Description Farmer John has been elected
POJ 1258 Agri-Net(最小生成樹)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49376 Accepted: 20506 Description Farmer John h
POJ(1258):Agri-Net(最小生成樹)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68523 Accepted: 28406 Description Farmer John has been elect
POJ1258 Agri-Net(最小生成樹)
Agri-Net 題目連結: 解題思路: 最小生成樹!!! AC程式碼(kruskal): #include <iostream> #include <cstdio&g
洛谷 P1546 最短網絡 Agri-Net(最小生成樹)
pre div spa 鏈接 namespace tdi 我們 nbsp 個數字 嗯... 題目鏈接:https://www.luogu.org/problemnew/show/P1546 首先不難看出這道題的思想是用了最小生成樹,但是這道題有難點: 1.
(極差最小生成樹)POJ 3522 - Slim Span
給定 ans operator 維護 無向圖 ostream bre opera max 題意: 給定一張無向圖,求出一個最長邊減最短邊最小的生成樹。 分析: 這題之前做過一模一樣的(應該是。。。),跑kruskal算法,維護一個subset,一旦出現了環,就刪除這條
搭橋(最小生成樹)
namespace cin 包含 text 數據 codevs ive spa -m codevs——1002 搭橋 時間限制: 1 s 空間限制: 128000 KB 題目等級 : 黃金 G
51nod 1640 天氣晴朗的魔法 (最小生成樹)
sam out 生成樹 魔法 space int 與此同時 names 算法題 1640 天氣晴朗的魔法 題目來源: 原創 基準時間限制:1 秒 空間限制:131072 KB 分值: 20 難度:3級算法題 收藏 關註 取消
(最小生成樹)Codeforces Educational Codeforces Round 9 Magic Matrix
屬於 while 數組 write call pre rar matrix ring You‘re given a matrix A of size n?×?n. Let‘s call the matrix with nonnegative elements magic i
POJ 1789 Truck History(最小生成樹)
++i ref n) mon 距離 live u+ -- task 題意 有n輛卡車 每輛卡車用7個字符表示 輸入n 再輸入n行字符 第i行與第j行的兩個字符串有多少個相應位置的字符不同 i與j之間的距離就是幾 求連接全部卡車的最短長度 題目不是這個意思
小鎮網(最小生成樹)
方案 ac代碼 簡單的 int scrip 矩陣 安排 con ems Description 農民約翰被選為他們鎮的鎮長!他當中一個競選承諾就是在鎮上建立起互聯網。並連接到全部的農場。當然,他須要你的幫助。約翰已經給他的農場安排了一條快速的網絡線路。他想