#include #include #include #include using namespace std; using vi=vector; using vvi=vector; using pii=pair; using vpii=vector; using vvpii=vector; struct q_t { int p, r, c; vi f; bool operator<(const q_t& rhs) const { return c q; public: void push(int p, int r, int c, vi& f) { q.push((q_t){p, r, c, f}); } int pop(int&p, int&r, int&c, vi&f) { if(q.empty()) return 0; auto t=q.top(); q.pop(); p=t.p; r=t.r; c=t.c; f=t.f; return 1; } }; int solve(vvpii& v, vi& u, int m) { int n=v.size(); vi f; int p=0, r=u[0], c=m, ret=-1; pq_t q; f.resize(n); f[0]=1; q.push(p, r, c, f); while(q.pop(p, r, c, f)) { //printf("p=%d r=%d c=%d\n", p, r, c); if(c<0) break; if(ret