ZOJ-1586 QS Network---最小生成树Prim

题目链接:

https://vjudge.net/problem/ZOJ-1586

题目大意:

首先给一个t,代表t个测试样例,再给一个n,表示有n个QS装置,接下来一行是n个QS装置的成本。接下来是n*n的矩阵,表示每两个QS 装置之间链接需要的费用。求在全联通的情况下求最少费用。

思路:

这里需要求最少费用,由于点和边都有权值,索性直接把两端点的权值加到边的权值上,那就变成了一个裸的MST题目了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stack>
 8 #include<map>
 9 #include<sstream>
10 using namespace std;
11 typedef long long ll;
12 const int maxn = 2e3 + 10;
13 const int INF = 1 << 30;
14 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
15 int T, n, m, x;
16 int Map[maxn][maxn];//存图
17 int lowcost[maxn], mst[maxn];
18 int a[maxn];
19 void prim(int u)//最小生成树起点
20 {
21     int sum_mst = 0;//最小生成树权值
22     for(int i = 1; i <= n; i++)//初始化两个数组
23     {
24         lowcost[i] = Map[u][i];
25         mst[i] = u;
26     }
27     mst[u] = -1;//设置成-1表示已经加入mst
28     for(int i = 1; i <= n; i++)
29     {
30         int minn = INF;
31         int v = -1;
32         //在lowcost数组中寻找未加入mst的最小值
33         for(int j = 1; j <= n; j++)
34         {
35             if(mst[j] != -1 && lowcost[j] < minn)
36             {
37                 v = j;
38                 minn = lowcost[j];
39             }
40         }
41         if(v != -1)//v=-1表示未找到最小的边,
42         {//v表示当前距离mst最短的点
43             //printf("%d %d %d\n", mst[v], v, lowcost[v]);//输出路径
44             mst[v] = -1;
45             sum_mst += lowcost[v];
46             for(int j = 1; j <= n; j++)//更新最短边
47             {
48                 if(mst[j] != -1 && lowcost[j] > Map[v][j])
49                 {
50                     lowcost[j] = Map[v][j];
51                     mst[j] = v;
52                 }
53             }
54         }
55     }
56     //printf("weight of mst is %d\n", sum_mst);
57     cout<<sum_mst<<endl;
58 }
59 int main()
60 {
61     cin >> T;
62     while(T--)
63     {
64         cin >> n;
65         for(int i = 1; i <= n; i++)cin >> a[i];
66         for(int i = 1; i <= n; i++)
67         {
68             for(int  j= 1; j <= n; j++)
69             {
70                 cin >> Map[i][j];
71                 if(i == j)continue;
72                 Map[i][j] += a[i] + a[j];//把两个端点的权值加在边上,就转变成裸的MST题目了
73             }
74         }
75         prim(1);
76     }
77     return 0;
78 }

原文地址:https://www.cnblogs.com/fzl194/p/8724624.html

时间: 2024-08-30 16:14:36

ZOJ-1586 QS Network---最小生成树Prim的相关文章

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

ZOJ 1586 QS Network(kruskal)(基础)

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

ZOJ 1586 QS Network prim优化模板

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

zoj 1586 QS Network

最小生成树,刚刚学了Prim算法. 对每条边变的权值进行预处理,c[i][j] = c[i][j] + p[i] + p[j] 其中c[i][j]为输入的权值,p[i],p[j]为连接这两个节点所需的费用. #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 1010; int c[max

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

最小生成树-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

zoj 1203 Swordfish 【最小生成树 prim 算法】

Swordfish Time Limit: 2 Seconds      Memory Limit: 65536 KB There exists a world within our world A world beneath what we call cyberspace. A world protected by firewalls, passwords and the most advanced security systems. In this world we hide our dee

zoj QS 1586 Network (prim算法)

QS Network Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 ea