#include "kollib.h"
#include "message.h"
#include <iostream>
using namespace std;
struct Zapytanie
{
int from;
int to;
int koszt;
};
int main() {
if(MyNodeId() == 0)
{
int nodes = NumberOfNodes();
int iloscZapytan = NumberOfQueries();
int kompletneZapytania = 0;
int wyslaneZapytania = 0;
Zapytanie zapytania[200];
for (int i = 1; i <= iloscZapytan; ++i) {
zapytania[i-1].from = QueryFrom(i);
zapytania[i-1].to = QueryTo(i);
}
for(int i = 1; i < nodes && i-1 < iloscZapytan; ++i)
{
wyslaneZapytania++;
// zapytanie
PutInt(i, i-1);
// resta
PutInt(i, zapytania[i-1].from);
PutInt(i, zapytania[i-1].to);
Send(i);
}
while(true)
{
int from = Receive(-1);
int zapytanie = GetInt(from);
int koszt = GetInt(from);
kompletneZapytania++;
zapytania[zapytanie].koszt = koszt;
if(kompletneZapytania == iloscZapytan)
break;
if(wyslaneZapytania <= iloscZapytan)
{
// zapytanie
PutInt(from, wyslaneZapytania);
// resta
PutInt(from, zapytania[wyslaneZapytania].from);
PutInt(from, zapytania[wyslaneZapytania].to);
Send(from);
wyslaneZapytania++;
}
}
for(int i = 1; i <= nodes; i++)
{
PutInt(i, -1);
Send(i);
}
for(int i = 0; i< iloscZapytan; i++)
cout << zapytania[i].koszt << endl;
}
else
{
while(true)
{
Receive(0);
int zapytanie = GetInt(0);
if(zapytanie == -1)
return 0;
int from = GetInt(0);
int to = GetInt(0);
int left = -1;
int right = -1;
int count = 0;
int oldLeft, oldRight;
if(from != to)
{
left = FirstNeighbor(from);
right = SecondNeighbor(from);
count++;
oldLeft = from;
oldRight = from;
int oldL, oldR, tmp;
while (left != to && right != to)
{
oldL = left;
oldR = right;
tmp = FirstNeighbor(left);
if(tmp == oldLeft)
left = SecondNeighbor(left);
else
left = tmp;
tmp = FirstNeighbor(right);
if(tmp == oldRight)
right = SecondNeighbor(right);
else
right = tmp;
oldLeft = oldL;
oldRight = oldR;
count++;
}
}
PutInt(0, zapytanie);
PutInt(0, count);
Send(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 | #include "kollib.h" #include "message.h" #include <iostream> using namespace std; struct Zapytanie { int from; int to; int koszt; }; int main() { if(MyNodeId() == 0) { int nodes = NumberOfNodes(); int iloscZapytan = NumberOfQueries(); int kompletneZapytania = 0; int wyslaneZapytania = 0; Zapytanie zapytania[200]; for (int i = 1; i <= iloscZapytan; ++i) { zapytania[i-1].from = QueryFrom(i); zapytania[i-1].to = QueryTo(i); } for(int i = 1; i < nodes && i-1 < iloscZapytan; ++i) { wyslaneZapytania++; // zapytanie PutInt(i, i-1); // resta PutInt(i, zapytania[i-1].from); PutInt(i, zapytania[i-1].to); Send(i); } while(true) { int from = Receive(-1); int zapytanie = GetInt(from); int koszt = GetInt(from); kompletneZapytania++; zapytania[zapytanie].koszt = koszt; if(kompletneZapytania == iloscZapytan) break; if(wyslaneZapytania <= iloscZapytan) { // zapytanie PutInt(from, wyslaneZapytania); // resta PutInt(from, zapytania[wyslaneZapytania].from); PutInt(from, zapytania[wyslaneZapytania].to); Send(from); wyslaneZapytania++; } } for(int i = 1; i <= nodes; i++) { PutInt(i, -1); Send(i); } for(int i = 0; i< iloscZapytan; i++) cout << zapytania[i].koszt << endl; } else { while(true) { Receive(0); int zapytanie = GetInt(0); if(zapytanie == -1) return 0; int from = GetInt(0); int to = GetInt(0); int left = -1; int right = -1; int count = 0; int oldLeft, oldRight; if(from != to) { left = FirstNeighbor(from); right = SecondNeighbor(from); count++; oldLeft = from; oldRight = from; int oldL, oldR, tmp; while (left != to && right != to) { oldL = left; oldR = right; tmp = FirstNeighbor(left); if(tmp == oldLeft) left = SecondNeighbor(left); else left = tmp; tmp = FirstNeighbor(right); if(tmp == oldRight) right = SecondNeighbor(right); else right = tmp; oldLeft = oldL; oldRight = oldR; count++; } } PutInt(0, zapytanie); PutInt(0, count); Send(0); } } return 0; } |
English