hdu2962(最短路+二分)

题意:在最大的高度下面求最短路,由于题目给出限高,所以我们只需要二分高度然后用SPFA

#include <iostream>
#include <string.h>
#include <queue>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 205
#define maxn 2005
const int INF = 9999999;
int ht[maxn][maxn];
int mp[maxn][maxn];
int d[maxn];
int mid_ht;
int n,m;
int used[maxn];

void SPFA(int s)
{
    fill(d,d+n+1,INF);
    queue<int> que;
    fill(used,used+n+1,0);
    d[s] = 0;
    que.push(s);
    while(!que.empty())
    {
        int u = que.front();que.pop();
        used[u] = 0;
        for(int i = 1; i <= n; i++)
        {
            if(ht[u][i] >= mid_ht && ht[u][i]|| ht[u][i] == -1)
            {
               if(d[i] > d[u] + mp[u][i])
               {
                   d[i] = d[u] + mp[u][i];
                   if(!used[i])
                   {
                       used[i] = 1;
                       que.push(i);
                   }

               }
            }
        }
    }

}

int main()
{
    #ifdef xxz
    freopen("in.txt","r",stdin);
    #endif // xxz
    int a,b,c,e;
    int Case = 1;
    while(~scanf("%d%d",&n,&m))
    {
        if(n == 0 && m== 0) break;
        for(int i = 0; i <= n; i++)
        {
            fill(mp[i],mp[i]+n+1,INF);
            fill(ht[i],ht[i]+n+1,0);
        }

        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d%d",&a,&b,&c,&e);
            mp[a][b] = mp[b][a] = e;
            ht[a][b] = ht[b][a] = c;
        }

        int start,End,h;
        scanf("%d%d%d",&start,&End,&h);
        int l = 0, r = h;
        int ans = 0 ,ans2 = INF;

        while(l <= r)
        {
            mid_ht = (l+r)/2;
            SPFA(start);
            if(d[End] == INF)
            {
                r = mid_ht - 1;
            }
            else {
                if(ans < mid_ht)
                {
                    ans = mid_ht;
                    ans2 = d[End];
                }
                else if(ans == mid_ht && d[End] < ans2)//注意这一项,当高度相同时考虑更小的路径
                {
                    ans2 = d[End];
                }
                l = mid_ht+1;
            }
        }
        if(Case >1 ) printf("\n");//不然会PE
        printf("Case %d:\n",Case++);
        if(ans2 == INF || ans == 0)
        {

            printf("cannot reach destination\n");
        }
        else {

            printf("maximum height = %d\n",ans);
            printf("length of shortest route = %d\n",ans2);
        }

    }
    return 0;
}
时间: 2024-12-11 13:15:15

hdu2962(最短路+二分)的相关文章

hdu2962 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 roads in th

HDU1839_Delay Constrained Maximum Capacity Path(最短路+二分)

解题报告 http://blog.csdn.net/juncoder/article/details/38349019 题目传送门 题意: 有N个点,点1为珍贵矿物的采矿区, 点N为加工厂,有M条双向连通的边连接这些点.走每条边的运输容量为C,运送时间为D. 他们要选择一条从1到N的路径运输, 这条路径的运输总时间要在T之内,在这个前提之下,要让这条路径的运输容量尽可能地大. 一条路径的运输容量取决与这条路径中的运输容量最小的那条边. 思路: 二分容量建图,spfa判时间是否符合条件 #incl

POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

CSU 1307 最短路+二分

题目大意: 帮忙找到一条a到b的最短路,前提是要保证路上经过的站点的最大距离尽可能短 这道题居然要用到二分...完全没去想过,现在想想求最大距离的最小值确实是... 这里不断二分出值代入spfa()或者dijkstla()中计算a到b的最短距离,每次都保证只经过边小于mid值的路径 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 #incl

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

poj3662 最短路+二分

1 //Accepted 508 KB 79 ms 2 //spfa+二分 3 //二分需要的花费cost,把图中大于cost的边设为1,小于cost的边设为0,然后spfa求 4 //最短路,如果小于K则可行,继续二分 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 #include <queue> 9 #include <cmath> 10 #include &

POJ3662Telephone Lines(最短路+二分)

传送门 题目大意:n个点p条边,每条边有权值,让1和n点联通,可以将联通1--n的边选k条免费, 求剩下边权的最大值. 题解:二分一个答案x,大于x的边权设为1,小于等于x的边权设为0,跑最短路. 若从1到n的最短路dis[n]<=k,则可以通过免费k条边,答案为x. 代码: #include<iostream> #include<cstdio> #include<queue> #include<cstring> #include<algorit

poj 3662 Telephone Lines(最短路+二分)

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of

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