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 ...
随机推荐
- node 搭建开发框架express
参考地址: http://www.itnose.net/detail/6095003.html 开发环境 E:\project> node -v v0.10.30 E:\project> ...
- VMware复制Centos6虚拟机要改的地方
1.删除文件 /etc/udev/rules.d/70-persistent-net.rules (它会绑定你网卡信息) 2.重新配置 # vi /etc/sysconfig/network-scr ...
- if....else
if....else语句是在特定的条件下成立执行的代码,在不成立的时候执行else后面的代码. 语法: if(条件) {条件成立执行}else{条件不成立执行} 下面来写一个简单的实例 以考试成绩为例 ...
- opencv 常用函数介绍
××××××××××××××××××××××××××××××××××××××× CvScalar imgmean,imgstd; double imgmax,imgmin; cvAvgSdv(img, ...
- SGU 137.Funny String
题目描述 一个序列S1 S2 S3... Sn 如果满足 新序列 S1-1 S2 S3 ...Sn+1能够通过旋转的操作(不是翻转)来得到旧的序列,那么这个序列就叫做Funny序列.例如 1 2 1 ...
- 24种设计模式--代理模式【Proxy Pattern】
什么是代理模式呢?我很忙,忙的没空理你,那你要找我呢就先找我的代理人吧,那代理人总要知道被代理人能做哪些事情不能做哪些事情吧,那就是两个人具备同一个接口,代理人虽然不能干活,但是被代理的人能干活呀. ...
- 子元素的margin-top影响父元素原因和解决办法
这个问题会出现在所有浏览器当中,原因是css2.1盒子模型中规定, In this specification, the expression collapsing margins means tha ...
- php配置虚拟主机的配置步骤(hosts、httpd.conf、vhosts.conf)1.配置本地的dns文件2.配置apache的主配置文件3.配置Apache的虚拟主机
1.域名解析(DNS) 找到C:\Windows\System32\drivers\etc目录下的hosts文件,在里面进行添加对应的内容
- Chatwin商务通
<script language="javascript" src="http://lwt.zoosnet.net/JS/LsJS.aspx?siteid=你商务通 ...
- YII 创建后台模块
1,在protected/config/main.php目录下修改如下目录 'modules'=>array( // uncomment the following to enable the ...