1. 程式人生 > >Hdu 6071 Lazy Running【同餘最短路】

Hdu 6071 Lazy Running【同餘最短路】

Lazy Running

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


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 p
i
, 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.

題目大意:

現在有4個點,我們一開始在點2處 ,我們希望最終也回到點2處 ,問我們怎樣走,使得總路徑和大於等於K並且最小。

思路:

我們知道,我們可以選以2為起點,1作為另一個點(或者是3的話也是相同的道理),使得我們的主人公一直徘徊在這條路徑上,那麼我們最終的Ans,可以表示為2*W(1,2)+Len這裡Len的長度明顯在區間:【0,2*W(1,2)】之間,那麼我們就去跑一個同餘的最短路即可。

那麼我們SPFA跑最短路,處理出陣列Dist【i】【Len】表示,從2出發,到點i處,長度%2*W(1,2)為Len的最短路。

那麼對於結果,我們列舉區間【0,2*W(1,2)】的值作為Len,然後向上去新增 2*W(1,2)即可。

對於Dist【2】【i】,我們考慮:

如果Dist【2】【i】>=K,那麼更新ans=min(ans,Dist【2】【i】);

否則我們設定Ned=K-Dist【2】【i】,表示缺少的路徑長度,我們向上去新增2*W(1,2)即可,新增的長度為:Ned/2*W(1,2)*(2*W(1,2))+Ned%(2*W(1,2))==0?0:2*W(1,2)。

其內容不難理解。

注意初始化陣列和INF要足夠大,就沒有別的什麼坑點了。

Ac程式碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define ll __int64
const ll INF=2e18;
struct node
{
    int u;
    ll len;
} now,nex;
ll mod;
ll mp[5][5];
ll vis[5][70050];
ll dist[5][70050];
void SPFA()
{
    queue<node>s;
    memset(vis,0,sizeof(vis));
    for(int j=1; j<=4; j++)
    {
        for(int k=0; k<=70000; k++)
        {
            dist[j][k]=INF;
        }
    }
    dist[2][0]=0;
    now.u=2,now.len=0;
    s.push(now);
    while(!s.empty())
    {
        now=s.front();
        // vis[now.u][now.len%mod]=0;
        s.pop();
        for(int v=1; v<=4; v++)
        {
            if(mp[now.u][v]!=INF)
            {
                nex.u=v;
                nex.len=now.len+mp[now.u][v];
                if(dist[v][nex.len%mod]>now.len+mp[now.u][v])
                {
                    dist[v][nex.len%mod]=now.len+mp[now.u][v];
                    if(vis[v][nex.len%mod]==0)
                    {
                        vis[v][nex.len%mod]=1;
                        s.push(nex);
                    }
                }
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll K,a,b,c,d;
        scanf("%I64d%I64d%I64d%I64d%I64d",&K,&a,&b,&c,&d);
        for(int i=1; i<=4; i++)
        {
            for(int j=1; j<=4; j++)
            {
                mp[i][j]=INF;
            }
        }
        mp[1][2]=mp[2][1]=a;
        mp[2][3]=mp[3][2]=b;
        mp[3][4]=mp[4][3]=c;
        mp[4][1]=mp[1][4]=d;
        mod=2*mp[1][2];
        SPFA();
        ll ans=INF;
        for(int i = 0; i < mod; i++)
        {
            if(dist[2][i]>=K)
            {
                ans=min(ans,dist[2][i]);
            }
            else
            {
                ll ned=K-dist[2][i];
                ll tmp;
                if(ned%mod==0)tmp=0;
                else tmp=mod;
                ans=min(ans,dist[2][i]+ned/mod*mod+tmp);
            }
        }
        printf("%I64d\n",ans);
    }
}