最小生成树,刚刚学了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. 软件各种版本的含义!例如RC,M,GA等等

    RC版本   RC:(Release Candidate)   Candidate是候选人的意思,用在软件上就是候选版本.Release是发行.发布的意思.Release.Candidate.就是发行 ...

  2. R和python连接SQL sever 数据库操作

    在R的使用中,为了方便提取数据, 我们经常要进行数据库进行操作,接下来我们尝试使用R进行连接数据. 这里我们使用R中的RODBC进行操作, 首先,我们需要先配置ODBC资源管理器 通过任务管理器或者w ...

  3. maven之(六)setting.xml的配置文件详解

    setting.xml配置文件 maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.h ...

  4. 使用ObjectInputStream和ObjectOutputStream注意问题

    1.对象序列化,类实现Serializable接口 不需要序列化的属性,使用transient声明 2.使用套接字流在主机之间传递对象注意问题: 学习自:Socket同时使用ObjectInputSt ...

  5. LanSoEditor_advance1.8.0 视频编辑的高级版本

    ------------------------------------------2017年1月11日11:18:33------------------------------------- 我们 ...

  6. [HMLY]1.CocoaPods详解----使用

    作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/18737437 转载请注明出处   一.什么是cocoaPods 1.为 ...

  7. Unity2D开发小细节

    当某个触碰物体挂在父组件时,如果当前子组件不加rigidbody,会默认使用父组件的rigidbody

  8. 日期的本质是double

    日期的本质是double,在多语种时用起来更方便.

  9. CoreAnimation的使用

    一.CABasicAnimation CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"posi ...

  10. Redis Cluster 实践

    一:关于redis cluster 1:redis cluster的现状 reids-cluster计划在redis3.0中推出,可以看作者antirez的声明:http://antirez.com/ ...