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 = ;
- int c[maxn][maxn];//邻接矩阵
- int x[maxn], y[maxn]; //x数组表示i节点到集合的最短距离 y数组表示i节点和集合中哪个点是最短距离
- int p[maxn];
- int main()
- {
- int sb;
- scanf("%d", &sb);
- while (sb--)
- {
- int n, m, i, j, u, v, cost;
- scanf("%d", &n);
- for (i = ; i <= n; i++) scanf("%d", &p[i]);
- for (i = ; i <= n; i++) for (j = ; j <= n; j++) scanf("%d", &c[i][j]);
- for (i = ; i <= n; i++) for (j = ; j <= n; j++) c[i][j] = c[i][j] + p[i] + p[j];
- for (i = ; i <= n; i++) x[i] = c[][i], y[i] = ;
- y[] = -;// y[i] == -1 表示i节点已经放入了集合
- int tot = ;//已经有一个点放入了集合
- int ans = ;
- while ()
- {
- int mincost = 0x7FFFFFFF, v, flag = ;
- for (i = ; i <= n; i++) if (y[i] != - && x[i] < mincost) flag = , v = i, mincost = x[i];
- if (!flag) break;
- y[v] = -; tot++; ans = ans + x[v];
- for (i = ; i <= n; i++) if (y[i] != - && c[v][i] < x[i]) x[i] = c[v][i], y[i] = v;
- }
- if (tot == n)printf("%d\n", ans);
- else printf("No Way!\n");
- }
- return ;
- }
zoj 1586 QS Network的更多相关文章
- ZOJ - 1586 QS Network (Prim)
ZOJ - 1586 QS Network (Prim) #include<iostream> #include<cstring> using namespace std; + ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- ZOJ 1586 QS Network(Kruskal算法求解MST)
题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...
- ZOJ 1586 QS Network Kruskal求最小生成树
QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...
- ZOJ 1586 QS Network MST prim水题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 题目大意: QS是一种生物,要完成通信,需要设备,每个QS需要的设备的价格 ...
- (最小生成树)QS Network -- ZOJ --1586
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 http://acm.hust.edu.cn/vjudge/ ...
- ZOJ QS Network
QS Network Time Limit: 2 Seconds Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...
- ZOJ1586:QS Network (最小生成树)
QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...
- ZOJ1586 QS Network
QS Network Time Limit: 2 Seconds Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...
随机推荐
- zabbix 布署实践【7 H3C网络设备监控模版制作思路】
我们知道,zabbix安装后自带Template OS Linux 模版已满足了绝大部分Linux服务器的基础环境监控,只是我们在其模版上稍微修改,可配合将SWAP监控取消,另存为一个叫OS Linu ...
- PHPStrom使用SASS,SCSS和Compass
以前尝试 SASS 的时候写了一篇安装方法,大部分操作还是相同,下面补充一些内容主要是填坑,实在太TMD坑爹了. 参考这篇文章: http://blog.csdn.net/zhouzme/articl ...
- hdu1041
#include <iostream> #include <string> using namespace std; const int SIZE = 1001; const ...
- 写一个MyList
首先定义接口 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- 【LeeetCode】4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Linux双网卡NAT共享上网
linux双网卡NAT共享上网 术语字汇 私有IP地址(路由不可达地址)是一个被用于本地局域网的IP地址(在互联网中不可见). 公用IP地址(路由可达地址)是一个在互联网中可见的IP地址. IP伪装是 ...
- CodeForces 685B Kay and Snowflake
树的重心,树形$dp$. 记录以$x$为$root$的子树的节点个数为$sz[x]$,重儿子为$son[x]$,重心为$ans[x]$. 首先要知道一个结论:以$x$为$root$的子树的重心$ans ...
- web service client端调用服务器接口
打开项目的web service client 其中wsdl URL http://www.51testing.com/html/55/67755-848510.html 去这里面查找一些公开的 ...
- 浙大pat 1012题解
1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To eval ...
- jQuery的9中构造函数
// 接受一个字符串,其中包含了用于匹配元素集合的 CSS 选择器 jQuery([selector,[context]]) // 传入单个 DOM jQuery(element) // 传入 DOM ...