# Lazy Running

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 657    Accepted Submission(s): 284

Problem Description
In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule, you must keep your speed, and your running distance should not be less than Input
The first line of the input contains an integer

Output
For each test case, print a single line containing an integer, denoting the minimum distance.

Sample Input
1 2000 600 650 535 380

Sample Output
2165
Hint
The best path is 2-1-4-3-2.

Source

 1 #pragma comment(linker, "/STACK:102400000,102400000")
2 #include <bits/stdc++.h>
3 #include <cstdlib>
4 #include <cstdio>
5 #include <iostream>
6 #include <cstdlib>
7 #include <cstring>
8 #include <algorithm>
9 #include <cmath>
10 #include <cctype>
11 #include <map>
12 #include <set>
13 #include <queue>
14 #include <bitset>
15 #include <string>
16 #include <complex>
17 #define LL long long
18 #define mod 1000000007
19 using namespace std;
20 LL x,d12,d23,d34,d41;
21 int t;
22 struct node{
23     LL st,we,dis;
24 }exm,ok;
25 int vis;
26 int dp;
27 LL d;
28 LL bew;
29 queue<node> q;
30 void dij()
31 {
32     while(!q.empty())
33         q.pop();
34     exm.st=2;
35     exm.we=0;
36     exm.dis=0;
37     q.push(exm);
38     dp=0;
39     vis[exm.st][exm.we]=1;
40     while(!q.empty()){
41         exm=q.front();
42         q.pop();
43         vis[exm.st][exm.we]=0;
44         LL now,ww;
45         now=(exm.st)%4+1;
46         ww=(exm.dis+d[exm.st][now])%(2*bew);
47         if(dp[now][ww]==-1||dp[now][ww]>=exm.dis+d[exm.st][now]){
48             dp[now][ww]=exm.dis+d[exm.st][now];
49             if(vis[now][ww]==0){
50             vis[now][ww]=1;
51             ok.st=now;
52             ok.we=ww;
53             ok.dis=exm.dis+d[exm.st][now];
54             q.push(ok);
55             }
56         }
57         now=(exm.st-1);
58         if(now==0)
59             now=4;
60         ww=(exm.dis+d[exm.st][now])%(2*bew);
61         if(dp[now][ww]==-1||dp[now][ww]>=exm.dis+d[exm.st][now]){
62             dp[now][ww]=exm.dis+d[exm.st][now];
63             if(vis[now][ww]==0){
64             vis[now][ww]=1;
65             ok.st=now;
66             ok.we=ww;
67             ok.dis=exm.dis+d[exm.st][now];
68             q.push(ok);
69         }
70         }
71     }
72
73 }
74 int main()
75 {
76     scanf("%d",&t);
77     for(int i=1;i<=t;i++){
78         memset(vis,0,sizeof(vis));
79         memset(dp,-1,sizeof(dp));
80         scanf("%lld %lld %lld %lld %lld",&x,&d,&d,&d,&d);
81         d=d;
82         d=d;
83         d=d;
84         d=d;
85         bew=min(d,d);
86         dij();
87         LL ans=1e18+1;
88         for(LL j=0;j<2*bew;j++){
89             if(dp[j]==-1)
90                 continue;
91             LL zhong=(max(0LL,x-dp[j]))/(2*bew);
92           if(dp[j]+zhong*2*bew<x)
93             zhong++;
94          ans=min(ans,dp[j]+zhong*2*bew);
95        }
96        printf("%lld\n",ans);
97     }
98     return 0;
99 }