Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- using namespace std;
- typedef struct Edge{
- int weight;
- int beg, end;
- }*PEdge;
- void heapify(Edge E[], int heapsize, int parentindex){
- int maxIndex = parentindex;
- int leftChild = parentindex*2+1;
- int rightChild = parentindex*2+2;
- if(leftChild < heapsize && E[leftChild].weight > E[maxIndex].weight)
- maxIndex=leftChild;
- if(rightChild < heapsize && E[rightChild].weight > E[maxIndex].weight)
- maxIndex=rightChild;
- if(maxIndex != parentindex){
- swap(E[parentindex],E[maxIndex]);
- heapify(E,heapsize,maxIndex);
- }
- }
- void heap_sort(Edge E[],int heapsize){
- for(int i=heapsize/2 - 1;i>=0;i--){
- heapify(E,heapsize,i);
- }
- for(int i=heapsize-1;i>=0;i--){
- swap(E[0],E[i]);
- heapify(E,--heapsize,0);
- }
- }
- void kruskal(int n, int m, Edge E[], PEdge F[]){
- PEdge e; int p, q, j=0,int X[50];
- heap_Sort(E,m*2);
- for(int i=1;i<=n;i++){
- X[i]=i;
- }
- int main(){
- int n,m,i=0,number,row=0,edge=0,dest,weight;
- ifstream in;
- ofstream out;
- string line;
- in.open("In0303.txt");
- out.open("Out0303.txt");
- if(!in || !out)
- return 0;
- in>>n>>m;
- Edge E[34];
- while (! in.eof() ){
- getline( in, line );
- istringstream is( line );
- while( is >> dest >> weight ) {
- edge++;
- E[edge].beg = row;
- E[edge].end = dest;
- E[edge].weight = weight;
- }
- row++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement