HDU 2962 Trucking 二分+最短路
来源:互联网 发布:php qq授权登陆 编辑:程序博客网 时间:2024/05/20 09:23
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=2962
题意
在一张地图中,一个运货车要从s运货到t,路中每条边有运输时间,有些边有运货限制(理解为路有路有承重限制),有些没有。给定运货车的最大运货量,求s到t能运货尽可能多的情况下,花费时间最少。
思路
二分枚举s->t的路中最小的h,将图中所有小于h的边都删去,跑最短路。
#include<cstdio>#include<queue>#include<iostream>#include<vector>#include<map>#include<cstring>#include<string>#include<set>#include<stack>#include<algorithm>#define cle(a) memset(a,0,sizeof(a))#define inf(a) memset(a,0x3f,sizeof(a))#define ll long long#define Rep(i,a,n) for(int i=a;i<=n;i++)using namespace std;#define INF2 9223372036854775807llconst int INF = ( 2e9 ) + 2;const ll maxn = 1010;const int maxm = maxn*maxn;struct edge{ int v,h,w,next;}e[maxm];struct node{ int u,d; bool operator < (const node &b)const { return d>b.d; }};int head[maxn],d[maxn],vis[maxn];int tot;void add(int u,int v,int h,int w){ e[tot].v=v; e[tot].h=h; e[tot].w=w; e[tot].next=head[u]; head[u]=tot++; e[tot].v=u; e[tot].h=h; e[tot].w=w; e[tot].next=head[v]; head[v]=tot++;}int dijkstra(int s,int t,int n,int lim){ for(int i=1;i<=n;i++)d[i]=INF; d[s]=0; priority_queue<node> q; memset(vis,0,sizeof(vis)); q.push(node{s,0}); while(!q.empty()) { node temp=q.top();q.pop(); int u=temp.u; if(vis[u])continue; vis[u]=1; for(int i=head[u];i!=-1;i=e[i].next) { if(e[i].h<lim)continue; int v=e[i].v; int w=e[i].w; if(d[v]>d[u]+w) { d[v]=d[u]+w; q.push(node{v,d[v]}); } } } return d[t];}int main(){ int n,m; int cas=0;// freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)&&(n+m)) { memset(head,-1,sizeof(head)); tot=0; for(int i=0;i<m;i++) { int u,v,h,w; scanf("%d%d%d%d",&u,&v,&h,&w); if(h==-1)h=INF; add(u,v,h,w); } int s,t,r; scanf("%d%d%d",&s,&t,&r); int l=0; int ans=INF; while(l<=r) { int mid=(l+r)>>1; int temp=dijkstra(s,t,n,mid); if(temp!=INF) { l=mid+1; ans=temp; } else r=mid-1; } if(cas!=0)puts(""); 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 0
- HDU 2962 Trucking 最短路+二分
- 【HDU】2962 Trucking 二分+最短路
- hdu 2962 Trucking (二分+最短路Spfa)
- hdu 2962 Trucking(二分+最短路)
- HDU 2962 Trucking 二分+最短路
- HDU 2962 Trucking 最短路
- HDU-2962-Trucking(最短路)
- HDU 2962 Trucking 最短路。。
- HDU 2962 Trucking 最短路
- HDU 2962 Trucking(二分+带限制最短路)
- hdu 2962 Trucking (最短路之SPFA算法 + 二分)
- hdu 2962 Trucking 最短路+二分。。Dijkstra+SPFA两种算法实现。
- hdu 2962 Trucking(二分最大可行的高度+最短路dijkstra)
- HDU 2962 Trucking 二分+最短路(带限制最短路)\\不满足递推关系,不能直接是spfa
- 【最短路+dijkstra+spfa】杭电 hdu 2962 Trucking
- hdu 2962 Trucking 二分 + Dijsktra
- HDU 2962 Trucking(Dijkstra+二分)
- hdu 2962 Trucking【二分+SPFA】
- 使用mysql数据库时InnoDB和MyISAM两个存储引擎的区别
- Centos7 安装Rabbitmq
- web前端代码学习第一天
- Kafka Consumer Rebalance
- HDU2089 不要62(数位dp)
- HDU 2962 Trucking 二分+最短路
- 爱奇艺校招
- 基于SSM实现的简易员工管理系统(基于阿里云的网站上线篇)
- 训练日记-21
- 机器学习-线性回归算法
- 部分贪心思想在信息学竞赛中的应用
- java常见字符串方法总结
- Sql Server 2008中存储过程传入表值参数
- bzoj 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚(DP)