• hdu 2962


    二分加最短路

    #include <stdio.h>
    #include <string.h>
    #define N 1005
    #define INF 0x3f3f3f3f
    struct 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("
    ");
            printf("Case %d:
    ",cas++);
            if(r<=0) printf("cannot reach destination
    ");
            else {
                printf("maximum height = %d
    ",r);
                printf("length of shortest route = %d
    ",ans);
            }
    
        }
        return 0;
    }
  • 相关阅读:
    将表中数据生成SQL语句
    遍历页面所有的控件
    MSN不能登陆
    刷新框架页
    JS传参出现乱码
    iframe攻击
    有关于VS调试Javascript的问题
    C#中StringBuilder类的使用
    前瞻XAML
    Asp.Net在SqlServer中的图片存取
  • 原文地址:https://www.cnblogs.com/llei1573/p/3912919.html
Copyright © 2020-2023  润新知