hdu oj1102 Constructing Roads(最小生成树)

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 13995    Accepted Submission(s): 5324

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

kicc

这道题和之前做的那道题类似,http://blog.csdn.net/whjkm/article/details/38471187 直接用prim再次水过 ,思想也和那道题类似,就是把两个已经连通的点之间的距离设为0就行了,然后再直接用prim水过;

下面是代码:

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=101;
const int Max=0x3f3f3f3f;
int map[maxn][maxn],low[maxn],visit[maxn];
int n;
int prim()
{
    int i,j,pos,min,mst=0;
    memset(visit,0,sizeof(visit));
    pos=1;
    visit[1]=1;
    for(i=1;i<=n;i++)
        low[i]=map[pos][i];
    for(i=1;i<n;i++)
    {
        min=Max;
        for(j=1;j<=n;j++)
        {
            if(!visit[j] && min>low[j])
            {
                min=low[j];
                pos=j;
            }
        }
        mst+=min;
        if(mst>=Max) break;//说明这个图不连通
        visit[pos]=j;
        for(j=1;j<=n;j++)
        {
            if(!visit[j] && low[j]>map[pos][j])//更新low数组
                low[j]=map[pos][j];
        }
    }
    return mst;
}
int main()
{
    int q,i,j;
    int a,b;
    while(scanf("%d",&n)!=EOF)
    {
        memset(map,Max,sizeof(map));
        for(i=1;i<=n;i++)
          for(j=1;j<=n;j++)
            scanf("%d",&map[i][j]);
        scanf("%d",&q);
       while(q--)
       {
           scanf("%d%d",&a,&b);
           map[a][b]=map[b][a]=0;
       }
       printf("%d\n",prim());
    }
    return 0;
}

hdu oj1102 Constructing Roads(最小生成树),布布扣,bubuko.com

时间: 2024-12-25 06:38:12

hdu oj1102 Constructing Roads(最小生成树)的相关文章

HDU 1102 Constructing Roads (最小生成树)

最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1表示原图不连通 */ const int INF = 0x3f3f3f3f; const int MAXN = 110; bool vis[MAXN]; int lowc[MAXN]; int map[MAXN][MAXN]; int Prim(int cost[][MAXN], int n) {

hdu 1102 Constructing Roads 最小生成树Prim算法AC 水~~~

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15230    Accepted Submission(s): 5826 Problem Description There are N villages, which are numbered from 1 to N, and you should

hdu 1102 Constructing Roads(最小生成树 Prim)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 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 conne

HDU 1102 Constructing Roads, Prim+优先队列

题目链接:HDU 1102 Constructing Roads Constructing Roads 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 conne

HDU 1102 Constructing Roads (裸的并查集)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13210    Accepted Submission(s): 4995 Problem Description There are N villages, which are numbered from 1 to N, and you should

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom   LIS 简单题 好题 超级坑

Constructing Roads In JGShining's Kingdom Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) whi

!HDU 1025 Constructing Roads In JGShining&#39;s Kingdom--DP--(LIS算法)

题意:在马路两边分别有n个城市,给出期望的n条路用于连接两边的城市,但是要求路不能有交叉,求在期望的n条中路实际能保留下来的最大的条数 分析:这题很好 1.本题抽象出来的模型应该是求最长上升(不下降)子序列 2.LIS的 nlog(n)算法: O(n^2) 的算法是dp[i]保留以i结尾的最长上升子序列的长度,令k=dp[i],O(nlog(n))算法是从k的角度出发,设d(k)为在长度为 k 的序列中的最小的位置,即:d(k)=min(a[i]),其中 f[i]=k,然后二分,每次看a[i]是

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom (DP)

Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we ca

hdu 1025 Constructing Roads In JGShining&#39;s Kingdom(二分法+最长上升子序列)

题目大意:河的两岸有两个不同的国家,一边是穷国,一边是富国,穷国和富国的村庄的标号是固定的,穷国要变富需要和富国进行交流,需要建桥,并且建的桥不能够有交叉.问最多可以建多少座桥.      思路:建路时如下图所示                 当一边的点已经固定了的时候,另外一边按照从小到大的序列与当前的边连接,得到最少的交叉.           题目给的第二组测试数据,如果按照图一则可以建2座桥,图二建一座桥 3 1 2 2 3 3 1