POJ6071 Lazy Running(最短路)

22 篇文章 0 订阅

Lazy Running

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


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
  
  
1 2000 600 650 535 380
 

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

Source
 

Recommend
liuyiding
 


题意:四个点依次排列,首尾相接。从第二个点出发,最后要求回到第二个点。问怎么走能使总路程为大于等于k的最小值。

非常巧妙的最短路。

考虑一个这样的情况,当我在某个时间回到第二个点时,此时总路程不够k,我是不是可以选择重复走第一个点和第二个点之间的路,或者第二个点和第三个点之间的路。

取d12和d23之间的较小值记为w,若存在一条长度为x的回路,那么必然存在一条长度为x+w的回路。把每一个点拆成0-w-1个点,求出同余最短路即可。


#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
#include <ctime>
using namespace std;
typedef long long ll;
const int MAXN=240100;
const long long INF=0x7fffffffffffffff;
typedef long long int ll;
struct edge{
    int v,next;
    int w;
}e[MAXN<<1];
int tot=0;
int head[MAXN];

void add(int u,int v,int w){
    e[tot].v=v;
    e[tot].w=w;
    e[tot].next=head[u];
    head[u]=tot++;
}

long long K,W;
long long d[MAXN];
queue<int> que;
int vis[MAXN];
long long spfa(int s){
    memset(vis,0,sizeof vis);
    fill(d,d+MAXN,INF);
    d[s]=0;
    while(!que.empty())
        que.pop();
    que.push(s);
    vis[s]=1;
    while(que.size()){
        int u=que.front();
        vis[u]=0;
        que.pop();
        for(int k=head[u];k!=-1;k=e[k].next){
            int v=e[k].v;
            int w=e[k].w;
            if(d[v]>d[u]+1LL*w){
                d[v]=d[u]+1LL*w;
                if(!vis[v]){
                    que.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    long long res=INF;
    for(int i=W;i<2*W;i++){
        long long temp=K-d[i];
        //if(d[i]<INF){
        //    cout<<i<<" "<<d[i]<<" "<<endl;
       // }
        if(temp<=0)
            res=min(res,d[i]);
        else{
            long long x=temp/W;
            if(temp%W)
                x++;
            res=min(res,x*W+d[i]);
        }
    }
    return res;
}



int main(){
    int t;
    scanf("%d",&t);
    while(t--){
      int d1,d2,d3,d4;
      tot=0;
      memset(head,-1,sizeof head);
      scanf("%lld%d%d%d%d",&K,&d1,&d2,&d3,&d4);
      W=min(d1,d2);
      W*=2;
      for(int i=0;i<W;i++){
        add(i,(i+d1)%W+W,d1);
        add(i+W,(i+d1)%W,d1);
        add(i+W,(i+d2)%W+2*W,d2);
        add(i+2*W,(i+d2)%W+W,d2);
        add(i+2*W,(i+d3)%W+3*W,d3);
        add(i+3*W,(i+d3)%W+2*W,d3);
        add(i,(i+d4)%W+3*W,d4);
        add(i+3*W,(i+d4)%W,d4);
      }
      printf("%lld\n",spfa(W));
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值