#include <iostream> #include <algorithm> #include "message.h" #include "skup.h" using namespace std; int main() { short num;//number of companies num=NumberOfCompanies(); long *cost=new long[num]; for(short i=0; i<num; i++) cost[i]=GetShareCost(i); short id=MyNodeId(); if(id==0) { short *link=new short[num]; short unlinked=0; for(short i=1; i<num; i++) { link[i]=0; PutChar(i,'a'); Send(i); Receive(i); if(GetChar(i)!=0) { link[i]=i; } else { unlinked++; } } for(short i=1; i<num; i++) { if(link[i]&&unlinked) { PutChar(i,'f'); PutInt(i,unlinked); for(short j=1; j<num; j++) { if(!link[i]) PutInt(i,j); } Send(i); Receive(i); if(GetChar(i)=='F') { int j=-1; while(j) { j=GetInt(i); link[j]=i; unlinked--; } } } }/* for(short i=1; i<num; i++) { PutChar(link[i],'g'); PutInt(link[i],i); Send(link[i]); Receive(link[i]); int cost_len=GetInt(link[i]); } PutChar(link[g_id],'g'); PutInt(link[g_id],g_id); Send(link[g_id]); Receive(link[g_id]); int num_of_el=GetInt(link[g_id]); PutInt(id_master2,num_of_el); for(int i=0; i<num_of_el; i++) { PutInt(id_master2,GetInt(link[g_id])); } Send(id_master2);*/ long long total_len=num; for(short i=1; i<num; i++) { PutChar(link[i],'l'); Send(link[i]); Receive(link[i]); total_len+=GetInt(link[i]); } long long *cost_all=new long long[total_len]; long long curr_index=0; for(curr_index; curr_index<num; curr_index++) cost_all[curr_index]=cost[curr_index]; for(short i=1; i<num; i++) { PutChar(link[i],'g'); PutInt(link[i],i); Send(link[i]); Receive(link[i]); int cost_len=GetInt(link[i]); for(int i=0; i<cost_len; i++) { cost_all[curr_index]=GetInt(link[i]); curr_index++; } } sort(cost_all,cost_all+total_len); long long output=0; int iterat=0; for(total_len; total_len>0; total_len--) { output+=cost_all[0]*total_len; iterat++; } cout<<output; } else { Receive(0); PutChar(0,GetChar(0)); Send(0); bool done=false; short *link=new short[num]; for(short i=1; i<num; i++) link[i]=0; int id_master=-1; int nodes_left=0; while(!done) { int id_master2=Receive(-1); if(id_master==-1) id_master=id_master2; char mess1=GetChar(id_master2); if(mess1=='f') { int len=GetInt(id_master2); int *ids=new int[len]; bool *ids_b=new bool[len]; for(int i=0; i<len; i++) { ids[i]=GetInt(id_master2); } int unlinked=0; for(int i=0; i<len; i++) { PutChar(ids[i],'b'); Send(ids[i]); Receive(ids[i]); if(GetChar(ids[i])!=0) { link[ids[i]]=ids[i]; nodes_left++; } else { unlinked++; ids_b[i]=false; } } for(int i=0; i<len; i++) { if(ids_b[i]&&unlinked) { PutChar(ids[i],'f'); PutInt(ids[i],unlinked); for(short j=0; j<len; j++) { if(!ids_b[j]) PutInt(ids[i],ids[j]); } Send(ids[i]); Receive(ids[i]); if(GetChar(ids[i])=='F') { int j=-1; while(j) { j=GetInt(ids[i]); link[j]=ids[i]; ids_b[j]=true; unlinked--; nodes_left++; } } } } PutChar(id_master2,'F'); for(int i=0; i<len; i++) { if(ids_b[i]) { PutInt(id_master2,ids[i]); } } PutInt(id_master2,0); Send(id_master2); } else if(mess1=='g') { int g_id=GetInt(id_master2); if(g_id==id) { PutInt(id_master2,num); for(int i=0; i<num; i++) { PutInt(id_master2,cost[i]); } Send(id_master2); nodes_left--; if(!nodes_left) return 0; } else { PutChar(link[g_id],'g'); PutInt(link[g_id],g_id); Send(link[g_id]); Receive(link[g_id]); int num_of_el=GetInt(link[g_id]); PutInt(id_master2,num_of_el); for(int i=0; i<num_of_el; i++) { PutInt(id_master2,GetInt(link[g_id])); } Send(id_master2); nodes_left--; if(!nodes_left) return 0; } } } } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #include <iostream> #include <algorithm> #include "message.h" #include "skup.h" using namespace std; int main() { short num;//number of companies num=NumberOfCompanies(); long *cost=new long[num]; for(short i=0; i<num; i++) cost[i]=GetShareCost(i); short id=MyNodeId(); if(id==0) { short *link=new short[num]; short unlinked=0; for(short i=1; i<num; i++) { link[i]=0; PutChar(i,'a'); Send(i); Receive(i); if(GetChar(i)!=0) { link[i]=i; } else { unlinked++; } } for(short i=1; i<num; i++) { if(link[i]&&unlinked) { PutChar(i,'f'); PutInt(i,unlinked); for(short j=1; j<num; j++) { if(!link[i]) PutInt(i,j); } Send(i); Receive(i); if(GetChar(i)=='F') { int j=-1; while(j) { j=GetInt(i); link[j]=i; unlinked--; } } } }/* for(short i=1; i<num; i++) { PutChar(link[i],'g'); PutInt(link[i],i); Send(link[i]); Receive(link[i]); int cost_len=GetInt(link[i]); } PutChar(link[g_id],'g'); PutInt(link[g_id],g_id); Send(link[g_id]); Receive(link[g_id]); int num_of_el=GetInt(link[g_id]); PutInt(id_master2,num_of_el); for(int i=0; i<num_of_el; i++) { PutInt(id_master2,GetInt(link[g_id])); } Send(id_master2);*/ long long total_len=num; for(short i=1; i<num; i++) { PutChar(link[i],'l'); Send(link[i]); Receive(link[i]); total_len+=GetInt(link[i]); } long long *cost_all=new long long[total_len]; long long curr_index=0; for(curr_index; curr_index<num; curr_index++) cost_all[curr_index]=cost[curr_index]; for(short i=1; i<num; i++) { PutChar(link[i],'g'); PutInt(link[i],i); Send(link[i]); Receive(link[i]); int cost_len=GetInt(link[i]); for(int i=0; i<cost_len; i++) { cost_all[curr_index]=GetInt(link[i]); curr_index++; } } sort(cost_all,cost_all+total_len); long long output=0; int iterat=0; for(total_len; total_len>0; total_len--) { output+=cost_all[0]*total_len; iterat++; } cout<<output; } else { Receive(0); PutChar(0,GetChar(0)); Send(0); bool done=false; short *link=new short[num]; for(short i=1; i<num; i++) link[i]=0; int id_master=-1; int nodes_left=0; while(!done) { int id_master2=Receive(-1); if(id_master==-1) id_master=id_master2; char mess1=GetChar(id_master2); if(mess1=='f') { int len=GetInt(id_master2); int *ids=new int[len]; bool *ids_b=new bool[len]; for(int i=0; i<len; i++) { ids[i]=GetInt(id_master2); } int unlinked=0; for(int i=0; i<len; i++) { PutChar(ids[i],'b'); Send(ids[i]); Receive(ids[i]); if(GetChar(ids[i])!=0) { link[ids[i]]=ids[i]; nodes_left++; } else { unlinked++; ids_b[i]=false; } } for(int i=0; i<len; i++) { if(ids_b[i]&&unlinked) { PutChar(ids[i],'f'); PutInt(ids[i],unlinked); for(short j=0; j<len; j++) { if(!ids_b[j]) PutInt(ids[i],ids[j]); } Send(ids[i]); Receive(ids[i]); if(GetChar(ids[i])=='F') { int j=-1; while(j) { j=GetInt(ids[i]); link[j]=ids[i]; ids_b[j]=true; unlinked--; nodes_left++; } } } } PutChar(id_master2,'F'); for(int i=0; i<len; i++) { if(ids_b[i]) { PutInt(id_master2,ids[i]); } } PutInt(id_master2,0); Send(id_master2); } else if(mess1=='g') { int g_id=GetInt(id_master2); if(g_id==id) { PutInt(id_master2,num); for(int i=0; i<num; i++) { PutInt(id_master2,cost[i]); } Send(id_master2); nodes_left--; if(!nodes_left) return 0; } else { PutChar(link[g_id],'g'); PutInt(link[g_id],g_id); Send(link[g_id]); Receive(link[g_id]); int num_of_el=GetInt(link[g_id]); PutInt(id_master2,num_of_el); for(int i=0; i<num_of_el; i++) { PutInt(id_master2,GetInt(link[g_id])); } Send(id_master2); nodes_left--; if(!nodes_left) return 0; } } } } return 0; } |