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

#define PB push_back
#define FORE(i,t) for(typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#define REP(i,n) for(int i=0,_=(n);i<_;++i)
#define FOR(i,a,b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i,a,b) for(int i=(a),_=(b);i>=_;--i)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const ll LLINF = 1e12 + 9;
const int INF = 1e9 + 9;

struct Edge {
  int a, b, weight;
};

//void dbg(string s) {
//  fprintf(stderr, s.c_str());
//  fprintf(stderr, "\n");
//}

int nodes_count = NumberOfNodes();
int node = MyNodeId();

int n_size = NumberOfStudents();

int queries_count = NumberOfQueries();
vector<pii> queries;

int selects = (int) sqrt((double) n_size) + 5;
vi selected;
set<int> is_query, is_selected;

map<int, int> mapping;
int mapping_count = 0;

int fetch_id(int x) {
  if (mapping.count(x)) {
    return mapping[x];
  }
  mapping[x] = mapping_count++;
}

pii get_part(int range) {
  return pii(
          (int) ((ll) range * node / nodes_count),
          (int) ((ll) range * (node + 1) / nodes_count - 1)
          );
}

void solve_part() {
  vector<Edge> edges;
  pii part = get_part(selects);
//  cerr << "part: " << part.first << " " << part.second << "\n";
  FOR(i, part.first, part.second) {
    const int start = selected[i];
//    cerr << "start: " << start << "\n";
    int dir[] = {FirstNeighbor(start), SecondNeighbor(start)};

    REP(d, 2) {
//      cerr << "  d: " << d << "\n";
      int dist = 1;
      int last = start;
      int last_vertex = start;
      for (int current = dir[d];; ++dist) {
//        cerr << "    current: " << current << "\n";
        if (is_query.count(current) || is_selected.count(current)) {

          edges.PB((Edge) {
            last_vertex, current, dist});
          if (is_selected.count(current)) {
            break;
          }
          last_vertex = current;
          dist = 0;
        }
        int n1 = FirstNeighbor(current);
        int n2 = SecondNeighbor(current);
        int next = last == n1 ? n2 : n1;
        last = current;
        current = next;
      }
    }
  }
  PutInt(0, SZ(edges));

  FORE(edge, edges) {
    PutInt(0, edge->a);
    PutInt(0, edge->b);
    PutInt(0, edge->weight);
  }
  Send(0);
//  cerr << "after solve part\n";
}

void dijkstra(const vector<Edge> &edges, const vector<pii> &qs) {
//  cerr << "dijkstra\n";
  vector<vector<Edge> > adj(mapping_count);
  
  FORE(edge, edges) {
    adj[edge->a].PB((Edge){-1, edge->b, edge->weight});
    adj[edge->b].PB((Edge){-1, edge->a, edge->weight});
//    cerr << "edge " << edge->a << ".."  << edge->b << "   " << edge->weight << "\n";
  }
  FORE (v, qs) {
//    cerr << "v " << v->first << " " << v->second << "\n";
    vector<ll> dist(mapping_count, LLINF);
    dist[v->first] = 0;
    priority_queue<pair<ll, int> > q;
    q.push(make_pair(0LL, v->first));
    while (!q.empty()) {
      int x = q.top().second;
      ll d = -q.top().first;
      q.pop();
      if (dist[x] != d) {
        continue;
      }
      if (x == v->second) {
        printf("%d\n", (int)d);
        break;
      }
      FORE (yt, adj[x]) {
        int y = yt->b;
        int w = yt->weight;
        if (d + w < dist[y]) {
          dist[y] = d + w;
          q.push(make_pair(-dist[y], y));
        }
      }
    }
  }
}

vector<Edge> translate_edges(const vector<Edge> &edges) {
  vector<Edge> result;

  FORE(edge, edges) {

    result.PB((Edge) {
      fetch_id(edge->a), fetch_id(edge->b), min(edge->weight, n_size - edge->weight)});
  }
  return result;
}

vector<pii> translate_queries(const vector<pii> &qs) {
  vector<pii> result;
  
  FORE (q, qs) {
    result.PB(pii(fetch_id(q->first), fetch_id(q->second)));
  }
  return result;
}

void merge_results() {
//  cerr << "merge_results\n";
  vector<Edge> edges;

  REP(i, nodes_count) {
    int received = Receive(-1);
    int edges_count = GetInt(received);

    REP(j, edges_count) {
      int a = GetInt(received);
      int b = GetInt(received);
      int weight = GetInt(received);
      Edge edge = (Edge){a, b, weight};
      edges.PB(edge);
    }
  }
  vector<Edge> translated_edges = translate_edges(edges);
  vector<pii> translated_queries = translate_queries(queries);
  dijkstra(translated_edges, translated_queries);
}

void inline jeden() {
//  dbg("jeden");
//  set<int> queries;
  REP(i, queries_count) {
    int a = QueryFrom(i + 1);
    int b = QueryTo(i + 1);
    queries.PB(pii(a, b));
    is_query.insert(a);
    is_query.insert(b);
  }

  REP(i, selects) {
    int s = (rand() % n_size) + 1;
    selected.PB(s);
    is_selected.insert(s);
  }
  solve_part();
  if (node == 0) {
    merge_results();
  }
}

int main() {
//  srand(34523454);
//  cerr << rand() << "\n";
  //int z; scanf("%d", &z); while(z--)
  jeden();
}