poj 1251

题意:就是求最小生成树的边权值和

prim算法求最小生成树

 1 import java.util.Scanner;
 2 import java.util.Arrays;
 3 /*
 4 prim
 5 */
 6 public class Main{
 7     static int map[][];               //存储邻接矩阵
 8     static boolean visit[];           //标记加入的结点
 9     static int n;
10     static int INF = 0x3f3f3f3f;
11
12     public static int prim(){
13         int sum=0;
14         visit = new boolean[n];
15         visit[0] = true;
16
17         for(int i=0;i<n-1;i++){         //n个结点需要n-1条边,循环n-1次
18             int min = INF;
19             int u = 0;                  //保存下一个要加入树的结点下标
20             for(int j=1;j<n;j++){
21                 if(visit[j]==false&&map[0][j]<min){
22                     min = map[0][j];
23                     u = j;
24                 }
25             }
26             visit[u] = true;            //将结点加入,标记置为true
27             sum+=min;
28             for(int j=1;j<n;j++){       //更新数据
29                 if(visit[j]==false&&map[u][j]<map[0][j])
30                     map[0][j] = map[u][j];
31             }
32         }
33         return sum;
34     }
35
36     public static void main(String[] args){
37         Scanner in = new Scanner(System.in);
38         while(in.hasNext()){
39             n = in.nextInt();
40             if(n==0)
41                 break;
42             map = new int[n][n];
43             for(int i=0;i<n;i++)
44                 Arrays.fill(map[i],INF);
45             for(int i=0;i<n-1;i++){
46                 char chx = in.next().charAt(0);
47                 int j = in.nextInt();
48                 while(j!=0){
49                     j--;
50                     char chy = in.next().charAt(0);
51                     int y = chy - ‘A‘;
52                     int cost = in.nextInt();
53                     map[i][y] = map[y][i] = cost;
54                 }
55             }
56             System.out.println(prim());
57         }
58     }
59 }

Kruskal算法求最小生成树

时间: 2024-08-18 07:20:08

poj 1251的相关文章

POJ 1251 Jungle Roads (prim)

D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extr

ZOJ 1406 POJ 1251 Jungle Roads 丛林中的道路,最小生成树,Kruskal算法

题目链接:ZOJ 1406 POJ 1251 Jungle Roads 丛林中的道路 Jungle Roads Time Limit: 2 Seconds      Memory Limit: 65536 KB The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some ye

[2016-04-09][POJ][1251][Jungle Roads]

时间:2016-04-09 00:02:24 星期六 题目编号:[2016-04-09][POJ][1251][Jungle Roads] 题目大意:给定n个城镇的若干条路及其每月维修的代价,问,在所有城市联通的情况下,最少需要多少维修费 分析: 保证边权最小,并且图联通-–>最小生成树 #include <algorithm> #include <cstring> #include <cstdio> using namespace std; int fa[30]

POJ 1251 Jungle Roads(kruskal)

Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the l

POJ 1251 Jungle Roads (最小生成树)

POJ 1251 Jungle Roads Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road

poj 1251(最小生成树)

Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensi

(最小生成树) Jungle Roads -- POJ -- 1251

链接: http://poj.org/problem?id=1251 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 110; const int INF = 0xfffffff; int n, J[N][N], dist[N], vis[N]; int Prim() { i

Poj(1251),Prim字符的最小生成树

题目链接:http://poj.org/problem?id=1251 字符用%s好了,方便一点. #include <stdio.h> #include <string.h> #define INF 0x3f3f3f3f int maps[305][305]; bool vis[305]; int dis[305]; int n; int Prim() { memset(vis,false,sizeof(vis)); for(int i=1; i<=n; i++) dis[

POJ - 1251(最小生成树.krustal)

题目链接:http://poj.org/problem?id=1251 题目: Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31876   Accepted: 14909 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s

poj 1251 统计难题(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 #include<string> 7 #include<cmath> 8 using namespace