All pastes #479592 Raw Edit

Someone

public text v1 · immutable
·published 2007-05-09 20:26 UTC
rendered paste body
#include<iostream>
#include<fstream>
#include<list>



using namespace std;

int root[256];


struct edges{
	char to, from;
	int weight;

	bool operator<(edges e){
		return (weight < e.weight);
	}
};

int getRoot(int l){

	if (root[l] == (-1))
		return l;

	return getRoot(root[l]);
};


bool join(int a, int b){
	
	int rootA = getRoot(a);
	int rootB = getRoot(b);

	if(rootA==rootB)
		return false;

	root[rootA] = rootB;
	return true;

};


int main(){
	list<edges> listOfEdges;

	for (int i = 0; i < 256 ; i++){
		root[i] = -1;
	};
	
	ifstream in("blah.txt");

	while (!in.eof()){
		edges e;
		in >> e.from >> e.to >> e.weight >> ws;
		listOfEdges.push_back(e);
	};

	listOfEdges.sort();
		

	for(list<edges>::iterator i = listOfEdges.begin(); i!=listOfEdges.end(); i++){
		
		if(join(i->from, i->to))
			cout << i->from << " " << i->to << " " << i->weight << endl;

	};



	return 0;

};