zoj 3036 IP-TV

发布于 2020-02-11 23:38:21

A consortium of European Internet providers manages a large backbone network, with direct links (connections) between a large number of European cities. A link between a pair of cities is bidirectional. The transmission of a message in a link has an associated cost. As it is common in the Internet, it is possible to use a(unbounded) sequence of direct links to indirectly transfer data between any pair of cities.

For allowing the broadcast of TV programs using this backbone, it is necessary to continuously send data to all nodes in the network. For helping to minimize costs, it is necessary to select the network links that will be used for transmitting data. The set of selected links must be connected and include all nodes in the network.

For helping the consortium to manage its network, you have been asked to create a program that computes the minimum cost for transmitting data to all cities of the backbone.

Given a set of network links, compute the minimum transmission cost for reaching all nodes.

Input

A positive integer P in a single line followed by a sequence of P test cases. The first line of the each test case contains a positive integer M, not greater than 2,000, with the number of cities that have network connections. The second line contains an integer N not greater than 50,000, with the number of existing links. Each of the following N lines contains the representation of a link. Each line contains two strings and one integer, separated by empty spaces, B E C, where B and E are the city names of the endpoints of the network link, with no more than 8 characters, and C is a positive integer, not greater than 30, representing the cost of transmitting in the link.

Output

The output consists of one single line that contains an integer with the minimum transmission cost for sending data to all cities.

Sample Input

1
4
5
lisbon london 6
lisbon paris 5
london paris 1
london berlin 2
paris berlin 10

Sample Output

8
关注者
0
被浏览
734
1 个回答
  • 面试哥
    面试哥 2020-02-11
    为面试而生,有面试问题,就找面试哥。
    #include <cstdio>
    #include <map>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    const int MAXN = 2010;
    const int MAXM = 50010;
    
    map<string, int> hash;
    int cnt;
    struct EE {
     int u, v, w;
    }ee[MAXM];
    int tot;
    bool cmp(const EE &cmp1, const EE &cmp2)
    {
     return cmp1.w < cmp2.w;
    }
    
    int fa[MAXN];
    
    int find(int x)
    {
     return fa[x] == x ? x : fa[x] = find(fa[x]);
    }
    
    int main()
    {
     int cn, cns;
     char str1[110], str2[110];
     int cost;
     string tmp1, tmp2;
     scanf("%d", &cns);
     for (cn = 0; cn < cns; cn++) {
     int n, m;
     scanf("%d%d", &n, &m);
     cnt = 0;
     tot = 0;
     hash.clear();
     for (int i = 0; i < m; i++) {
     scanf("%s%s%d", str1, str2, &cost);
     tmp1 = str1;
     tmp2 = str2;
     if (hash.find(tmp1) == hash.end()) {
     hash[tmp1] = cnt++;
     }
     if (hash.find(tmp2) == hash.end()) {
     hash[tmp2] = cnt++;
     }
     ee[tot].u = hash[tmp1];
     ee[tot].v = hash[tmp2];
     ee[tot].w = cost;
     tot++;
     }
     sort(ee, ee + tot, cmp);
    
     for (int i = 0; i < cnt; i++)
     fa[i] = i;
     int fx, fy;
     int ans = 0;
     for (int i = 0; i < tot; i++) {
     fx = find(ee[i].u);
     fy = find(ee[i].v);
     if (fx != fy) {
     fa[fx] = fy;
     ans += ee[i].w;
     }
     }
     printf("%d\n", ans);
     }
     return 0;
    }
推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看