#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; vvi f; vi fv; bool operator<(const q_t& rhs) const { return c>rhs.c; } }; class pq_t { priority_queue q; public: void push(int p, int r, int c, vvi& f, vi& fv) { q.push((q_t){p, r, c, f, fv}); } int pop(int&p, int&r, int&c, vvi&f, vi&fv) { if(q.empty()) return 0; auto t=q.top(); q.pop(); p=t.p; r=t.r; c=t.c; f=t.f; fv=t.fv; return 1; } }; int solve(vvpii& v, vi& u, int m) { int n=v.size(); vvi f; vi fv; int p=0, r=u[0], c=0, ret=-1; pq_t q; fv.resize(n); f.resize(n); for(int i=0;im) continue; f[p][i]=1; np=v[p][i].first; nr=((fv[np])?r:r+u[np]); fv[np]++; q.push(np, nr, nc, f, fv); fv[np]--; f[p][i]=0; } } return ret; } int main(void) { int n, m; vi u; vvpii v; while(scanf("%d%d", &n, &m)==2) { v.clear(); v.resize(n); u.resize(n); for(int i=0;i