HDU 6071 最短路思维题

Lazy Running

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


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.
 

题意:小Q要跑步打卡,有四个点,他从第二个点开始跑,给出一个K,d1,d2,d3,d4。分别对应1到2,2到3,3到4,4到1的距离

K是你要跑的最少长度,跑步必须跑完一段路,比如从1到2点,不能中途返回

要你求出大于等于K的最小值。

思路:与2这个点连接的有d1和d2这两条边,取小的那条 的两倍作为 m,你只要跑回2点之后,不够的话再跑m一直跑到够就ok了,但是在补m之前的跑路也要是最优的

用一个dis[i][j]表示到i点,且路程为j的时候距离,可以写成dis[i][j % m],因为你乱跑跑到 i 用了 j 的距离,和你用最短路到达 i ,然后补m,补成 j 这两种走法是一样的,

这样mod m 不但可以省空间,还可以降低查询时间。

最后取查询一遍dis[1][j] (j 从 0 到 m)数组,取最小值,如果刚好是k就输出,如果不够,就补m。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
#define INF 2000000000000000000
#define ll long long
#define MAXN 100005
struct Edge{
	int y;
	ll w;
};
vector<Edge>e[5];
ll dis[4][MAXN],k,m;
struct par{
	ll x;
	int y;
	bool operator < (const par &a) const {  
        return x>a.x;//最小值优先  
    }  
};
inline void Dijkstra(int id){
	priority_queue<par> q;
	for(int i = 0;i < 4;i++){
		for(int j = 0;j <= m;j++){
			dis[i][j] = INF;
		}
	}
	par p;
	p.x = 0ll;
	p.y = id;
	q.push(p);
	while(!q.empty()){
		ll w = q.top().x;
		int y = q.top().y;
		q.pop();
		if(w > dis[y][w % m])  // 如果这段路程已经比最优情况长了,那就不用继续下去了 
			continue;
		for(int i = 0;i < e[y].size();i++){
			int nxt = e[y][i].y;
			ll dist = w + e[y][i].w;
			if(dis[nxt][dist % m] > dist){  //由于最后都用 m 去补,所以dist可以直接mod m  
				dis[nxt][dist % m] = dist;	// 多的部分你换成去跑 m 和你真实去跑是一样的 
				par temp;
				temp.x = dist;
				temp.y = nxt;
				q.push(temp);
			}
		} 
	}
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int d1,d2,d3,d4;
		memset(e,0,sizeof(e));
		scanf("%lld %d %d %d %d",&k,&d1,&d2,&d3,&d4);
		m = 2 * min(d1,d2);
		e[0].push_back(Edge{1,d1});
		e[1].push_back(Edge{0,d1});
		
		e[1].push_back(Edge{2,d2});
		e[2].push_back(Edge{1,d2});
		
		e[2].push_back(Edge{3,d3});
		e[3].push_back(Edge{2,d3});
		
		e[0].push_back(Edge{3,d4});
		e[3].push_back(Edge{0,d4});
		
		Dijkstra(1);
		ll ans = INF;
		for(int i = 0;i < m;i++){
			ll dist = k - dis[1][i];
			if(dist <= 0){
				ans = min(ans,dis[1][i]);		//如果刚好到起点跑完,那就是那么长,不用补 m 
			}else{
				ans = min(ans,dis[1][i] + dist / m * m + (dist % m > 0) * m);
			}	
		}
		printf("%lld\n",ans);
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值