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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

class Edge
{
public:
    LL target;
    LL cost;
    Edge(LL p_target, LL p_cost) { target = p_target; cost = p_cost; }
};

class EdgeComparator
{
public:
    bool operator()(Edge &a, Edge &b)
    {
        return a.cost > b.cost;
    }
};

bool A;
LL n;
bool randomBits[2200];
vector<Edge> edges[2100];
priority_queue<Edge, vector<Edge>, EdgeComparator> candidates;
int state[2100]; // 0-not seen, 1-candidate, 2-calculated
bool bits[2100][16];
LL dist[2100]; // calculated distance
LL currentDistance=0;

LL randomInt(LL seed)
{
    return (1103515245*seed + 12345) % 2147483648;
}

void initialize()
{
	LL x = 772342422;
	int k = 0;
	FOR(i,1,70)
	{
	    x = randomInt(x);
	    LL r = x;
	    FOR(j, 1, 30)
	    {
	        randomBits[k++] = (r % 2 == 0);
	        r = r / 2;
	    }
	}
	FOR(k,0,2026)
	{
	    LL r = k;
	    FOR(i,0,15)
	    {
	        bits[k][i]=r % 2;
	        r = r / 2;
	    }
	}
}

void readGraph()
{
    cin >> n;
    LL k; cin >> k;
    LL a,b,c;
    FOR(i,1,k)
    {
        cin >> a; cin >> b; cin >> c;
        edges[a].push_back(Edge(b,c));
        edges[b].push_back(Edge(a,c));
    }
    FOR(i,1,n)
    {
        state[i]=0;
        dist[i]=-1;
    }
}

void addCandidatesFrom(int v)
{
    int w, ec;
    FOREACH(e,edges[v])
    {
        w = e->target;
        ec = e->cost;
        if (state[w]==0)
        {
            dist[w] = dist[v]+ec;
            state[w] = 1;
            candidates.push(Edge(w, dist[w]));
        }
        else if (state[w]==1)
        {
            if (dist[v]+ec < dist[w])
            {
                dist[w] = dist[v]+ec;
                candidates.push(Edge(w, dist[w]));
            }
        }
    }
}

string myName()
{
    if (A) return "A"; else return "B";
}

void send(int k, int bitCount)
{
    FOR(i,0,bitCount-1) if (bits[k][i]) cout << "+ 1\n"; else cout << "+ 0\n";
    cout.flush();
}

int receive(int bitCount)
{
    int num=0;
    int m=1; int b;
    FOR(i,1,bitCount)
    {
        cout << "?\n"; cout.flush();
        cin >> b;
        num += m*b;
        m*=2;
    }
    return num;
}

void doTheWork()
{
    state[1]=2; dist[1]=0; currentDistance=0;
    addCandidatesFrom(1);
    LL myMinDist=0, hisMinDist=0;
    int myOK=0; int hisOK=0; int chosen;
    FOR(q,2,n)
    {
        while (candidates.size()>0 && state[candidates.top().target]==2) candidates.pop();
        if (candidates.size()==0) myMinDist=510; else myMinDist=candidates.top().cost-currentDistance;
        if ((A && randomBits[q]) || (!A && !randomBits[q]))
        {
            // I send my best distance to the peer
            //cerr << q << " " << myName() << " sends " << myMinDist << "\n"; cerr.flush();
            send(myMinDist,9);
            myOK = receive(1);
            //cerr << q << " " << myName() << " received " << myOK << "\n"; cerr.flush();
            if (myOK==1)
            {
                // my candidate is chosen, so send node number:
                chosen = candidates.top().target;
                send(chosen, 11);
                state[chosen] = 2;
                currentDistance = dist[chosen];
                addCandidatesFrom(chosen);
            }
            else
            {
                // his candidate is better, so receive distance and node number:
                hisMinDist = receive(9);
                chosen = receive(11);
                state[chosen] = 2;
                dist[chosen] = currentDistance + hisMinDist;
                currentDistance = dist[chosen];
                addCandidatesFrom(chosen);
            }
        }
        else
        {
            hisMinDist = receive(9);
            //cerr << q << " " << myName() << " received " << hisMinDist << "\n"; cerr.flush();
            if (myMinDist<hisMinDist) hisOK=0; else hisOK=1;
            //cerr << q << " " << myName() << " sends " << hisOK << "\n"; cerr.flush();
            send(hisOK, 1);
            if (hisOK==1)
            {
                // his candidate is better, so receive his node number:
                chosen = receive(11);
                state[chosen] = 2;
                dist[chosen] = currentDistance + hisMinDist;
                currentDistance = dist[chosen];
                addCandidatesFrom(chosen);
            }
            else
            {
                // my candidate is chosen, so I must send distance and node number:
                chosen = candidates.top().target;
                send(myMinDist, 9);
                send(chosen, 11);
                state[chosen] = 2;
                currentDistance = dist[chosen];
                addCandidatesFrom(chosen);
            }
        }
    }
}


/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);
	initialize();
    string name; cin >> name;
    A = (name=="Algosia");
    readGraph();
    doTheWork();
    if (A)
    {
        cout << "! ";
        FOR(q,1,n) cout << dist[q] << ' ';
        cout << "\n";
        cout.flush();
    }

    return 0;
};