POJ 1258 Agri-Net(最小生成樹)
Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 49376 Accepted: 20506
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.
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
本題是一道簡單的最小生成樹題目。題目給出的矩陣代表著每兩個點之間的距離,其他的話沒什麼好說的了。
下面是AC程式碼:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int u,v,cost;
}a[100005];
int pre[105];
int fin(int x)
{
if(x==pre[x])
{
return x;
}
else
{
return pre[x]=fin(pre[x]);
}
}
void join(int x,int y)
{
int t1=fin(x);
int t2=fin(y);
if(t1!=t2)
{
pre[t1]=t2;
}
}
bool cmp(node x,node y)
{
return x.cost<y.cost;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int cnt=0;
for(int i=1;i<=n;i++)
{
pre[i]=i;
for(int j=1;j<=n;j++)
{
scanf("%d",&a[cnt].cost);
a[cnt].u=i;
a[cnt].v=j;
cnt++;
}
}
sort(a,a+cnt,cmp);
int sum=0;
for(int i=0;i<cnt;i++)
{
if(fin(a[i].u)!=fin(a[i].v))
{
join(a[i].u,a[i].v);
sum+=a[i].cost;
}
}
printf("%d\n",sum);
}
return 0;
}
相關推薦
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
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(最小生成樹 Prim演算法)
Agri-Net Time Limit:1000MS Memory Limit:10000KB Description Farmer John has been elected mayor of his town! One of his campaign pro
poj 1258 Agri-Net(最小生成樹模板程式碼)
感覺用這題來當模板更適合。 題意就是給你鄰接矩陣求最小生成樹啦。~ prim程式碼:效率很高。172k...0ms。 #include<stdio.h> #include<alg
POJ 1258 Agri-Net(最小生成樹 Prim 模版題)
大意:新鎮長競選宣言就是將網路帶到每一個農場,給出農場個數,兩兩之間建光纜的耗費,求所有都聯通的最小耗費。 思路:最小生成樹,因為邊比較稠密,用Prim做。 PS;對於比較稠密的圖,用Prim,對於比較稀疏的圖,用 Kruskal。Kruskal是找邊的過程,
POJ-1258 Agri-Net(kruskal最小生成樹)
require .org char long lte scan problem lec self Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 60255 Acce
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.
WUST 1944 最短網路Agri-Net(最小生成樹之prim演算法)
1944: 最短網路Agri-Net Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lldSubmitted: 22 Accepted: 9 [Submit][Status][Web Boar
洛谷 P1546 最短網路 Agri-Net(最小生成樹_Prim)
傳送門 最小生成樹模板,大家都說是Kruskal,但brz大神說是稠密圖要用Prim。 由於大神很強我聽大神的 關於Prim演算法和Kruskal看這裡,我覺得他寫得很好 Code: #i
POJ 1789 Truck History(最小生成樹)
++i ref n) mon 距離 live u+ -- task 題意 有n輛卡車 每輛卡車用7個字符表示 輸入n 再輸入n行字符 第i行與第j行的兩個字符串有多少個相應位置的字符不同 i與j之間的距離就是幾 求連接全部卡車的最短長度 題目不是這個意思
POJ-3522 Slim Span(最小生成樹)
while several oid win pro png zeros nds ber Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8633
poj 1789 Truck History (最小生成樹)
=Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for b
POJ 2349 Arctic Network(最小生成樹)
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Description The Department of National Defence (DND) wishes to connect severa
POJ 1861:Network(最小生成樹&&kruskal)
nis bool cmp edge his table int pst 應該 Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13266 Accepted: 5
UVA 1359 POJ 3522 Slim Span(最小生成樹kruskal)
body void where sca 最小邊 asi efi easily bool Given an undirected weighted graph G, you should find one of spanning trees specified as fo
POJ1258 Agri-Net【最小生成樹】
ios rim def define sin pri ret fine 輸出 題意: 有n個農場,已知這n個農場都互相相通,有一定的距離,現在每個農場需要裝光纖,問怎麽安裝光纖能將所有農場都連通起來,並且要使光纖距離最小,輸出安裝光纖的總距離。 思路: 又是一個最小生成樹,
POJ 3723 Conscription(最小生成樹)
protect pic print script ++ have pac been algo Windy has a country, and he wants to build an army to protect his country. He has picked u
POJ-2421-Constructing Roads(最小生成樹 普利姆)
connected number this brush first cst 數字 main memory Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26694 Accepted: