Agri-Net - poj 1258 (Prim 算法)

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44373   Accepted: 18127

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
 1 #include <stdio.h>
 2 #define INF 100000
 3 int n;
 4 int map[200][200];
 5 int dis[200],vis[200];
 6 int Prim(){
 7     for(int i=0;i<n;i++){
 8         vis[i]=0;
 9         dis[i]=INF;
10     }
11     dis[0]=0;
12
13     for(int i=0;i<n;i++){
14         int min=INF;
15         int p;
16         for(int j=0;j<n;j++){
17             if(!vis[j]&&min>dis[j]){
18                 min=dis[j];
19                 p=j;
20             }
21         }
22         vis[p]=1;
23         for(int j=0;j<n;j++){
24             if(!vis[j]&&dis[j]>map[p][j]){
25                 dis[j]=map[p][j];
26             }
27         }
28     }
29     for(int i=1;i<n;i++){
30         dis[0]+=dis[i];
31     }
32     return dis[0];
33 }
34 int main() {
35     while(~scanf("%d",&n)){
36     for(int i=0;i<n;i++){
37         for(int j=0;j<n;j++){
38             scanf("%d",&map[i][j]);
39         }
40     }
41     int min=Prim();
42     printf("%d\n",min);
43     }
44     return 0;
45 }
时间: 2024-08-29 07:56:48

Agri-Net - poj 1258 (Prim 算法)的相关文章

Truck History - poj 1789 (Prim 算法)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company

Highways - poj 2485 (Prim 算法)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24383   Accepted: 11243 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian g

POJ 1258 Agri-Net(Prim算法)

题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> #include<cstring> using namespace std; int n; int tot; int v[150][150]; int dist[150];//存 节点到树 的最小距离 bool use[150];//标记节点是否存在 int main(){ while(sca

POJ 1258 Agri-Net(Prim)

题目网址:http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 60004   Accepted: 24855 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet conne

POJ 1258 Agri-Net (最小生成树+Prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39820   Accepted: 16192 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 nee

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 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 nee

poj 2349 Arctic Network (prim算法)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10525   Accepted: 3470 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 1258 Agri-Net (prim最小生成树)

最小生成树模板题 #include<bits/stdc++.h> using namespace std; int n,a; int dist[120],m[120][120]; void prim() {     bool p[1020];     for(int i=2;i<=n;i++)     {         p[i]=false;         dist[i]=m[1][i];     }     dist[1]=0,p[1]=true;     for(int i=1;

poj 1258 Agri-Net(Prim)(基础)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44487   Accepted: 18173 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 nee