首页 > 代码库 > hdu 2962
hdu 2962
二分加最短路
#include <stdio.h>#include <string.h>#define N 1005#define INF 0x3f3f3f3fstruct tt{ int h,cost;}dis[N][N];int vis[N],d[N],num =1;int dijkstral(int v0,int t,int n,int h){ int i,temp,x,y; for(i = 1 ; i <= n ; i++) { if(dis[v0][i].h>=h){ d[i] = dis[v0][i].cost; }else d[i] = INF; vis[i] = 0; } d[v0] = 0; vis[v0] = 1; for(i = 1 ; i <= n ; i++) { temp = INF ; for(y = 1 ; y <= n ; y++) if(!vis[y] && temp>d[y]) temp = d[x=y]; if(temp == INF) return 0; vis[x] = 1; if(x == t) return 1; for(y = 1 ; y <= n ; y++) if(!vis[y] &&dis[x][y].cost+d[x]<d[y]&&dis[x][y].h>=h) d[y] = d[x] + dis[x][y].cost; } return 0;}int main(){ int R,C,s,t,h_limit,i,j,cas = 1; while(~scanf("%d %d",&C,&R),C+R) { for(i = 1 ; i <= C ; i++) { for(j = 1 ; j <= C ; j++) { dis[i][j].cost = (i == j) ?0:INF; dis[i][j].h = 0; } } while(R--) { int x,y,h,val; scanf("%d %d %d %d",&x,&y,&h,&val); if(h == -1) h = INF; dis[x][y].cost = dis[y][x].cost = val; dis[x][y].h = dis[y][x].h = h; } scanf("%d %d %d",&s,&t,&h_limit); int l = 1 ,r = h_limit,mid,ans; while(l<=r) { mid = l+(r-l)/2; if(dijkstral(s,t,C,mid)){ ans = d[t]; l = mid+1; }else r = mid-1; } if(cas>1) printf("\n"); printf("Case %d:\n",cas++); if(r<=0) printf("cannot reach destination\n"); else { printf("maximum height = %d\n",r); printf("length of shortest route = %d\n",ans); } } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。