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("\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;
}

hdu 2962

时间: 2024-10-14 04:55:14

hdu 2962的相关文章

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

UVALive 4223 / HDU 2962 spfa + 二分

Trucking Problem Description A certain local trucking company would like to transport some goods on a cargo truck from one place to another. It is desirable to transport as much goods as possible each trip. Unfortunately, one cannot always use the ro

HDU 2962 Trucking

二分查找+最短路 二分限制高度,然后求最短路 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<algorithm> using namespace std; const int INF=0x7FFFFFFF; const int maxn=1000+10; struct Edge { int fro

hdu 2962 Trucking 最短路+二分。。Dijkstra+SPFA两种算法实现。

Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1966    Accepted Submission(s): 680 Problem Description A certain local trucking company would like to transport some goods on a cargo

hdu 2962 最短路+二分

题意:最短路上有一条高度限制,给起点和最大高度,求满足高度最大情况下,最短路的距离 不明白为什么枚举所有高度就不对 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 const int maxint=999999; 5 int c[1005][1005][2],dist[1005],H[1005]; 6 using namespace std; 7 int n,line; 8 int i,j,k

hdu 2962 Trucking (最短路径)

Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1692    Accepted Submission(s): 587 Problem Description A certain local trucking company would like to transport some goods on a cargo

hdu 2962 题解

题目 题意 给出一张图,每条道路有限高,给出车子的起点,终点,最高高度,问在保证高度尽可能高的情况下的最短路,如果不存在输出 $ cannot ?reach ?destination $ 跟前面 $ hdu5418 $ 一样的,题目挺基础的,但是在细节方面比较抠.要是最高度尽可能高,我们就可以想去枚举可能的高度去跑 $ spfa $ ,但我们可以发现这样效率太低了,那么我们为什么不二分答案呢?给出最高高度了,最低不就是 $ 0 $ 吗?二分答案再去跑 $ spfa $ 加上限制条件. 这题时限

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123