您的位置:首页 > 其它

HODJ 2962 Trucking

2016-05-25 11:40 106 查看
题目链接: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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: