最小生成树,刚刚学了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的更多相关文章

  1. ZOJ - 1586 QS Network (Prim)

    ZOJ - 1586 QS Network (Prim) #include<iostream> #include<cstring> using namespace std; + ...

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

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

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

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

  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. ZOJ 1586 QS Network MST prim水题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 题目大意: QS是一种生物,要完成通信,需要设备,每个QS需要的设备的价格 ...

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

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

  7. ZOJ QS Network

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

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

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

  9. ZOJ1586 QS Network

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

随机推荐

  1. C语言之进制

    进制 一种计数的方式.侧重点在于计数的时候是逢多少进一. 1)      C语言可以识别的进制 二进制 每一位. 0 0 1 0 0 1 1 0 1 0 1 0 0 1 0 1 1 0 在C语言中,写 ...

  2. MySQL索引类型

    一.简介 MySQL目前主要有以下几种索引类型:1.普通索引2.唯一索引3.主键索引4.组合索引5.全文索引 二.语句 CREATE TABLE table_name[col_name data ty ...

  3. 浅谈 Linux

          1969年,美国贝尔实验室的肯-汤普森在DEC PDP-7机器上开发出了UNIX系统.      1971年,肯-汤普森的同事丹尼斯-里奇发明了C语言:1973年,UNIX系统的绝大部分源 ...

  4. js音乐播放器

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat=&quo ...

  5. hdu 1299 Diophantus of Alexandria(数学题)

    题目链接:hdu 1299 Diophantus of Alexandria 题意: 给你一个n,让你找1/x+1/y=1/n的方案数. 题解: 对于这种数学题,一般都变变形,找找规律,通过打表我们可 ...

  6. hdu 4342 History repeat itself(数学题)

    题目链接:hdu 4342 History repeat itself 题意: 让你找第a个非完全平方数m,并且求前m个数的开方向下取整的和. 题解: 第一个问题: 假设第a个非平方数是X,X前面有n ...

  7. Ubuntu 14.04—Anaconda 相关

    Anaconda 使用国内镜像: https://keyunluo.github.io/2016/07/17/2016-07-17-anaconda-python.html Anaconda下的 De ...

  8. <poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies

    本题链接:http://poj.org/problem?id=2139 Description:     The cows have been making movies lately, so the ...

  9. Makefile.am编写规则

    概念 Makefile.am是比Makefile更高层次的规则只需要指定要生成什么目标,依赖于什么文件,和要安装到什么目录.automake会根据Makefile.am来自动生成Makefile.in ...

  10. JavaScript忍者秘籍——运行时代码求值

    1. 代码求值机制 JavaScript中,有很多不同的代码求值机制. ● eval()函数 ● 函数构造器 ● 定时器 ● <script>元素 - 用eval()方法进行求值 作为定义 ...