HODJ 2962 Trucking

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962

题目大意:给一个图,其中边的权值包括长度和高度的限制,现在给你一个起点和终点,问你能够到达终点的最高的货物高度,以及此时的最短路径。

思路很简单,直接对高度进行二分,再加上SPFA求最短路就可行了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1000+2;
int head[maxn],dis[maxn],vis[maxn],n,tot;
struct Edge
{
	int u,v,w,h,next;
}edge[maxn*maxn];
void addEdge(int u,int v,int w,int h)
{
	edge[tot].u = u;
	edge[tot].v = v;
	edge[tot].w = w;
	edge[tot].h = h;
	edge[tot].next = head[u];
	head[u] = tot++;
}
void SPFA(int s, int lim)
{
	queue<int> q;
	for(int i=1; i<=n; i++)
		dis[i] = INF, vis[i] = 0;
	dis[s] = 0;
	q.push(s);
	vis[s] = 1;
	while(!q.empty())
	{
		int u = q.front();
		q.pop();
		vis[u] = 0;
		for(int i=head[u]; i!=-1; i=edge[i].next)
		{
			int v = edge[i].v;
			if(edge[i].h < lim) continue;
			if(dis[u] + edge[i].w < dis[v])
			{
				dis[v] = dis[u] + edge[i].w;
				if(!vis[v])
				{
					q.push(v);
					vis[v] = 1;
				}
			}
		}
	}
}
int main()
{
	int m,cas = 1;
	while(scanf("%d%d",&n,&m) && (n+m))
	{
		if(cas > 1) printf("\n");
		tot = 0;
		memset(head,-1,sizeof(head));
		for(int i=0; i<m; i++)
		{
			int u,v,w,h;
			scanf("%d%d%d%d",&u,&v,&h,&w);
			if(h == -1) h = INF;
			addEdge(u,v,w,h);
			addEdge(v,u,w,h);
		}
		int s,e,lim;
		scanf("%d%d%d",&s,&e,&lim);
		int l = 0, r = lim, ans = INF, mid;
		while(l <= r)
		{
			mid = (l+r)>>1;
			SPFA(s,mid);
			if(dis[e] != INF)
			{
				l = mid + 1;
				ans = dis[e];
			}
			else r = mid-1;
		}
		printf("Case %d:\n",cas++);
		if(ans != INF)
		{
			printf("maximum height = %d\n",r);
			printf("length of shortest route = %d\n",ans);
		}
		else printf("cannot reach destination\n");
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值