最小生成树,刚刚学了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. [ios2]警告:Block的Retain Cycle的解决方法 【转】

    <span style="background-color: rgb(248, 248, 248); font-family: 'PT Sans', Geogia, Baskervil ...

  2. Testlink1.9.14介绍及使用

    Testlink简介  今天在为部门培训testlink的使用,顺便把相关片段记录下来. TestLink是一款开源的测试管理工具,主要用于进行测试过程的管理,通过使用TestLink提供的功能,可以 ...

  3. 类xml数据格式解析

    需要解析一种类xml的数据文件,数据格式1如下: <head> //文件头 <type>xtype</type> <condition> key1=va ...

  4. 记一次kali和win8.1的双系统修复!!

    简要情况: 原来电脑存在的系统:win7和kali. 后来的系统:win8.1和原本的kali 情况描述:在我装完win8.1后就直接启动到win8.1了没有了grub2的选择启动界面,但是我还是想要 ...

  5. javascript 事件委托 和jQuery事件绑定on、off 和one

    一. 事件委托什么是事件委托?用现实中的理解就是:有100 个学生同时在某天中午收到快递,但这100 个学生不可能同时站在学校门口等,那么都会委托门卫去收取,然后再逐个交给学生.而在jQuery 中, ...

  6. find中的-print0和xargs中-0的区别

    默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...

  7. ios下点击穿透focus获取问题

    在ios下的浏览器中当点击当前页的一个按钮,用window.location.href进行跳转时,如果下一个页面里这点击按钮的位置是一个textarea或者text等那么他会触发focus事件,会出现 ...

  8. 关于Python2字符编码的体会

    对于Python的字符编码问题也懵了很久,最近做爬虫多次遇到网页转码的问题,干脆彻底解决掉!Just Do it! 1.两种类型str与unicode str和unicode都是basestring的 ...

  9. Chapter 20_1 table库

    table库是由一些辅助函数构成,把table作为数组来操作,所有的函数都忽略传入参数的那张表中的非数字键. 无论如何,若一个操作需要取表的长度,这个表必须是一个真序列,或是拥有__len元方法. 提 ...

  10. wpf listview 行变色

    <ListView x:Name="listView_Date" Grid.Row="3" BorderBrush="{x:Null}" ...