1. 程式人生 > >poj-1258 Agri-Net(最小生成樹 Prim演算法)

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 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

題意:求連通所有村莊的最短距離和。

思路:prim演算法。

初始集合只有一個村莊1,然後迴圈選擇dist[x](初始化=v[x][1])最小的放入集合,sum+=dist[x];

use[x]=1表示x村莊已在集合的連通村莊中。

然後從1~n更新集合外村莊的dist[i]=dist[i]<v[i][x]?dist[i]:v[i][x];

迴圈直至集合外村莊為0;

#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
int dist[200],v[200][200],use[200];
bool cmp(const int &a,const int &b)
{
	return dist[a]<dist[b];
}
int main()
{
	int n,i,j;
	int sum=0;
	int a;
	while(scanf("%d",&n)!=EOF)
	{
		sum=0;
		memset(use,0,sizeof(use));
		vector<int> vec;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
				scanf("%d",&v[i][j]);
			dist[i]=v[i][1];
			if(i!=1) vec.push_back(i);//儲存集合外村莊
		}
		use[1]=1;
		sort(vec.begin(),vec.end(),cmp);
		while(!vec.empty())
		{
			a=vec[0];
			vec.erase(vec.begin());//刪除 表示已放入集合
		    sum+=dist[a];
		    use[a]=1;
			for(i=1;i<=n;i++)
				if(!use[i]) dist[i]=dist[i]<v[i][a]?dist[i]:v[i][a];
				sort(vec.begin(),vec.end(),cmp);//將更新dist後的村莊按從小到大排序
		}
		printf("%d\n",sum);
	}
	return 0;
}


相關推薦

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小生成樹

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(kruskal小生成樹)

require .org char long lte scan problem lec self Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 60255 Acce

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

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.

HDU 1233 還是暢通工程小生成樹prim 模板

Problem Description 某省調查鄉村交通狀況,得到的統計表中列出了任意兩村莊間的距離。省政府“暢通工程”的目標是使全省任何兩個村莊間都可以實現公路交通(但不一定有直接的公路相連,只要能間接通過公路可達即可),並要求鋪設的公路總長度為最小。請計算最小的公路總長度。

7-3 公路村村通小生成樹K演算法

7-3 公路村村通(30 分) 現有村落間道路的統計資料表中,列出了有可能建設成標準公路的若干條道路的成本,求使每個村落都有公路連通所需要的最低成本。 輸入格式: 輸入資料包括城鎮數目正整數N(≤1000)和候選道路數目M(≤3N);隨後的M行對應M條道路,每行給出3個正整數,分別是

POJ 1258 Agri-Net(小生成樹prim演算法)

Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45283 Accepted: 18599 Descri

POJ 1861:Network小生成樹&amp;&amp;kruskal

nis bool cmp edge his table int pst 應該 Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13266 Accepted: 5

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

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-2421-Constructing Roads小生成樹 普利姆

connected number this brush first cst 數字 main memory Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26694 Accepted: