HDU--2962
2024-08-30 21:53:53
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962
分析:最短路+二分。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define ll long long
#define inf 1000000000
#define maxn 1005
using namespace std;
struct edge
{
int to,h,len;
edge(int x,int y,int z)
{
to=x;h=y;len=z;
}
};
vector<edge>e[maxn];
queue<int>q;
bool vis[maxn];
int d[maxn],n,m,s,t,H,maxh;
void spfa(const int s,int h)
{
while(!q.empty())q.pop();
for(int i=;i<=n;i++)
{
d[i]=inf;vis[i]=false;
}
d[s]=;vis[s]=true;
q.push(s);
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=;i<e[u].size();i++)
{
if(e[u][i].h>=h&&d[e[u][i].to]>d[u]+e[u][i].len)
{
d[e[u][i].to]=d[u]+e[u][i].len;
if(!vis[e[u][i].to])
{
vis[e[u][i].to]=true;
q.push(e[u][i].to);
}
}
}
vis[u]=false;
}
}
int main()
{
int cas=;
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==)break;
for(int i=;i<maxn;i++)e[i].clear();
int u,v,h,l;
for(int i=;i<m;i++)
{
scanf("%d%d%d%d",&u,&v,&h,&l);
if(h==-)h=inf;
e[u].push_back(edge(v,h,l));
e[v].push_back(edge(u,h,l));
}
scanf("%d%d%d",&s,&t,&H);
int lef=,r=H,maxh=,ans=inf;
while(lef<=r)
{
int mid=(r+lef)>>;
if(mid<=)break;
spfa(s,mid);
if(d[t]!=inf)
{
lef=mid+;
maxh=mid;
ans=d[t];
}
else r=mid-;
}
if(cas>) puts("");
printf("Case %d:\n",cas++);
if(ans==inf||maxh<)cout<<"cannot reach destination\n";
else {
printf("maximum height = %d\n",maxh);
printf("length of shortest route = %d\n",ans);
}
}
return ;
}
最新文章
- 去除项目中的SVN标记
- 绘图: Shape, Path
- duilib进阶教程 -- 扩展duilib的消息 (11)
- Android应用性能优化之使用SparseArray替代HashMap
- sharepint 数据视图 添加超链接
- magento如何获取某一产品的订单量代码
- 判断Windows操作系统的版本
- VS/Visual studio 源代码编辑器里的空处出现点号解决办法
- 常见的HTTP错误总结
- Eclipse查找类路径快捷方式
- SQL Server 127个SQL server热门资料汇总
- [C#]Array 添加扩展
- JS-运动框架
- 常用的 css 样式 记录
- Antlr v4入门教程和实例
- 学习day02
- [FWT] UOJ #310. 【UNR #2】黎明前的巧克力
- 《C#从现象到本质》读书笔记(八)第10章反射
- P10.3 usestock0.cpp
- [SSRS / RV] (.rdlc报表)冻结表头,固定行列标题