首页 > 代码库 > HDU 6071 同余最短路 spfa

HDU 6071 同余最短路 spfa

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 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
题意:求从2点回到2点的路径距离和大于等与K的最小值
题解:叉姐的分析
 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 long18 #define mod 100000000719 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[5][60004];26 int dp[5][60004];27 LL d[5][5];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[2][0]=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[1][2],&d[2][3],&d[3][4],&d[4][1]);81         d[2][1]=d[1][2];82         d[3][2]=d[2][3];83         d[4][3]=d[3][4];84         d[1][4]=d[4][1];85         bew=min(d[2][1],d[2][3]);86         dij();87         LL ans=1e18+1;88         for(LL j=0;j<2*bew;j++){89             if(dp[2][j]==-1)90                 continue;91             LL zhong=(max(0LL,x-dp[2][j]))/(2*bew);92           if(dp[2][j]+zhong*2*bew<x)93             zhong++;94          ans=min(ans,dp[2][j]+zhong*2*bew);95        }96        printf("%lld\n",ans);97     }98     return 0;99 }

 

HDU 6071 同余最短路 spfa