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
#include <cassert>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <cctype>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <numeric>
#include <cmath>
#include "message.h"
#include "kollib.h"
using namespace std;

typedef vector <int > VI;
typedef vector < VI > VVI;
typedef long long LL;
typedef vector < LL > VLL;
typedef vector < double > VD;
typedef vector < string > VS;
typedef pair<int,int> PII;
typedef vector <PII> VPII;
typedef istringstream ISS;

#define ALL(x) x.begin(),x.end()
#define REP(i,n) for (int i=0; i<(n); ++i)
#define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var)
#define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var)
#define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it)
#define PB push_back
#define PF push_front
#define MP(a,b) make_pair(a,b)
#define ST first
#define ND second
#define SIZE(x) (int)x.size()

int n, id, num_instances;
VI wezly;
int p, r;
int my_node;
map<int,int> mapa;
const int Q = 2200;
int q;
int queries[Q][2];
int important[2*Q];


inline int is_important(int x) {
  return binary_search(important, important+2*q, x);
}

inline int is_splitting(int x) {
  x--;
  return (x % p == r) && (x/p < num_instances-1);
}

void go(int x, int parent) {
  VPII v;
  int d = 1;
  while (true) {
    if (is_important(x)) v.PB(MP(x, d));
    if (is_splitting(x)) break;
    int y = FirstNeighbor(x);
    if (y == parent) {
      y = SecondNeighbor(x);
    }
    parent = x; 
    x = y;
    d++;
  }
  if (my_node < x) {
    PutInt(0, SIZE(v));
    FOREACH(it, v) {
      PutInt(0, it->ST);
      PutInt(0, it->ND);
      PutInt(0, my_node);
      PutInt(0, x);
      PutInt(0, d);
    }
  } else {
    PutInt(0, 0);
  }
  PutInt(0, my_node);
  PutInt(0, x);
  PutInt(0, d);
  Send(0);
}

vector<pair<int,PII> > edges;

void addEdge(int a,int b,int c) {
//  fprintf(stderr,"edge: a=%d b=%d c=%d\n", a, b, c);
  edges.PB(MP(a,MP(b,c)));
  edges.PB(MP(b,MP(a,c)));
}

int dijkstra(int start, int end) {
  map<int,int> dmap;
  dmap[start] = 0;
  priority_queue<PII> q;
  q.push(MP(0, start));
  while (!q.empty()) {
    PII p = q.top();
    q.pop();
    if (-p.ST == dmap[p.ND]) {
      if (p.ND == end) return -p.ST;
      FOREACH(it, edges) if (it->ST == p.ND) {
        int x = it->ND.ST;
        int koszt = it->ND.ND - p.ST;
        if (dmap.count(x) == 0 || dmap[x] > koszt) {
          dmap[x] = koszt;
          q.push(MP(-koszt, x));
        }
      }
    }
  }
  //FOREACH(it, dmap) printf("dmap[%d]=%d\n", it->ST, it->ND);
  assert(0);
  return -1;
}

void server() {
  if (q > 0) {
    REP(bar, 2 * (num_instances - 1)) {
      int nr = Receive(-1);
      int k = GetInt(nr);
      int prev = -1;
      int prevd = -1;
      REP(foo, k){
        int x = GetInt(nr);
        int d = GetInt(nr);
        int skad = GetInt(nr);
        int dokad = GetInt(nr);
        int dd = GetInt(nr);
        //fprintf(stderr,"server: x=%d d=%d skad=%d dokad=%d dd=%d\n", x, d, skad, dokad, dd);
        addEdge(skad, x, d);
        addEdge(dokad, x, dd-d);

        if (prev != -1) {
          addEdge(prev, x, d - prevd);
        }
        prev = x; prevd = d;
      }
      int a = GetInt(nr);
      int b = GetInt(nr);
      int c = GetInt(nr);
      if (a < b) {
        addEdge(a,b,c);
      }
      //fprintf(stderr,"server: a=%d b=%d c=%d\n", a, b, c);
    }

//    fprintf(stderr, "sizeof(edges)=%d\n", SIZE(edges));
    REP(i,q) printf("%d\n", dijkstra(queries[i][0], queries[i][1]));
  }
}

void client() {
  if (q > 0) {
    int a = FirstNeighbor(my_node);
    go(a, my_node);
    int b = SecondNeighbor(my_node);
    go(b, my_node);
  }
}

int main(){
  id = MyNodeId();
  num_instances = NumberOfNodes();
  n = NumberOfStudents();
  num_instances = min(num_instances, n+1);
  if (id >= num_instances) return 0;

  wezly.PB(-1); //id=0 does nothing

  p = n / (num_instances-1);
  r = rand() % p;

  REP(i, num_instances-1) {
    int x = i * p + r + 1;
    assert(mapa.count(x) == 0);
    wezly.PB(x);
    mapa[x] = i;
  }

  my_node = wezly[id];
//  fprintf(stderr,"id = %d num_instances = %d my_node = %d\n", id, num_instances, my_node);

  q = NumberOfQueries();
  REP(i,q) {
    queries[i][0] = QueryFrom(i+1);
    queries[i][1] = QueryTo(i+1);
    important[2*i] = queries[i][0];
    important[2*i+1] = queries[i][1];
  }
  sort(important, important+2*q);

  if (id == 0) server();
  else client();
  return 0;
}