Constructing Roads POJ - 2421 最小生成树板子题

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=110;
int p[N];
struct edge{
    int a;
    int b;
    int w;
}e[N*N];
int map[N][N],flag[N][N],num,n;
bool cmp(edge a,edge b)
{
    return a.w<b.w;
}
int find(int x)
{
    if(p[x]!=x)
        p[x]=find(p[x]);
    return p[x];
}
int main()
{
    while(cin>>n&&n)
    {
        memset(flag,0,sizeof flag);
        memset(map,0,sizeof map);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                cin>>map[i][j];
        int m,x,y;
        cin>>m;
        while(m--)
        {
            cin>>x>>y;
            flag[x][y]=1;
        }
        num=0;
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                if(flag[i][j])
                {
                    e[num].a=i;
                    e[num].b=j;
                    e[num++].w=0;
                }
                else
                {
                    e[num].a=i;
                    e[num].b=j;
                    e[num++].w=map[i][j];
                }
        sort(e,e+num,cmp);
        for(int i=1;i<=n;i++)
            p[i]=i;
        int sum=0;
        for(int i=0;i<num;i++)
        {
            int a=find(e[i].a);
            int b=find(e[i].b);
            int w=e[i].w;
            if(a!=b)
            {
                p[a]=b;
                sum+=w;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12240099.html

时间: 2024-10-11 19:43:07

Constructing Roads POJ - 2421 最小生成树板子题的相关文章

Constructing Roads POJ - 2421

题目链接:https://vjudge.net/problem/POJ-2421 思路:一些村庄,建一些路,使得所有村庄能相连,而且使得所有路长度之和最短. 题目说了,有些村庄之间已经建了路,说明有些路我们不需要建,那么在预处理的时候 把那些已经建过边的两个村庄的距离赋值为0,那么在跑最小生成树板子的时候就完成了 一些路已经建立的情况. 1 #include <stdio.h> 2 #include <iostream> 3 #include <queue> 4 usi

(最小生成树)Constructing Roads -- poj -- 2421

链接: http://poj.org/problem?id=2421 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 110; const int INF = 0xfffffff; int n, J[N][N], dist[N], vis[N]; int Prim() { i

Networking POJ - 1287 最小生成树板子题

#include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge{ int a,b,w; }e[N]; bool cmp(edge a,edge b) { return a.w<b.w; } int p[N]; int find(int x) { if(p[x]!=x) p[x]=find(p[x]); return p[x]; } int main() { int

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e

POJ - 2421(最小生成树.prim)

题目: 题目链接: http://poj.org/problem?id=2421 Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27947 Accepted: 12316 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e

HDU 1102 Constructing Roads【简单最小生成树,Prime算法+Kruskal算法】

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

Constructing Roads(1102 最小生成树)

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

poj 1251 最小生成树模板题

Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E 44E 2 F 60 G 38F 0G 1 H 35H 1 I 353A 2 B 10 C 40B 1 C 200Sample Output 21630 prim算法 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <

poj 2421

// poj 2421 prim/*最小生成树,用Prim算法有n个城镇,已知每两个城镇的距离,其中某些城镇之间的道路已经修好,要求用最少的路径修完剩下的城镇之间的路*/ #include<stdio.h>#define InF 2000int g[110][110],bz[110],low[110];int min,ans=0,Q,n,a,b; void prim(){ int i,j,k; bz[1]=1,j=1; for (i=2;i<=n;i++) { low[i]=g[1][i