zoukankan      html  css  js  c++  java
  • hdoj3371-Connect the Cities(prim)

    hdoj3371-Connct th cities
    Problem Description
    In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.

    Input
    The first line contains the number of test cases.
    Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
    To make it easy, the cities are signed from 1 to n.
    Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
    Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.

    Output
    For each case, output the least money you need to take, if it’s impossible, just output -1.

    Sample Input
    1
    6 4 3
    1 4 2
    2 6 1
    2 3 5
    3 4 33
    2 1 2
    2 1 3
    3 4 5 6

    Sample Output
    1

    思路:

    本题主要考查最小生成树,但是要注意的是:
    1.首先可能有重边的情况,所以需要选择重边的最小权值;
    2.然后会出现有城市已经连接的情况,所以我们可以把这些城市两点的权值设为0;

    code:

    //Exe.Time  Exe.Memory
    //  889MS    2732K
    #include <fstream>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    int data[505][505];
    int dist[505];
    int vis[505];
    int n, m, k;
    
    int prim()
    {
        int total_weight = 0;
        dist[1] = 0;
        for(int i = 1; i <= n; i ++)
        {
            int min_dist = INF, min_vertex;
            int ok = true;
            for(int j = 1; j <= n; j ++)
            {
                if(!vis[j] && min_dist > dist[j])
                {
                    min_dist = dist[j];
                    min_vertex = j;
                    ok = false;
                }
            }
            vis[min_vertex] = true;
            if(ok)
            {       //当不存在含未选择的顶点的边时return -1;
                //cout << min_vertex << endl;
                return -1;
            }
            total_weight += min_dist;
            for(int j = 1; j <= n; j ++)
            {
                if(!vis[j] && dist[j] > data[min_vertex][j])
                {
                    dist[j] = data[min_vertex][j];
                }
            }
        }
        return total_weight;
    }
    
    int main()
    {
    //  freopen("data.in", "r", stdin);
    //  ifstream cin("data.in");
        int t;
        scanf("%d", &t);
        //cin >> t;
        while(t --)
        {
            //int n, m, k;
            //cin >> n >> m >> k;
            scanf("%d%d%d", &n, &m, &k);
            memset(data, INF, sizeof(data));
            memset(dist, INF, sizeof(dist));
            memset(vis, 0, sizeof(vis));
            for(int i = 0; i < m; i ++)
            {
                int x, y, w;
                scanf("%d%d%d", &x, &y, &w);
                //cin >> x >> y >> w;
                //注意可能出现重边
                if(w >= data[x][y])
                {
                    continue;
                }
                data[x][y] = w;
                data[y][x] = w;
            }
            int temp[505];
            for(int i = 0; i < k; i ++)
            {
                int T;
                //cin >> T;
                int temp[505];
                scanf("%d", &T);
                for(int j = 0; j < T; j ++)
                {
                    scanf("%d", &temp[j]);
                }
                //将已连接的城市之间的权值设为0;
                for(int j = 0; j < T; j ++)
                {
                    for(int l = j + 1; l < T; l ++)
                    {
                        int x = temp[j];
                        int y = temp[l];
                        data[x][y] = 0;
                        data[y][x] = 0;
                    }
                }
            }
            printf("%d
    ", prim());
            //cout << prim() << endl;
        }   
    }
  • 相关阅读:
    frida枚举当前加载的模块以及模块中方法
    python request请求时候json严格校验怎么去除空格
    firda-so静态注册
    LeetCode 724. 寻找数组的中心索引
    LeetCode 679. 24点游戏
    LeetCode 845. 数组中的最长山脉
    并查集各种情况下的时间复杂度
    LeetCode 547. 省份数量
    LeetCode 5. 最长回文子串
    LeetCode 103. 二叉树的锯齿形层序遍历
  • 原文地址:https://www.cnblogs.com/topk/p/6580107.html
Copyright © 2011-2022 走看看