题意:若两个QS之间要想连网,除了它们间网线的费用外,两者都要买适配器, 求使所有的QS都能连网的最小费用。

分析:这个除了边的权值外,顶点也有权值,因此要想求最小价值,必须算边及顶点的权值和。

解决方法:用prim算法,在构造邻接矩阵时,在i到j的权值的基础上再加上i点的权值和j点的权值即可。

附上AC代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define infinity 1000000
  5. #include<iostream>
  6. #include<algorithm>
  7. using namespace std;
  8. int N;
  9. int G[501][501];
  10. int QS[1000];
  11. int lowcost[100000],closeset[100000];
  12. int used[100000];
  13. int prim(int vcount)
  14. {
  15. int sum=0;
  16. int i,j,k;
  17. int min;
  18. for (i=0; i<vcount; i++)
  19. {
  20. lowcost[i]=G[0][i];
  21. closeset[i]=0;
  22. used[i]=0;
  23. }
  24. used[0]=1;
  25. for (i=1; i<=vcount-1; i++)
  26. {
  27. j=0;
  28. min = infinity;
  29. for (k=1; k<vcount; k++)
  30. if ((!used[k])&&(lowcost[k]<min))
  31. {
  32. min =lowcost[k];
  33. j=k;
  34. }
  35. used[j]=1;
  36. sum+=min;
  37. for (k=1; k<vcount; k++)
  38. if (!used[k]&&(G[j][k]<lowcost[k]))
  39. {
  40. lowcost[k]=G[j][k];
  41. closeset[k]=j;
  42. }
  43. }
  44. return sum;
  45. }
  46. int main()
  47. {
  48. int t,i,j,n,sum;
  49. scanf("%d",&t);
  50. while(t--)
  51. {
  52. scanf("%d",&n);
  53. for(i=0;i<n;i++)
  54. scanf("%d",&QS[i]);
  55. for(i=0;i<n;i++)
  56. {
  57. for(j=0;j<n;j++)
  58. {
  59. scanf("%d",&G[i][j]);
  60. G[i][j]=G[i][j]+QS[i]+QS[j];
  61. }
  62. }
  63. sum=prim(n);
  64. printf("%d\n",sum);
  65. }
  66. return 0;
  67. }

  prim算法详见:http://www.cnblogs.com/PJQOOO/p/3855017.html。我是按照这个学的最小生成树。

————Anonymous.PJQ

QS Network(最小生成树)的更多相关文章

  1. ZOJ1586——QS Network(最小生成树)

    QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...

  2. ZOJ1586:QS Network (最小生成树)

    QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...

  3. ZOJ 1586 QS Network (最小生成树)

    QS Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  4. ZOJ 1586 QS Network Kruskal求最小生成树

    QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...

  5. (最小生成树)QS Network -- ZOJ --1586

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 http://acm.hust.edu.cn/vjudge/ ...

  6. 最小生成树 E - QS Network

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

  7. ZOJ1586 QS Network

    QS Network Time Limit: 2 Seconds      Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...

  8. ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...

  9. ZOJ1586 QS Network 2017-04-13 11:46 39人阅读 评论(0) 收藏

    QS Network Time Limit: 2 Seconds      Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...

随机推荐

  1. hibernate 一对多映射

    package com.entity.onetomany; import java.util.ArrayList; import java.util.List; import javax.persis ...

  2. javascript获取页面各种高度

    网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...

  3. 【Java Web】使用URLRewrite实现网站伪静态

    大部分搜索引擎都会优先考虑收录静态的HTML页面,而不是动态的*.jsp.*.php页面.但实际上绝大部分网站都是动态的,不可能全部是静态的HTML页面,因此互联网上大部分网站都会考虑伪静态——就是将 ...

  4. JAVA实现AES的加密和解密算法

    原文 JAVA实现AES的加密和解密算法 import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import ja ...

  5. 9天快速入门java

    Java入门教程[9天快速入门JAVA] §1.3.简单的Java程序 下面我们先介绍两个简单的Java程序,并对其进行分析. 例1.1. public class HelloWorldApp{//a ...

  6. 从底层简析Python程序的执行过程

    摘要:是否想在Python解释器的内部晃悠一圈?是不是想实现一个Python代码执行的追踪器?没有基础?不要怕,这篇文章让你初窥Python底层的奥妙. [编者按]下面博文将带你创建一个字节码级别的追 ...

  7. 【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])

    需求,要求批量新增或者改动一个List,在Spring MVC中是不支持以下代码的写法 @RequestMapping(value = "/update", method = Re ...

  8. UVA 10574 - Counting Rectangles(枚举+计数)

    10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...

  9. RHEL Server 6.3下MySQL5.5.25a源码安装

    OS:RHEL Server 6.3 MySQL:mysql-5.5.25a.tar.gz 相关依赖包: ncurses-5.9.tar.gz bison-2.5.tar.gz 安装MySQL 一.安 ...

  10. yii Query Builder (yii 查询构造器) 官方指南翻译

    /**** Query Builder translated by php攻城师 http://blog.csdn.net/phpgcs Preparing Query Builder 准备 Quer ...