HDUOJ---1102Constructing Roads
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12173 Accepted Submission(s): 4627
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.
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.
0 990 692
990 0 179
692 179 0
1
1 2
最小生成树....
代码:
//最小生成树....
//@Gxjun coder
#include<stdio.h>
#include<string.h>
const int inf=0x3f3f3f3f ,v=;
int vis[v],lowc[v];
int sta[v][v];
int prim(int cost[][v],int n)
{
int i,j,p;
int minc,res=;
memset(vis , , sizeof(vis));
vis[]=;
for(i= ; i<n ;i++)
lowc[i]=cost[][i];
for(i=;i<n;i++)
{
minc=inf;
p=-;
for(j= ; j<n ;j++)
{
if(==vis[j] && minc>lowc[j])
{
minc=lowc[j];
p=j;
}
}
if(inf==minc) return -; //原图不连通
res+=minc ;
vis[p]=;
for(j=; j<n ;j++)
{
if(==vis[j] && lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
}
return res;
} int main()
{
int tol,res,i,j;
while(scanf("%d",&tol)!=EOF)
{
for(i=;i<tol;i++)
{
for(j=; j<tol;j++)
{
scanf("%d",&sta[i][j]);
}
}
scanf("%d",&res);
int num1,num2;
for(i=;i<res;i++)
{
scanf("%d%d",&num1,&num2);
sta[num1-][num2-]=sta[num2-][num1-]=;
}
printf("%d\n",prim(sta,tol));
}
return ;
}
HDUOJ---1102Constructing Roads的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- hduoj 1455 && uva 243 E - Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...
- 杭电--1102--Constructing Roads--并查集
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
随机推荐
- java环境配置错误集锦
eclipse生成的文件目录 D:\eeworkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps 1.java. ...
- 让QT/Embedded支持国际化
让QT/Embedded支持国际化 环境配置: Qt/Embedded ,在主机和目标板上存放路径都为:/root/qt-embedded-free- Qt/X11 3.3 (主要用到其中的lupda ...
- MongoDB:如何正常关闭服务(转)
原文链接:MongoDB:关闭服务 MongoDB 提供几种关闭服务的命令,具体为以下: 一 使用 Crtl+C 关闭 [mongo@redhatB data]$ mongod --dbpath=/ ...
- 把Scala代码当作脚本运行
1. 在类UNIX系统上作为脚本运行 在类Unix系统上,你可以设置一个shell前导词来执行脚本.如下例: Script.scala #!/usr/bin/env scala !# println( ...
- Rotate List leetcode java
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- LA 4728 Square ,旋转卡壳法求多边形的直径
给出一些正方形.让你求这些正方形顶点之间的最大距离的平方. //返回点集直径的平方 int diameter2(vector<Point> & points) { vector&l ...
- 网络结构设计——负载均衡之LVS学习笔记(二)
LVS按个人理解的说就是将一台Linux服务器当作路由器等功能的技术.LVS---Linux虚拟服务器. LVS实现了三种IP负载均衡技术VS/NAT.VS/TUN.VS/DR. 今天简单分享一下我在 ...
- 第三章:ES分词简单说明
1. Index_Analyzer为索引时使用的分词器,search_Analyzer为搜索时使用的分词器 这个索引,对应的数据是下图: 数据格式是用%分隔的年份,"index_analyz ...
- ListView GridViewColumn.CellTemplate
<ListView.View> <GridView> <GridViewColumn Header="Meaningful Use Objectives&quo ...
- Linux上如何查看Deb和RPM软件包的更新日志
导读 当一个程序或库打包成Deb或RPM软件包后会有一些元数据文件包含在其中,其中之一就是 changelog文件,它记录了软件包每次更新后发生了什么变化.因此,如果你想找出你安装或更新的软件包发生了 ...