zoukankan      html  css  js  c++  java
  • POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/124434#problem/D

    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


    ##题意: 求最小花费使得所有点联通.
    ##题解: 裸的最小生成树. 读入邻接矩阵后再建图. 对于已经联通的边,直接把它们的距离赋成0. (即能用就优先用).
    ##代码: ``` cpp #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 110 #define mod 100000007 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std;

    struct node{
    int left,right,cost;
    }road[maxn*maxn];

    int cmp(node x,node y) {return x.cost<y.cost;}
    int p[maxn],m,n;
    int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}
    int kruskal()
    {
    int ans=0;
    for(int i=1;i<=n;i++) p[i]=i;
    sort(road+1,road+m+1,cmp);
    for(int i=1;i<=m;i++)
    {
    int x=find(road[i].left);
    int y=find(road[i].right);
    if(x!=y)
    {
    ans+=road[i].cost;
    p[x]=y;
    }
    }
    return ans;
    }

    int dis[maxn][maxn];

    int main(int argc, char const *argv[])
    {
    //IN;

    while(scanf("%d", &n) != EOF)
    {
        m = 0;
        memset(road,0,sizeof(road));
    
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=n; j++) {
                scanf("%d", &dis[i][j]);
            }
        }
    
        int q; cin >> q;
        while(q--) {
            int x,y; scanf("%d %d", &x,&y);
            dis[x][y] = dis[y][x] = 0;
        }
    
        for(int i=1; i<=n; i++) {
            for(int j=i+1; j<=n; j++) {
                road[++m].left = i;
                road[m].right = j;
                road[m].cost = dis[i][j];
            }
        }
    
        int ans=kruskal();
    
        printf("%d
    ", ans);
    }
    
    return 0;
    

    }

  • 相关阅读:
    用户场景分析
    团队项目个人工作总结(4月22日)
    团队项目个人工作总结(4月21日)
    团队项目个人工作总结(4月20日)
    第九周学习进度情况
    站立会议10-个人总结
    站立会议9-个人总结
    站立会议8-个人总结
    textarea中文提交乱码问题解决
    站立会议7-个人总结
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5719318.html
Copyright © 2011-2022 走看看