最小生成树-QS Network(Prim)

题目大意:

给出的案例结果得出步骤,如下图所示,从结点1开始查找,找出的一条路径如绿色部分所标注。(关键处在于连接每条路径所需要的适配器的价格得加上去)

代码实现:

 1 #include<iostream>
 2 #include<cstdio>
 3 using  namespace std;
 4 #define MAX 1000
 5 //注意此处范围得按照题意设置为>=1000,否则会Segmentation Fault
 6 #define MAXCOST 0x7fffffff
 7
 8 int graph[MAX][MAX];
 9
10 int prim(int graph[][MAX], int n)
11 {
12     int lowcost[MAX];
13     int mst[MAX];
14     int i, j, min, minid, sum = 0;
15     for (i = 2; i <= n; i++)
16     {
17         lowcost[i] = graph[1][i];
18         mst[i] = 1;
19     }
20     mst[1] = 0;
21     for (i = 2; i <= n; i++)
22     {
23         min = MAXCOST;
24         minid = 0;
25         for (j = 2; j <= n; j++)
26         {
27             if (lowcost[j] < min && lowcost[j] != 0)
28             {
29                 min = lowcost[j];
30                 minid = j;
31             }
32         }
33         sum += min;
34         lowcost[minid] = 0;
35         for (j = 2; j <= n; j++)
36         {
37             if (graph[minid][j] < lowcost[j])
38             {
39                 lowcost[j] = graph[minid][j];
40                 mst[j] = minid;
41             }
42         }
43     }
44     return sum;
45 }
46
47 int main()
48 {
49     int i, j, k, t, en, n,x, y, cost,pre[1001];
50     scanf("%d",&t);
51     while(t--){
52         scanf("%d",&n);
53         for(i=1;i<=n;i++)
54             scanf("%d",&pre[i]);
55         for (i = 1; i <= n; i++)
56         {
57             for (j = 1; j <= n; j++)
58             {
59                 graph[i][j] = MAXCOST;
60             }
61         }
62         //构建图G
63         for(i = 1; i <= n; i++){
64             for(k=1;k<=n;k++){
65                 scanf("%d",&graph[i][k]);
66                 graph[i][k]+=pre[i];//直接将每条路径上存在的适配器价格给加到权值里面去即可
67                 graph[i][k]+=pre[k];
68             }
69         }
70         cost = prim(graph, n);
71         cout <<cost << endl;
72     }
73     return 0;
74 }

原文地址:https://www.cnblogs.com/LJHAHA/p/10356697.html

时间: 2024-08-29 00:58:34

最小生成树-QS Network(Prim)的相关文章

ZOJ 1586 QS Network prim优化模板

链接: 1586 题意: 有一个N X N的网络,每两个点都有边相连,边的权值用邻接矩阵输入,每个点也有一个权值,当它们之间的那条边被选取时,需要加上两个点的权值.求这个网络的最小生成树. 直接套用prim算法的模板 其中用到一个节约内存的优化 将lowdistance 和visit 两个数组 结合起来 如果访问过lowdistance改成-1即可 另外该题中边的权值应为边本身的权值加上两端点的权值. #include<iostream> #include<cstdio> #inc

ZOJ 1586 QS Network(最小生成树 prim)

题意  输入n  然后输入n个数  代表连接时每个站点自身的消耗   然后输入n*n的矩阵  第i行第j列的数代表第i个站点和第j个站点之间路上的花费  链接i,j两个节点的总花费为两站点自身花费加上路上的花费  求⑩这n个站点连通的最小总花费 又是裸的最小生成树  给点的就用prim咯 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 10

ZOJ 1586 QS Network (经典MST~Prim)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two n

QS Network(最小生成树)

题意:若两个QS之间要想连网,除了它们间网线的费用外,两者都要买适配器, 求使所有的QS都能连网的最小费用. 分析:这个除了边的权值外,顶点也有权值,因此要想求最小价值,必须算边及顶点的权值和. 解决方法:用prim算法,在构造邻接矩阵时,在i到j的权值的基础上再加上i点的权值和j点的权值即可. 附上AC代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #define infinity 1000

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

QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two network adapters (one for each QS) and a segm

ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&amp;amp;&amp;amp;prim)

Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they

FZU 1096 QS Network

QS Network Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original ID: 1096 64-bit integer IO format: %I64d      Java class name: Main In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS.

E - QS Network

E - QS Network 思路:最小生成树,数组不要开小了. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 100010 using namespace std; int t,n,tot,sum,ans; int fa[MAXN],mon[MAXN]; struct nond{ int x,y,z; }v[1000050]; i

hdu 1162 Eddy&#39;s picture 最小生成树入门题 Prim+Kruskal两种算法AC

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7428    Accepted Submission(s): 3770 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to