[2016-04-14][POJ][1287][Networking]

  • 时间:2016-04-14 14:48:44 星期四

  • 题目编号:[2016-04-14][POJ][1287][Networking]

  • 题目大意:求最小生成树

  • 分析:直接prim算法,更新边的时候,重边取最小值

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. const int maxn = 50 + 10;
  6. const int inf = 0x3f3f3f3f;
  7. int g[maxn][maxn],vis[maxn],lowc[maxn];
  8. int prim(int n){
  9. int ans = 0;
  10. memset(vis,0,sizeof(vis));
  11. for(int i = 2 ; i <= n ; ++i) lowc[i] = g[1][i];
  12. vis[1] = 1;
  13. for(int i = 2 ; i <= n ;++i){
  14. int minc = inf;
  15. int p = -1;
  16. for(int j = 1 ;j <= n ;++j){
  17. if(!vis[j] && minc > lowc[j]){
  18. minc = lowc[j];
  19. p = j;
  20. }
  21. }
  22. if(minc == inf) return -1;
  23. ans += minc;
  24. vis[p] = 1;
  25. for(int j = 1 ; j <= n ; ++j){
  26. if(!vis[j] && lowc[j] > g[p][j])
  27. lowc[j] = g[p][j];
  28. }
  29. }
  30. return ans;
  31. }
  32. int main(){
  33. //freopen("in.txt","r",stdin);
  34. int n,m;
  35. while(~scanf("%d",&n) && n){
  36. scanf("%d",&m);
  37. int a,b,c;
  38. memset(g,0x3f,sizeof(g));
  39. for(int i = 1; i <= m ; ++i ){
  40. scanf("%d%d%d",&a,&b,&c);
  41. g[a][b] = min(c,g[a][b]);
  42. g[b][a] = g[a][b];
  43. }
  44. printf("%d\n",prim(n));
  45. }
  46. return 0;
  47. }

来自为知笔记(Wiz)

时间: 2024-10-12 17:04:32

[2016-04-14][POJ][1287][Networking]的相关文章

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

题目链接:ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible

POJ 1287 Networking (最小生成树)

Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1287 Appoint description:  System Crawler  (2015-06-02) Description You are assigned to design network connections between certain poin

POJ - 1287 Networking

题目链接:http://poj.org/problem?id=1287 Sample Input 1 0 2 3 1 2 37 2 1 17 1 2 68 3 7 1 2 19 2 3 11 3 1 7 1 3 5 2 3 89 3 1 91 1 2 32 5 7 1 2 5 2 3 7 2 4 8 4 5 11 3 5 10 1 5 6 4 2 12 0 Sample Output 0 17 16 26 分析:最小生成树纯模板 1 #include <cstdio> 2 #include &

POJ 1287 Networking (最小生成树模板题)

Description You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between

POJ 1287 Networking(最小生成树)

题意  给你n个点 m条边  求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 55, M = 3000; int par[N], n, m, ans; struct edge{int u, v, w;} e[M]; bool cmp(edge a, edge b){return a.w < b

POJ 1287 Networking【MST模板题】

Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8214 Accepted: 4528 Description You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possib

2016/04/14

常用类 一.System System 代表Java程序运行平台 System 是一个fianl 类  该类的所以属性都是静态 常用的方法: currentTimeMillis();   //返回以毫秒为单位的当前时间 从1970-01-01  开始 long a = System.currentTimeMillis()/1000/60/60/24/365+1970;  System.out.println("当前年份为:"+a); //可以算出当前的年份 System.exit(0)

&ldquo;耐撕&rdquo;团队2016.04.14站立会议

1. 时间 : 19:20--19:40  共计20分钟 2. 人员 : Z   郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:http://www.cnblogs.com/charliePU/), Q 齐嘉亮(博客:http://www.cnblogs.com/dendroaspis-polylepis/), M 张敏(博客:http://www.cnblogs.com/zhangminss/) 3.功能点清单. 1.

Ubuntu 12.04, 14.04 安装 oracle java

第一步:如果系统中已安装了open java,需要清理, 反之 略过此过程 sudo apt-get purge openjdk* 第二步: 安装 sudo apt-get install software-properties-common 第三步: 添加信任的软件源: sudo add-apt-repository ppa:webupd8team/java 第四步,更新 sudo apt-get update 第五步: 安装java java6 sudo apt-get install or