HDU6071-Lazy Running

来源:互联网 发布:手机编辑epub软件 编辑:程序博客网 时间:2024/05/07 07:29

Lazy Running

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


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 K meters.

There are 4 checkpoints in the campus, indexed as p1,p2,p3 and p4. Every time you pass a checkpoint, you should swipe your card, then the distance between this checkpoint and the last checkpoint you passed will be added to your total distance.

The system regards these 4 checkpoints as a circle. When you are at checkpoint pi, you can just run to pi1 or pi+1(p1 is also next to p4). You can run more distance between two adjacent checkpoints, but only the distance saved at the system will be counted.





Checkpoint p2 is the nearest to the dormitory, Little Q always starts and ends running at this checkpoint. Please write a program to help Little Q find the shortest path whose total distance is not less than K.
 

Input
The first line of the input contains an integer T(1T15), denoting the number of test cases.

In each test case, there are 5 integers K,d1,2,d2,3,d3,4,d4,1(1K1018,1d30000), denoting the required distance and the distance between every two adjacent checkpoints.
 

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

Sample Input
12000 600 650 535 380
 

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

Source
2017 Multi-University Training Contest - Team 4
 

题意:给出四个点1,2,3,4,1和2,2和3,3和4,4和1 之间有路相连,现在从2点出发,最后回到2点,要求路径大于等于k,问路径长度最短是多少

解题思路:取一条与2相连的权值最小的边w。若存在一条从起点到终点的长度为k的路径,那么必然存在一条长度为k+2w的路径,只要一开始在那条边上往返走就好了。设dis[i][j]表示从起点到i,路径长度模2w为j时,路径长度的最小值。


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst LL INF = 0x3f3f3f3f3f3f3f3f;struct node{    int id;    LL dis;    bool operator <(const node &a)const    {        return dis>a.dis;    }} pre,nt1;LL dis[5][60009],k,w[10],r;int s[5],nt[10],e[10],cnt;void add(int u,int v,LL val){    nt[cnt]=s[u],s[u]=cnt,e[cnt]=v,w[cnt++]=val;    nt[cnt]=s[v],s[v]=cnt,e[cnt]=u,w[cnt++]=val;}void Dijkstra(){    memset(dis,INF,sizeof dis);    dis[2][0] = 0;    priority_queue<node>q;    pre.id=2,pre.dis=0;    q.push(pre);    while(!q.empty())    {        pre=q.top();        q.pop();        for(int i=s[pre.id]; ~i; i=nt[i])        {            int ee=e[i];            if(pre.dis + w[i] >= dis[ee][(w[i]+pre.dis)%r]) continue;            dis[ee][(w[i]+pre.dis)%r] = pre.dis+w[i];            nt1.id=ee;            nt1.dis=pre.dis+w[i];;            q.push(nt1);        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(s,-1,sizeof s);        cnt=0;        int d1,d2,d3,d4;        scanf("%lld%d%d%d%d",&k,&d1,&d2,&d3,&d4);        r = 2 * min(d1,d2);        add(1,2,d1);        add(2,3,d2);        add(3,4,d3);        add(4,1,d4);        Dijkstra();        LL ans = INF;        for(int i=0; i<r; i++)        {            if(dis[2][i]>k) ans = min(dis[2][i],ans);            else ans = min(ans,dis[2][i] + (k - dis[2][i])/r*r + ((k - dis[2][i])%r>0) * r);        }        printf("%lld\n",ans);    }}

原创粉丝点击