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
/*
 * main.cpp
 *
 *  Created on: May 15, 2014
 *      Author: jrogowski
 */

#include "kollib.h"
#include "message.h"
#include <vector>
#include <tuple>
#include <iostream>
#include <unordered_set>
#include <unordered_map>

using namespace std;

int main()
{
	unordered_map<int, tuple<int, int>> m1;
	unordered_map<int, tuple<int, int>> m2;
	// find interesting points
	unordered_set<int> s;
	vector<int> v;
	for (int i = 1; i <= NumberOfQueries(); ++i)
	{
		if (s.find(QueryFrom(i)) == s.end())
		{
			v.push_back(QueryFrom(i));
		}
		if (s.find(QueryTo(i)) == s.end())
		{
			v.push_back(QueryTo(i));
		}
		s.insert(QueryFrom(i));
		s.insert(QueryTo(i));
	}
	if (MyNodeId() > 0) {
		vector<tuple<int, int, int> > ans;
		for (int i = MyNodeId() - 1; i < v.size(); i += NumberOfNodes())
		{
			int result = 1;
			int prev = v[i];
			int current =  FirstNeighbor(v[i]);
			while (s.find(current) == s.end())
			{
				if (prev != FirstNeighbor(current)) {
					prev = current;
					current = FirstNeighbor(current);
				} else {
					prev = current;
					current = SecondNeighbor(current);
				}
				++result;
			}
			//cout << MyNodeId() << " Found from " << v[i] << " to " << current << " with d = " << result << endl;
			ans.push_back(make_tuple(v[i], current, result));
			// 2
			result = 1;
			prev = v[i];
			current =  SecondNeighbor(v[i]);
			while (s.find(current) == s.end())
			{
				if (prev != FirstNeighbor(current)) {
					prev = current;
					current = FirstNeighbor(current);
				} else {
					prev = current;
					current = SecondNeighbor(current);
				}
				++result;
			}
			//cout << MyNodeId() << " Found from " << v[i] << " to " << current << " with d = " << result << endl;
			ans.push_back(make_tuple(v[i], current, result));
		}
		PutInt(0, ans.size());
		for (int i = 0; i < ans.size(); ++i)
		{
			PutInt(0, get<0>(ans[i]));
			PutInt(0, get<1>(ans[i]));
			PutInt(0, get<2>(ans[i]));
		}
		Send(0);
	} else {
		// receive results
		//cout << "Start getting" << endl;
		for (int inst = 1; inst < NumberOfNodes(); ++inst)
		{
			Receive(inst);
			int size = GetInt(inst);
			//cout << "Getting " << size << " points" << endl;
			for (int i = 0; i < size; i+=2)
			{
				int from = GetInt(inst);
				int to = GetInt(inst);
				int cost = GetInt(inst);
				m1[from] = make_tuple(to, cost);
				//cout << "Getting info m1 " << from << " " << to << " " << cost <<endl;
				from = GetInt(inst);
				to = GetInt(inst);
				cost = GetInt(inst);
				m2[from] = make_tuple(to, cost);
				//cout << "Getting info m2 " << from << " " << to << " " << cost <<endl;
			}
		}
		//cout << "Done getting" << endl;
		for (int i = 1; i <= NumberOfQueries(); ++i)
		{
			int from = QueryFrom(i);
			int to = QueryTo(i);
			//cout << " Iterating " << from << " to " << to << endl;
			if (from == to)
			{
				cout << "0" << endl;
			}
			else
			{
				//path 1
				int result = 0;
				int current = from;
				int prev = 0;
				//cout << "Starting from " << from << " with m1[from] " << get<0>(m1[current]) << endl;
				while (current != to)
				{
					if (get<0>(m1[current]) != prev)
					{
						prev = current;
						current = get<0>(m1[current]);
						result += get<1>(m1[current]);
					}
					else
					{
						prev = current;
						current = get<0>(m2[current]);
						result += get<1>(m2[current]);
					}
					//cout << "Now current = " << current << endl;
				}
				int result2 = 0;
				current = from;
				prev = 0;
				while (current != to)
				{
					if (get<0>(m2[current]) != prev)
					{
						prev = current;
						current = get<0>(m2[current]);
						result2 += get<1>(m2[current]);
					}
					else
					{
						prev = current;
						current = get<0>(m1[current]);
						result2 += get<1>(m1[current]);
					}
				}
				int result3 = result < result2 ? result : result2;
				cout << result3 << endl;

			}

		}
	}


	return 0;
}