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");
}
}
题目大意:给一个图,其中边的权值包括长度和高度的限制,现在给你一个起点和终点,问你能够到达终点的最高的货物高度,以及此时的最短路径。
思路很简单,直接对高度进行二分,再加上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");
}
}
相关文章推荐
- 什么是Spring的依赖注入。
- iOS之AFN错误代码1016(Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable)
- 冲刺第二阶段第二天
- Git免密码提交
- 纯css三级导航
- 正则_学习笔记
- Unreal Engine 4 —— 使用反汇编来确定该进行优化的地方
- python 学习笔记十四 jQuery案例详解(进阶篇)
- 10个常见的Node.js面试题
- css3的背景坐标
- hard link vs soft link
- Android 使用第三方SDK—友盟实现分享功能
- Python2学习笔记(2)
- vim的最最最基本操作
- MySQL如何选择float, double, decimal
- MongoDB JAVA API Filters
- C++小里程
- SpringMVC Controller写法
- Ajax-基础篇
- Android Navigation TabBar控件实现多彩标签栏