acm algorithm practice Dec. 27 MST
what makes me amused joyful is I use emacs coding this algorithm..:)
although i am not proficient in it at all.
besides, today I installed aqumacs in place of the built-in emacs of Mac. thereby(thus) reviewed the bash command and the profile .bash_profile
------------------------------------------------
poj 1258 Agri-Net
Input
Output
------------------------------------------------------
it's a basic MST problem using Kruskac's algorithm. (since Prime algorithm is kinda like djstrak algorithm which is practiced yesterday :)).
both the data structure and the algorithm are quite simple.
the key of K algorithm is Union-find & union
the code is:
edges.sort(cmp);
for(list<edge>::iterator it = edges.begin(); it != edges.end(); it++){
//if find set
int x = (*it).from;
int y = (*it).to;
while(vertices[x].parent != x){
x = vertices[x].parent; // climb up
}
while(vertices[y].parent != y){
y = vertices[y].parent; //climb up
}
if(x == y){//reject
continue;
}else{
//put the edge into A
A += (*it).weight;
//flip, now the x, y is the ROOT !!!!!
if(vertices[x].size <= vertices[y].size){
vertices[x].parent = y; // x's parent is set to y
vertices[y].size = vertices[x].size + vertices[y].size;
}else{
vertices[y].parent = x; // y's parent is set to x
vertices[x].size = vertices[x].size + vertices[y].size;
}
}
//else do nothing
}
the caveat doesn't belong to the algorithm issue... at first, I can not pass poi until I change the input
while (cin >> N) {}
. the instruction itself doesn't mention this input format :( and thank God I didn't change my original implementation of algorithm, or it would be quite a waste of time ....
---------------------------------------------