poj_2421_mst
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179 刚开始用kruskal,结果tle 了,可能是我的代码写挫了吧。改为prim,wa了,改了cost[u][v]=cost[v][u]
#include<cstdio>
#include<cstring>
const int INF=0x3f3f3f;
const int MAXN=110;//看清上限
bool vis[MAXN];
int lowc[MAXN];
int cost[MAXN][MAXN];
int prim(int n)
{
memset(vis,false,sizeof(vis));
int ans=0;
vis[1]=true;
for(int i=2;i<=n;i++)
lowc[i]=cost[1][i];
for(int i=2;i<=n;i++)//我开始这里写成了i=1;当然一直是return -1;
{
int minc=INF;
int p=-1;
for(int j=1;j<=n;j++)
if(!vis[j]&&lowc[j]<minc)
{
minc=lowc[j];
p=j;
}
if(minc==INF)
return -1;
ans+=minc;
vis[p]=true;
for(int j=1;j<=n;j++)
if(!vis[j]&&cost[p][j]<lowc[j])
lowc[j]=cost[p][j];
}
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int w;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cost[i][j]=INF;
for(int i=1;i<=n;i++)
{
lowc[i]=INF;
for(int j=1;j<=n;j++)
{
scanf("%d",&w);
if(w<cost[i][j])//只保存最小权值
cost[i][j]=w;
}
}
int m;
scanf("%d",&m);
int u,v;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
cost[u][v]=cost[v][u]=0;//这里不能只写cost[u][v]=0;
}
int ans=prim(n);
printf("%d\n",ans);//我忘了要\n
}
return 0;
}
poj_2421_mst的更多相关文章
随机推荐
- java 四种内部类和内部接口
/** * 常规内部类:常规内部类没有用static修饰且定义在在外部类类体中. * 1.常规内部类中的方法可以直接使用外部类的实例变量和实例方法. * 2.在常规内部类中可以直接用内部类创建对象 * ...
- myeclipse10安装egit和使用
一.下载egit插件并安装到eclipse 下载egit插件包,然后解压放到Eclipse的dropins文件夹内或者直接放到对应的文件夹下 二.安装成功(window->preferences ...
- java CS结构软件自动升级的实现
前段时间做了一个工具发布给公司的各部门使用后反馈了不少BUG,每次修改后均需要发邮件通知各用户替换最新版本,很不方便,因此后来就写了一个自动升级的功能,这样每次发布新的版本时只需要将其部署到自动升级服 ...
- windows的页自映射机制
windows下由于启用了页机制,所有软件层面的地址操作都是VA,通过descriptor(base address(32bit))+offset得到的线性地址并不直接对应物理地址,而是经过页转换机构 ...
- Performance Analysis Methodology
http://www.brendangregg.com/methodology.html The USE Method: for finding resource bottlenecks The TS ...
- HTTP头详解
HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Chars ...
- js字符串函数
JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...
- 发现PDF Transformer+转换的图像字体小了如何处理
ABBYY PDF Transformer+转换的原始图像字体太小怎么办?为了获得最佳文本识别效果,请用较高的分辨率扫描用极小字体打印的文档,否则很容易在转换识别时出错.下面小编就给大家讲讲该怎么解决 ...
- oracle sqlplus及常用sql语句
常用sql语句 有需求才有动力 http://blog.csdn.net/yitian20000/article/details/6256716 常用sql语句 创建表空间:create tables ...
- proBuilder编辑的模型变黑
----更正: 旧帖中方法有误,解决不了问题. 更正确法: 将proBuilder创建的模型的Static属性由“-”改为去掉勾选: ----旧帖 proBuilder编辑的模型变黑解法: 1,U ...