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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "kollib.h"
#include "message.h"
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <set>
#include <vector>
#include <map>
#include <ctime>
using namespace std;

#define MAXPROC 100
#define DEBUG 0

typedef map<int, vector<pair<int,int> > > Graph;

int ns, id, n, s[MAXPROC], m;
set<int> startpoints;
//map<int, vector<pair<int,int> > > G;
map<int, vector<int> > query;
set<int> inquery;
Graph edges, g;
int nedges=0;

void walk(int x, int last) {
  map<int, int> cost;
  int dist = 1;
  do {
    #if DEBUG
    if (id==1) printf("walk id=%d x=%d last=%d\n",id,x,last);
    #endif
    if (query.find(x) != query.end()) {
      #if DEBUG
      if (id==1) printf("klient\n");
      #endif
      cost[x] = dist;
      for (vector<int>::iterator it = query[x].begin(); it != query[x].end(); it++) {
        if (cost.find(*it) != cost.end()) {
          // dodaj krawedz (x, *it) z kosztem dist - cost[*it]
          edges[*it].push_back(make_pair(x, dist-cost[*it]));
          nedges++;
          #if DEBUG
          if (id==1) printf("kurwa %d %d c=%d\n",*it,x,dist-cost[*it]);
          #endif
        }
      }

      #if DEBUG
      if (id==1) printf("push:%d %d\n",s[id],x);
      #endif
      edges[s[id]].push_back(make_pair(x, dist));
      nedges++;
    }
    //if (inquery.find(x) != inquery.end()) {
      // dodaj krawedz od punktu startowego
      //if (id==1) printf("dodaje %d %d c=%d\n",s[id],x,dist);
    //}
    int a = FirstNeighbor(x);
    int b = SecondNeighbor(x);
    if (last == a) {
      last = x;
      x = b;
    }
    else
    {
      last = x;
      x = a;
    }
    dist++;
  } while (startpoints.find(last) == startpoints.end());
  // sąsiednie nody też łączymy
  edges[s[id]].push_back(make_pair(last, dist-1));
  nedges++;
}

int shortest(int a, int b) {
  map<int,int> d;
  set<pair<int,int> > q; 

  d[a] = 0;
  q.insert(make_pair(0, a));
  while (!q.empty()) { 
    int u = q.begin()->second;
    q.erase(q.begin());
    for (vector<pair<int,int> >::iterator v = g[u].begin(); v != g[u].end(); v++) {
      if (d.find(v->first) == d.end() || d[v->first] > d[u] + v->second) {
        d[v->first] = d[u] + v->second;
        q.insert(make_pair(d[v->first], v->first));
      }
    }
  } 
  return d[b];
}

int main() {
  n = NumberOfNodes();
  ns = NumberOfStudents();
  id = MyNodeId(); 
  m = NumberOfQueries();
  srand (time(NULL));
  for (int i = 0; i <= id; i++) { 
    s[id] = rand() % ns + 1;
  }

  #if DEBUG
  printf("s[%d]=%d\n",id,s[id]);
  #endif

  // wyślij wszystkim s
  for (int i = 0; i < n; i++) {
    if (i != id) {
      PutInt(i, s[id]);
      Send(i);
    }
  }

  // odbierz s od reszty
  for (int i = 0; i < n; i++) {
    if (i != id) {
      Receive(i);
      int si = GetInt(i);
      startpoints.insert(si);
      s[i] = si; 
    }
    startpoints.insert(s[id]);
  }

  // uzupełnij zapytania których będziemy pytać
  for (int i = 1; i <= m; i++) {
    int from, to;
    from = QueryFrom(i);
    to = QueryTo(i);
    query[from].push_back(to);
    query[to].push_back(from);
    //inquery.insert(to);
    //inquery.insert(from);
  }

  // idziemy w jednym oraz drugim kierunku szukając 
  // uczniow o których będziemy pytać
  //printf("\n");
  walk(FirstNeighbor(s[id]), s[id]);
  //printf("\n");
  walk(SecondNeighbor(s[id]), s[id]);
 //printf("\n");
 

  // eliminacja powtorzen krawedzi
  nedges = 0;
  for (Graph::iterator it = edges.begin(); it != edges.end(); it++) {
    sort(it->second.begin(), it->second.end());
    unique(it->second.begin(), it->second.end());
    nedges += it->second.size();
  }
  

  // id!=0 wysyłają id=0 swoje krawędzie
  if (id != 0) {
    PutInt(0, nedges); 
    Send(0);
    for (Graph::iterator it = edges.begin(); it != edges.end(); it++) {
      for (vector<pair<int,int> >::iterator jt = it->second.begin(); jt != it->second.end(); jt++) {
        PutInt(0, it->first);
        PutInt(0, jt->first);
        PutInt(0, jt->second);
        //if (id == 1) printf("wyslalem %d %d\n", it->first, jt->first);
      }
    }
    Send(0);
  }
  else
  {
    // id=0 odbiera krawędzie od pozostałych 

    for (int i = 1; i < n; i++) {
      Receive(i);
      int q = GetInt(i);
      int a, b, c;
      Receive(i);
      while (q--) {
        a = GetInt(i); 
        b = GetInt(i); 
        c = GetInt(i);
        edges[a].push_back(make_pair(b, c));
       // printf("odebralem %d %d\n",a,b);
        //edges[b].push_back(make_pair(a, c));
      }
    }



    // g = nieskierowany edge
    for (Graph::iterator it=edges.begin(); it != edges.end(); it++) {
      for (vector<pair<int,int> >::iterator jt=it->second.begin(); jt != it->second.end(); jt++) {
        g[it->first].push_back(make_pair(jt->first, jt->second)); 
        g[jt->first].push_back(make_pair(it->first, jt->second));
      }
    }

    // debug
    #if DEBUG
    printf("nieskierowany:\n");
    for (map<int, vector< pair<int,int> > >::iterator it=g.begin(); it != g.end(); it++) {
      printf("%d:", it->first);
      for (vector<pair<int,int> >::iterator jt=it->second.begin(); jt != it->second.end(); jt++) {
        printf(" (%d,%d)", jt->first, jt->second);
      }
      printf("\n");
    }
    #endif

    // szukanie najkrótszej ścieżki dla zapytań
    //
    for (int i = 1; i <= m; i++) {
      printf("%d\n",shortest(QueryFrom(i), QueryTo(i)));
    }
  }
  
  return 0;
}