Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. using namespace std;
  6. typedef struct Edge{
  7. int weight;
  8. int beg, end;
  9. }*PEdge;
  10.  
  11. void heapify(Edge E[], int heapsize, int parentindex){
  12. int maxIndex = parentindex;
  13. int leftChild = parentindex*2+1;
  14. int rightChild = parentindex*2+2;
  15.  
  16. if(leftChild < heapsize && E[leftChild].weight > E[maxIndex].weight)
  17. maxIndex=leftChild;
  18. if(rightChild < heapsize && E[rightChild].weight > E[maxIndex].weight)
  19. maxIndex=rightChild;
  20. if(maxIndex != parentindex){
  21. swap(E[parentindex],E[maxIndex]);
  22. heapify(E,heapsize,maxIndex);
  23. }
  24.  
  25. }
  26. void heap_sort(Edge E[],int heapsize){
  27.  
  28. for(int i=heapsize/2 - 1;i>=0;i--){
  29. heapify(E,heapsize,i);
  30. }
  31. for(int i=heapsize-1;i>=0;i--){
  32. swap(E[0],E[i]);
  33. heapify(E,--heapsize,0);
  34. }
  35. }
  36. void kruskal(int n, int m, Edge E[], PEdge F[]){
  37. PEdge e; int p, q, j=0,int X[50];
  38. heap_Sort(E,m*2);
  39. for(int i=1;i<=n;i++){
  40. X[i]=i;
  41. }
  42.  
  43. int main(){
  44. int n,m,i=0,number,row=0,edge=0,dest,weight;
  45. ifstream in;
  46. ofstream out;
  47. string line;
  48. in.open("In0303.txt");
  49. out.open("Out0303.txt");
  50. if(!in || !out)
  51. return 0;
  52. in>>n>>m;
  53. Edge E[34];
  54. while (! in.eof() ){
  55. getline( in, line );
  56. istringstream is( line );
  57. while( is >> dest >> weight ) {
  58. edge++;
  59. E[edge].beg = row;
  60. E[edge].end = dest;
  61. E[edge].weight = weight;
  62. }
  63. row++;
  64. }
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement