2017多校训练赛第四场 HDU 6071(最短路)

Lazy Running

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

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 thanK 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 pi−1 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.

2017多校训练赛第四场 HDU 6071(最短路)_第1张图片

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 thanK.

Input

The first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.
In each test case, there are 5 integers K,d1,2,d2,3,d3,4,d4,1(1≤K≤1018,1≤d≤30000), 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

2017 Multi-University Training Contest - Team 4

 

 

        比赛的时候就是蠢……

        当时只剩下十几分钟的时候看到这题的……其实很简单的一道最短路,前提是想到了……

        题目要求是只有四个点,然后连边成正方形,问从2号点出发,再回到2号点且走过的总距离大于K的最短的路径是多少。对于这个,我们考虑如果存在一条合法路径,那么我再走2*w也一定是合法路径,其中w表示与2相连的某条边的长度。即回到2之后再出去再回来,这样的路径一定合法。那么,如果走了某条路径回到了2,然后总距离不够的话,我们只需要加上几个2*w使得最后结果大于等于K即可。

        那么如何使这个结果最小呢?我们考虑设置一个数组d[x][p]表示从2出发最后到达x点且费用对2*w取模结果为p时的最小花费。这样子原本的一个点就可以拆成2*w个点,这样跑一遍dijkstra即可求出d数组。之后,对于每一个对2*w取模后的数值,我们都可以计算把它补到大于K且距离K最近的花费,再在这些花费中取一个最小的即可。那么,这样子考虑为什么可以包含全部的而且最优的解呢?因为我们最后补的是2*w或者它的倍数,然后我把对2*w取模后的所有取值的最小花费都计算了一次,这样子得出来的最小花费一定包含所有的情况,即2*w的所有剩余系都被包括了,所以可以保证正确性。具体代码如下:

#include
#define LL long long
using namespace std;

struct Edge{int y;LL w;};
typedef pair P;
vector g[4];
LL d[4][100000],K,m;
int n,s;

inline void dijkstra(int s)
{
    priority_queue,greater

> q; for(int i=0;id[j][w%m]) continue; for(int k=0;kdist) //更新的时候更新取模后对应的点 { d[y][dist%m]=dist; q.push(P(dist,y)); } } } } int main() { n=4; s=1; int T_T; cin>>T_T; while(T_T--) { int d1,d2,d3,d4; memset(g,0,sizeof(g)); scanf("%I64d%d%d%d%d",&K,&d1,&d2,&d3,&d4); g[0].push_back(Edge{1,d1}); g[1].push_back(Edge{0,d1}); g[1].push_back(Edge{2,d2}); g[2].push_back(Edge{1,d2}); g[2].push_back(Edge{3,d3}); g[3].push_back(Edge{2,d3}); g[3].push_back(Edge{0,d4}); g[0].push_back(Edge{3,d4}); m=2*min(d1,d2); //为了使效率更高,w取两条与2相连的边中小的那个 dijkstra(s); LL ans=1e18; int p; for(int i=0;i0)*m); } printf("%I64d\n",ans); } return 0; }

 

 

你可能感兴趣的:(HDU,2017HDU多校赛)