zoj 3036 IP-TV
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
-
#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; }
-
zoj 2182 Cable TV Network
2020-02-12 关注 0 浏览128 1答案
-
zoj 2482 IP Address
2020-02-12 关注 0 浏览127 1答案
-
zoj 2645 IP Networks
2020-02-12 关注 0 浏览152 1答案
-
poj 3036 Honeycomb Walk
2020-02-12 关注 0 浏览138 1答案
-
半衰期3036小时,最常用于口服的强心苷是( )
2021-04-19 关注 0 浏览50 1答案
-
The Oriental Pearl TV Tower______
2022-05-22 关注 0 浏览28 1答案
-
poj 1966 Cable TV Network
2020-02-12 关注 0 浏览154 1答案
-
His mother wants to watch TV play _____.
2022-05-22 关注 0 浏览31 1答案
-
She__________Tv when I came in.
2022-05-22 关注 0 浏览16 1答案
-
The TV play we watched last night was very ________.
2022-05-23 关注 0 浏览18 1答案