【HDU3371】Connect the Cities

Connect the Cities

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

Total Submission(s): 12861    Accepted Submission(s): 3543

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

题意:大约就是给出了连通的和不连通的岛屿 不连通的给出了 维修所需的价格 最后求用最小的代价 得到所有的连通

题解:比较简单的最小生成树的问题  Prim和Kruskal都可以 但是Kruskal用C++提交会超时 应该是自己剪枝不够 Prime跑了500+ms 感觉还好吧 后面总结两种算法的时候我会专门提到两种方法的选择问题 先给出两种方法的AC代码

//看HDU上面大牛的耗时都是200-ms 可惜不清楚是怎么实现的

Kruskal:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 511;
int pi[maxn];
struct node{
    int p, q, c;
}no[maxn*maxn];

bool cmp(node a, node b);
int Find(int x);

int main(){
    int cas;
    scanf("%d", &cas);
    while(cas--){
        for(int i = 1; i <= 500; ++i){
            pi[i] = i;
        }
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 1; i <= m; ++i){
            scanf("%d%d%d", &no[i].p, &no[i].q, &no[i].c);
        }
        sort(no+1, no+m+1, cmp);

        int num = 0;
        int t, root, tmp;
        for(int i = 0; i < k; ++i){
            scanf("%d%d", &t, &root);
            for(int j = 1; j < t; ++j){
                scanf("%d", &tmp);
                int x = Find(tmp);
                int y = Find(root);
                if(x != y){
                    pi[x] = y;
                    ++num;
                }
            }
        }

        if(num == n-1){
            printf("0\n");
            continue;
        }
        //bool flag = false;
        int ans = 0;
        for(int i = 1; i <= m; ++i){
            int x = Find(no[i].p);
            int y = Find(no[i].q);
            if(x == y){
                continue;
            }
            pi[x] = y;
            ans += no[i].c;
            ++num;

            if(num == n-1){
                //flag = true;
                break;
            }
        }

        if(num == n-1){
            printf("%d\n", ans);
        }
        else{
            printf("-1\n");
        }
    }
    return 0;
}

bool cmp(node a, node b){
    return a.c < b.c;
}

int Find(int x){
    int r=x;
    while(pi[r]!=r)
        r=pi[r];
    return r;
}

Prim:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 511;
const int inf = 0x7ffffff;
int dist[maxn][maxn];
int low[maxn];
bool vis[maxn];
int v[maxn];

void Prim(int n);

int main(){
    int cas;
    scanf("%d", &cas);
    while(cas--){
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);

        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                if(i == j){
                    dist[i][j] = 0;
                }
                else{
                    dist[i][j] = inf;
                }
            }
        }

        while(m--){
            int p, q, c;
            scanf("%d%d%d", &p, &q, &c);
            dist[p][q] = dist[q][p] = min(dist[p][q], c);
        }

        while(k--){
            int t;
            scanf("%d", &t);
            for(int i = 0; i < t; ++i){
                scanf("%d", &v[i]);
            }
            for(int i = 0; i < t; ++i){
                for(int j = 0; j < t; ++j){
                    dist[v[i]][v[j]] = 0;
                }
            }
        }

        Prim(n);
    }
    return 0;
}

void Prim(int n){
    int ans = 0;

    for(int i = 1; i <= n; ++i){
        low[i] = dist[1][i];
        vis[i] = false;
    }

    vis[1] = true;

    int flag;

    for(int i = 2; i <= n; ++i){
        int mi = inf;
        flag = -1;
        for(int j = 2; j <= n; ++j){
            if(mi > low[j] && !vis[j]){
                mi = low[j];
                flag = j;
            }
        }

        if(mi >= inf){
            flag = -1;
            break;
        }

        ans += mi;
        vis[flag] = true;

        for(int j = 2; j <= n; ++j){
            if(dist[flag][j] < low[j] && !vis[j]){
                low[j] = dist[flag][j];
            }
        }
    }

    if(flag == -1){
        printf("-1\n");
    }
    else{
        printf("%d\n", ans);
    }

}

Prim有一个优先队列的优化 Shinelin做了一下但是G++时间比我直接用Prim久一点 后面如果找到更省时的方法再更新

时间: 2024-10-12 23:44:56

【HDU3371】Connect the Cities的相关文章

【HDU3371】Connect the Cities(MST基础题)

注意输入的数据分别是做什么的就好.还有,以下代码用C++交可以过,而且是500+ms,但是用g++就会TLE,很奇怪. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #incl

HDU3371 Connect the Cities 【最小生成树Kruskal】

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9856    Accepted Submission(s): 2800 Problem Description In 2100, since the sea level rise, most of the cities disappear. Thou

HDU3371 Connect the Cities【Kruskal】

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11152    Accepted Submission(s): 3155 Problem Description In 2100, since the sea level rise, most of the cities disappear. Tho

HDU 3371 Connect the Cities 【最小生成树,Prime算法+Kruskal算法】

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17167    Accepted Submission(s): 4335 Problem Description In 2100, since the sea level rise, most of the cities disappear. Tho

【LR11】Error -27796: Failed to connect to server"server:port": [10060] Connection timed out错误解决办法

  场景描述:被测系统是发布在远程服务器上的,假设IP是10.10.10.10,端口是8066,那么访问地址是http://10.10.10.10:8066/,在control机器上我设置了IP欺骗. 错误现象:在场景运行时出现大量Action.c(8): Error -27796: Failed to connect to server"server:port": [10060] Connection timed out错误. 官方的troubleshooting: 查看工具的tro

【MongoDB】2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接。

1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接. 2:添加一配置文件 ##数据文件 dbpath=E:\ruanjian\MongoDB\data ##日志文件 logpath=E:\ruanjian\MongoDB\log\mongo.log 添加相应目录和文件,如下图所示 3: C:\User

【MySQL】ould not connect, server may not be running. Can&#39;t connect to MySQL server on &#39;127.0.0.1&#39; (10

出现如下错误 21:11:36 Could not connect, server may not be running.Can't connect to MySQL server on '127.0.0.1' (10061) 解决方法很简单 打开Windows MySQL的服务即可 [MySQL]ould not connect, server may not be running. Can't connect to MySQL server on '127.0.0.1' (10

【转】ERROR 2003 (HY000): Can&#39;t connect to MySQL server on &#39;192.168.1.165&#39; (113)

原文转自:http://blog.csdn.net/chengyuqiang/article/details/54285857 1.程序报错: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link 2.尝试远程连接数据库: [[email protected] ~]# mysql -h192.168.1.165 -uroot -p123456 ERROR 2003 (HY000): Can't c

【Django】RROR 2003 (HY000): Can&#39;t connect to MySQL server on &#39;localhost&#39; (10061)

刚刚启动项目的时候,突然报了这个错误.之前一直正常 后来百度一下,让我在window的host文件下,把被注释的127.0.0.1   localhost这个的注释取消 然鹅并木有用 直接用cmd连接MySQL都失败了 后来去服务里面查,发现MySQL的服务没有启动(或者说不知道为啥停了...) 然后就正常了... [Django]RROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061) 原文地址:https: