#include #include #include #include using namespace std; typedef long long ll; typedef pair pii; struct road_t { int to, c; }; struct point_t { int s; vector road; }; struct state_t { int c, p, s; bool operator < (const state_t& rhs) const { return c>rhs.c; } }; priority_queue que; int min_u(int& m, int v) { if(m>v) { m=v; return 1; } return 0; } void push(int c, int p, int s) { que.push((state_t){c, p, s}); } int pop(int& c, int& p, int& s) { if(que.empty()) return 0; state_t w=que.top(); c=w.c; p=w.p; s=w.s; que.pop(); return 1; } int solve(vector& point) { int c=0, p=0, s=0, n=point.size(); vector dp(n*n, 50*1000*2); push(c, p, s); for(;pop(c, p, s);) { int idx=s*n+p; if(min_u(dp[idx], c)==0) continue; for(auto r: point[p].road) { push(c+r.c, r.to, s); } if(p==0 || p==n-1 || s==n-1 || s==p) continue; int ns=p; if(s>0) ns=n-1; push(c+point[p].s, p, ns); } return dp.back(); } int main(void) { int n, m, i, a, b, c; vector point; while(scanf("%d", &n)==1) { point.clear(); point.resize(n); for(i=0;i