zoj 1586
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586







//zoj 1586
#include<iostream>
#include<cstdio>
using namespace std;
#define N 1005
int a[N][N],low[N],n,ans;
int b[N]; int min(int x,int y)
{ return x<y?x:y; } void prim(int u0)
{
int i,j,m,k;
ans=0;
for (i=1;i<n;i++) low[i]=a[u0][i];
low[u0]=-1;
for (i=1;i<n;i++)
{
m=1<<20;
for (j=0;j<n;j++)
if (low[j]!=-1&&low[j]<m)
{ m=low[j]; k=j; }
ans+=m;
low[k]=-1;
for (j=0;j<n;j++)
if (low[j]!=-1)low[j]=min(low[j],a[k][j]);
}
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&b[i]); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
a[i][j]+=b[i]+b[j];
}
}
prim(0);
cout<<ans<<endl;
}
return 0;
} *************************************************************************************************** #include<iostream>
#include<cstdio>
using namespace std;
#define N 1005
int a[N][N],low[N],n,ans;
int b[N]; int min(int x,int y)
{ return x<y?x:y; } void prim(int u0)
{
int i,j,m,k;
ans=0;
for (i=1;i<n;i++) low[i]=a[u0][i];
low[u0]=-1;
for (i=1;i<n;i++)
{
m=1<<20;
for (j=0;j<n;j++)
if (low[j]!=-1&&low[j]<m)
{ m=low[j]; k=j; }
ans+=m;
low[k]=-1;
for (j=0;j<n;j++)
if (low[j]!=-1)low[j]=min(low[j],a[k][j]);
}
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&b[i]); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(i=j) a[i][j]=1<<20; //对角线
else a[i][j]+=b[i]+b[j];
}
}
prim(0);
cout<<ans<<endl;
}
return 0;
} **************************************************************************888 #include<iostream>
#include<cstdio>
using namespace std;
#define N 1005
#define INF 1000000
int a[N][N],low[N],n,ans;
int b[N]; int min(int x,int y)
{ return x<y?x:y; } void prim(int u0)
{
int i,j,m,k;
ans=0;
for (i=1;i<n;i++) low[i]=a[u0][i];
low[u0]=-1;
for (i=1;i<n;i++)
{
m=1<<20;
for (j=0;j<n;j++)
if (low[j]!=-1&&low[j]<m)
{ m=low[j]; k=j; }
ans+=m;
low[k]=-1;
for (j=0;j<n;j++)
if (low[j]!=-1)low[j]=min(low[j],a[k][j]);
}
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&b[i]); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(i=j) a[i][j]=INF; //对角线
else a[i][j]+=b[i]+b[j];
}
}
prim(0);
cout<<ans<<endl;
}
return 0;
}
zoj 1586的更多相关文章
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- ZOJ - 1586 QS Network (Prim)
ZOJ - 1586 QS Network (Prim) #include<iostream> #include<cstring> using namespace std; + ...
- (最小生成树)QS Network -- ZOJ --1586
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 http://acm.hust.edu.cn/vjudge/ ...
- E - QS Network - zoj 1586(简单)
题意:在一个叫做QS的星系,他们使用一些特殊的通讯方式,两个人之间通讯需要使用一个网络适配器,但是一个网络适配器只能跟一个人联系,所有它连接几个人就需要及格适配器,而且每个人都有一些不同的偏好,喜欢的 ...
- zoj 1586 QS Network
最小生成树,刚刚学了Prim算法. 对每条边变的权值进行预处理,c[i][j] = c[i][j] + p[i] + p[j] 其中c[i][j]为输入的权值,p[i],p[j]为连接这两个节点所需的 ...
- 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 板子题
#include<iostream> #include<algorithm> using namespace std; ; struct edge{ int a,b; doub ...
随机推荐
- C#生成缩略图不清晰模糊问题的解决方案!
之前网上找了个生成缩略图的代码,改了改直接用了.问题来了,等比例缩略图时总是发现左边.上边的边线大概有一像素的白边,领导不乐意了,那咱就改吧.图片放大了才发现,那个好像是渐变的颜色,晕,这样的功能领导 ...
- python爬虫scrapy的Selectors参考文档
http://doc.scrapy.org/en/1.0/topics/selectors.html#topics-selectors-htmlcode
- iOS_ruby环境的配置
AC机中安装RUBY环境 转自:http://www.cnblogs.com/foxting/p/4520829.html 在安装CocoaPods之前要先配置好RUBY环境,本文就怎么安装RUBY ...
- 让div 实现 input效果
<div class="arcils-info" c /> .arcils-info { border: none; width: 100%; font-size: 1 ...
- socket.io 实例
//引用 var io = require('socket.io')(server); //server io.on('connection', function(socket) { // ...
- OpenGL光源位置
一.OpenGL光源简介 OpenGL提供了多种形式的光源,如点光源.平行光源和聚光灯光源等.所有光源都使用 glLight*接口来设置光源属性,其中包括 glLight{if} 和 glLight{ ...
- DX笔记之一---Direct3D基础
一.预备知识 1.表面 表面就是Direct3D用于储存2D图像数据的一个像素矩阵.width和height以像素为单位,pitch以字节单位,用接口IDirect3DSurface来描述表面 Loc ...
- phpcms(4) V9 栏目管理
phpcms V9框架系统后台管理之栏目管理,请参见下文的源码分析(添加栏目和修改栏目): 参照添加栏目的界面图示,便于对源代码的理解: <?php // 文件路径:phpcms/modul ...
- php基础知识【函数】(9)数学和对象类函数
一.数学 abs -- 绝对值 ceil -- 进一法取整 floor -- 舍去法取整 fmod -- 返回除法的浮点数余数 round -- 对浮点数进行四舍五入 sqrt -- 平方根 pi( ...
- python自动开发之(django)第十九天
一.路由系统,URL 1.函数及类 函数:url(r'^index/', views.index), 类:url(r'^home/', views.Home.as_view()), 2.顺序 url( ...