sicily 1090 Highways

求最小生成树的长度最小的边,我用的是prim算法:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 const int INF=0x3f3f3f3f;
 7 int map[505][505];
 8 int visit[505];
 9 int dis[505];
10 int ans[505];
11
12 //最小生成树
13 int prim(int n, int cur) //从哪个点开始都可以
14 {
15     memset(ans, 0, sizeof(ans));
16     visit[cur] = 1;
17     int index;
18     int count=0;
19     for(int i=1; i<=n; i++)
20         dis[i] = map[cur][i]; //dis[i]为到每个相邻顶点的最小距离
21
22     for(int i=2; i<=n; i++)
23     {
24         int minn = 0x3f3f3f3f;
25         for(int j=1; j<=n; j++)
26         {
27             if(!visit[j] && dis[j] < minn)
28             {
29                 minn = dis[j];
30                 index = j;
31             }
32         }
33         visit[index] = 1;
34         ans[count++] = minn;
35
36         for(int j=1; j<=n; j++)
37             if(!visit[j] && dis[j] > map[index][j])
38                 dis[j] = map[index][j];
39     }
40     return count;
41 }
42
43 int main()
44 {
45     int t, n;
46     scanf("%d", &t);
47     int k=0;
48     while(t--)
49     {
50         if(k==0)
51             k++;
52         else
53             printf("\n");
54         memset(dis, 0, sizeof(dis));
55         memset(map, 0, sizeof(map));
56         memset(visit, 0, sizeof(visit));
57
58         scanf("%d", &n);
59         for(int i=1; i<=n; i++)
60             for(int j=1; j<=n; j++)
61             {
62                 map[i][j] = INF;
63                 scanf("%d", &map[i][j]);
64             }
65         int cnt = prim(n, 1);
66         sort(ans, ans+cnt);
67         printf("%d\n", ans[cnt-1]);
68     }
69     return 0;
70 }
时间: 2024-08-03 03:17:13

sicily 1090 Highways的相关文章

Sicily 1090. Highways 解题报告

题目链接:Sicily 1090 思路: 简单的最小生成树问题,这里用prim算法即可.用visited数组记录每个结点是否已经被访问,即是否已经在最小生成树中.每次从不在最小生成树中的结点中取出一个key值最小的结点放入生成树中,key值表示结点到已经在生成树中点集合的最小距离.每次加入一个结点后更新与它相邻的结点的key值. 代码: #include <iostream> #include <queue> #include <stdio.h> #include &l

1090. Highways

#include "iostream" #include "memory.h" #include "cstdio" using namespace std; int grap[500][500]; int ans; int dis[500]; bool visited[500]; void prime(int n){ int source = 1; for (int i = 1; i <= n; i++){ dis[i] = grap[so

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

[spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't make up their minds which one to choose. Suppose we have a list of cities that can be c

BZOJ 1090: [SCOI2003]字符串折叠( 区间dp )

按照题意dp...dp(l, r) = min{ dp(l, x) + dp(x+1, r) , 折叠(l, r) } 折叠(l, r)我是直接枚举长度然后哈希判.. -------------------------------------------------------------- #include<bits/stdc++.h> using namespace std; typedef unsigned long long ull; const int maxn = 109; con

Lightoj 1090 - Trailing Zeroes (II)

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q<=1000000,求出的末尾有几个0? 解题思路: 是不是一下子懵了,数字好大,复杂度好高,精度怎么办···············,就问你怕不怕? 因为你是Acmer,这都不应该是问题.因为10的因子只有2和5,所以可以打表保存从1到当前数字相乘的积中分别含有2,5的个数.然后算出中分别含有2,5的个数,

51nod 1090 3个数和为0 &amp; 51nod 1267 4个数和为0(标记二分)

题目意思: 3个数的和为0: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1090 给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从中找出所有和 = 0的3个数的组合.如果没有这样的组合,输出No Solution.如果有多个,按照3个数中最小的数从小到大排序,如果最小的数相等则按照第二小的数排序. Input 第1行,1个数N,N为数组的长度(0 <= N <= 1000) 第2 -

UVA - 1393 Highways

Description Hackerland is a happy democratic country with m×n cities, arranged in a rectangular m by n grid and connected by m roads in the east-west direction and n roads in the north-south direction. By public demand, this orthogonal road system is