hdu 2962(最短路+二分)
2013-04-15 22:27
295 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962
思路:二分枚举高度,每次都sfpa一下,然后如果dist[e]存在的话,就更新shortpath...
View Code
思路:二分枚举高度,每次都sfpa一下,然后如果dist[e]存在的话,就更新shortpath...
View Code
#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<algorithm> const int MAXN=1000+10; const int inf=1<<30; using namespace std; struct Node{ int v,w,h; }; vector<Node>mp[MAXN]; int n,m; int s,e; int dist[MAXN]; bool visited[MAXN]; void SPFA(int u,int limit){ for(int i=1;i<=n;i++)dist[i]=inf; dist[u]=0; memset(visited,false,sizeof(visited)); queue<int>Q; Q.push(u); while(!Q.empty()){ int u=Q.front(); Q.pop(); visited[u]=false; for(int i=0;i<mp[u].size();i++){ int v=mp[u][i].v; int h=mp[u][i].h; int w=mp[u][i].w; if(h<limit)continue;//h应该尽量要大 if(dist[u]+w<dist[v]){ dist[v]=dist[u]+w; if(!visited[v]){ Q.push(v); visited[v]=true; } } } } } int main(){ int _case=1; while(~scanf("%d%d",&n,&m)&&(n+m)){ if(_case>1)puts(""); for(int i=1;i<=n;i++)mp[i].clear(); for(int i=1;i<=m;i++){ int u,v,w,h; scanf("%d%d%d%d",&u,&v,&h,&w); Node p1,p2; p1.v=u,p2.v=v; p1.w=p2.w=w; if(h==-1)h=inf; p1.h=p2.h=h; mp[u].push_back(p2); mp[v].push_back(p1); } int limit; scanf("%d%d%d",&s,&e,&limit); //二分的时候得注意了,不知为什么wa了好多次 int low=0,high=limit,res=inf; while(low<high){ int mid=(low+high+1)>>1; SPFA(s,mid); if(dist[e]==inf){ high=mid-1; }else { low=mid; res=dist[e]; } } printf("Case %d:\n",_case++); if(res==inf){ printf("cannot reach destination\n"); }else { printf("maximum height = %d\n",low); printf("length of shortest route = %d\n",res); } } return 0; }
相关文章推荐
- hdu 2962 Trucking(二分+最短路)
- Trucking(HDU 2962 最短路+二分搜索)
- HDU 2962 Trucking 二分+最短路(带限制最短路)\\不满足递推关系,不能直接是spfa
- hdu 2962 Trucking (二分+最短路Spfa)
- hdu 2962 Trucking (二分+最短路Spfa)
- hdu 2962(二分答案+最短路)
- HDU - 2962 Trucking 最短路加二分
- HDU 2962 Trucking 【二分 + 最短路】
- hdu 2962 Trucking(二分最大可行的高度+最短路dijkstra)
- hdu 2962(最短路+二分)
- HDU 2962 Trucking 最短路+二分
- HDU 2962 Trucking 二分+最短路
- hdu 2962 Trucking (最短路之SPFA算法 + 二分)
- 【HDU】2962 Trucking 二分+最短路
- HDU 4606 Occupy Cities (计算几何+最短路+二分+最小路径覆盖)
- HDU 1839 Delay Constrained Maximum Capacity Path 最短路+二分
- HDU 3468 Treasure Hunting(二分匹配+最短路)
- hdu Trucking 2962 Dijs + 二分 BFS+ priority——queue SPAF + 邻接表
- hdu 1839 Delay Constrained Maximum Capacity Path(最短路+二分)
- hdu 1839(二分+最短路)