Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12173    Accepted Submission(s): 4627

Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

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
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

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
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum. 
 
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
 
Sample Output
179
 
Source
 

最小生成树....

代码:

 //最小生成树....
//@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的更多相关文章

  1. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  2. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  4. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  5. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  6. 杭电--1102--Constructing Roads--并查集

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  8. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  9. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  10. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

随机推荐

  1. matlab 投影

    function[l]= Gray(I) % I: The name of image A=imread(I);m=0;n=0;[m,n]= size(A);Hproj=zeros(m,1);Vpro ...

  2. 《深入理解C#(第3版)》

    <深入理解C#(第3版)> 基本信息 原书名:C# in depth 作者: (英)Jon Skeet 译者: 姚琪琳 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:978 ...

  3. spring IOC的常见几种以来注入的方式

    在spring ioc中有三种依赖注入,分别是:a.接口注入:b.setter方法注入:c.构造方法注入: 接口注入: public class ClassA { private InterfaceB ...

  4. 【BZOJ】【3239】Discrete Logging

    BSGS BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p) /******************** ...

  5. 【屌丝程序的口才逆袭演讲稿50篇】第十三篇:爱迪生欺骗了我们!【张振华.Jack】

    演讲稿主题:<爱迪生欺骗了我们>                      --作者:张振华Jack.摘抄<马云为雅虎员工的演讲稿:爱迪生欺骗了我们> 非常多人都记得爱迪生说的 ...

  6. 树行控件TreeView 在WinForm下 怎么实现重命名功能

    public Form1() {     InitializeComponent();     this.Load+=new EventHandler(Form1_Load);     treeVie ...

  7. 深入理解模型,视图和控制器(C#)

    这篇文章向你提供ASP.NET MVC 模型,视图和控制的高度概览.换句话说,解释一下ASP.NET MVC中的 ‘M’, ‘V’, 和 ‘C’. 看完这篇文章以后,你应该就能理解ASP.NET MV ...

  8. Socket.IO介绍:支持WebSocket、用于WEB端的即时通讯的框架

    一.基本介绍 WebSocket是HTML5的一种新通信协议,它实现了浏览器与服务器之间的双向通讯.而Socket.IO是一个完全由JavaScript实现.基于Node.js.支持WebSocket ...

  9. windows10(64位)Anaconda3+Python3.6搭建Tensorflow(cpu版本)及keras

    转自:windows10(64位)Anaconda3+Python3.6搭建Tensorflow(cpu版本)及keras 1.本来电脑安装的是anaconda3 5.3.1,但安装的python版本 ...

  10. SQL Server-已更新或删除的行值要么不能使该行成为唯一行,要么改变了多个行

    在更新没有设置主键的表的时候出现下图中的问题: 问题原因: 这种问题大多是由于没有主键(PK)导致同一张表中存在若干条相同的数据 DBMS存储时,只为其存储一条数据,因为DBMS底层做了优化,以减少数 ...