首页 > 代码库 > 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;}