[UVA] 1174 - IP-TV
Background
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.
Problem
Given a set of network links, compute the minimum transmission cost for reaching all nodes.
Input
Input consists of multiple test cases the first line of the input contains the number of test cases. There is a blank line before each dataset. The first line of each dataset 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. Print a blank line between datasets.
Sample Input
1
4
5
lisbon london 6
lisbon paris 5
london paris 1
london berlin 2
paris berlin 10
Sample Output
8作法 : 最小生成樹
不會寫 HASH, 犧牲一下記憶體做個 Trie
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
int x, y, v;
}Path;
Path D[50001];
int cmp(const void *i, const void *j) {
Path *a, *b;
a = (Path *)i, b = (Path *)j;
return a->v - b->v;
}
struct {
int son[128];
int code;
}Trie[60001];
int TrieSize;
int Recode(int *n, char *name) {
static int i, j;
for(i = 0, j = 0; name[i]; i++) {
if(!Trie[j].son[name[i]])
Trie[j].son[name[i]] = ++TrieSize;
j = Trie[j].son[name[i]];
}
if(!Trie[j].code)
Trie[j].code = (*n)++;
return Trie[j].code;
}
int Parent[2001], Rank[2001];
int MakeSet(int N) {
static int i;
for(i = 1; i <= N; i++)
Parent[i] = i, Rank[i] = 1;
}
int FindP(int x) {
if(x != Parent[x])
Parent[x] = FindP(Parent[x]);
return Parent[x];
}
int Link(int x, int y) {
if(Rank[x] < Rank[y])
Parent[x] = y, Rank[x] += Rank[y];
else
Parent[y] = x, Rank[y] += Rank[x];
}
int Union(int x, int y) {
x = FindP(x), y = FindP(y);
if(x != y) {
Link(x, y);
return 1;
}
return 0;
}
int main() {
int T, M, N, C, size, i, x, y;
char B[31], E[31];
scanf("%d", &T);
while(T--) {
size = 1, TrieSize = 0;
scanf("%d %d", &M, &N);
memset(Trie, 0, sizeof(Trie));
for(i = 0; i < N; i++) {
scanf("%s %s %d", B, E, &C);
x = Recode(&size, B);
y = Recode(&size, E);
D[i].x = x, D[i].y = y, D[i].v = C;
}
qsort(D, N, sizeof(Path), cmp);
MakeSet(M);
int Ans = 0;
for(i = 0; i < N; i++)
Ans += D[i].v * Union(D[i].x, D[i].y);
printf("%d\n", Ans);
if(T) puts("");
}
return 0;
}