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 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 the shortest route: some roads may have obstacles (e.g.
bridge overpass, tunnels) which limit heights of the goods transported.
Therefore, the company would like to transport as much as possible each trip,
and then choose the shortest route that can be used to transport that
amount.

For the given cargo truck, maximizing the height of the goods
transported is equivalent to maximizing the amount of goods transported. For
safety reasons, there is a certain height limit for the cargo truck which cannot
be exceeded.

Input

The input consists of a number of cases. Each case
starts with two integers, separated by a space, on a line. These two integers
are the number of cities (C) and the number of roads (R). There are at most 1000
cities, numbered from 1. This is followed by R lines each containing the city
numbers of the cities connected by that road, the maximum height allowed on that
road, and the length of that road. The maximum height for each road is a
positive integer, except that a height of -1 indicates that there is no height
limit on that road. The length of each road is a positive integer at most 1000.
Every road can be travelled in both directions, and there is at most one road
connecting each distinct pair of cities. Finally, the last line of each case
consists of the start and end city numbers, as well as the height limit (a
positive integer) of the cargo truck. The input terminates when C = R = 0.

Output

For each case, print the case number followed by the
maximum height of the cargo truck allowed and the length of the shortest route.
Use the format as shown in the sample output. If it is not possible to reach the
end city from the start city, print "cannot reach destination" after the case
number. Print a blank line between the output of the cases.

Sample Input

5 6
1 2 7 5
1 3 4 2
2 4 -1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 10
5 6
1 2 7 5
1 3 4 2
2 4 -1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 4
3 1
1 2 -1 100
1 3 10
0 0

Sample Output

Case 1:

maximum height = 7

length of shortest route = 20

Case 2:

maximum height = 4

length of shortest route = 8

Case 3:

cannot reach destination

这一题是和某大神一起a的,结果被他说成了是一个人写的代码,哼,不再和他愉快的玩耍了!!!

好,不多说了,我们讨论这一题。

题目大意:

(1)这一题的输入数据有很多,第一行是两个数,表示城市和路,从第二行开始就是城市与城市之间的限高和路长。最后一行输入的是起始和终止点,还有一个就是卡车所能承受的最大高度。

(2)所以这题想输出的是从起始到结束最大高度下的最短路~~~注意题目所给的限高哦。

(3)还有一个就是当限高为-1时,那么这条路就是没有高度的限制。

(4)还有在中间有一个放行,小心PE。

(5)对了,还有最最后一个注意的就是这里采取二分来找能满足条件的最大高度,能够节省很大的时间~~

详见代码。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

struct node
{
    int h,len;
} map[1010][1010];

int start,end,height,c;
int node[1010];
const int inf=9999999;

int Spfa(int high)
{
    for (int i=1; i<=c; i++)
        node[i]=inf;
    queue<int>q;
    int inq[1010]= {0};
    int tm=start;
    node[tm]=0;
    inq[tm]=1;
    q.push(tm);
    while (!q.empty())
    {
        int s=q.front();
        q.pop();
        for (int i=1; i<=c; i++)
        {
            //cout<<s<<i<<" "<<node[i]<<" "<<map[s][i].len<<endl;
            if (map[s][i].h>=high&&node[i]>map[s][i].len+node[s])
            {
                node[i]=map[s][i].len+node[s];
                //cout<<"   "<<i<<" "<<node[i]<<endl;
                if (!inq[i])
                {
                    q.push(i);
                    inq[i]=1;
                }
            }
        }
        inq[s]=0;

    }
    if (node[end]!=inf)
        return node[end];
    else
        return -1;
}

int main ()
{
    int r,maxx,minn,h,k=1;
    while (cin>>c>>r&&(c||r))
    {
        int ans=-1,cmp=-1;
        for(int i=1; i<=c; i++)
        {
            for(int j=1; j<=c; j++)
            {
                map[i][j].len=inf;
                map[i][j].h=0;
            }
        }
        maxx=0,minn=inf;
        for (int i=1; i<=r; i++)
        {
            int a,b,len;

            cin>>a>>b>>h>>len;
            if(h==-1) h=inf;
            if (minn>h) minn=h;
            if (maxx<h) maxx=h;
            //cout<<minn<<" "<<maxx<<endl;
            if (map[a][b].len>len)
                map[a][b].len=map[b][a].len=len;
            if (map[a][b].h<h)
                map[a][b].h=map[b][a].h=h;
        }
        cin>>start>>end>>height;
        maxx=height>maxx?maxx:height;
        int l=minn,r=maxx;
        while (l<=r)
        {
            int mid=(r+l)>>1;
            //cout<<l<<" "<<r<<" "<<mid<<endl;
            int flag=Spfa(mid);
            if (flag!=-1)
            {
                l=mid+1;
                ans=mid;
                cmp=flag;
            }
            else
                r=mid-1;
        }
        /*for (int i=minn;i<=maxx;i++)//这就是所谓的超时的方法。。。
        {
            int flag=Spfa(i);
            if(flag!=-1)
            {
                ans=i;
                cmp=flag;
            }
        }*/
        if (k>1)
        printf ("\n");//注意这里的格式问题,小心PE哦~
        printf("Case %d:\n",k++);
        if(ans==-1)
            printf("cannot reach destination\n");
        else
        {
            printf ("maximum height = %d\n",ans);
            printf ("length of shortest route = %d\n",cmp);
        }
    //printf("\n");
    }
    return 0;
}

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

时间: 2024-10-14 04:47:39

hdu 2962 Trucking (二分+最短路Spfa)的相关文章

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 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 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 6071 同余最短路 spfa

Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 657    Accepted Submission(s): 284 Problem Description In HDU, you have to run along the campus for 24 times, or you will fail in P

HDU 1839 Delay Constrained Maximum Capacity Path(二分+最短路)

题目地址:HDU 1839 我去..原来这题这么简单...网络流中这种二分建图的方式做了一大堆了..这种题还能难倒我吗...白天一直没怎么看懂题,对题意懵懵懂懂的...晚上好好看了看题,这不就是网络流中练的最多的那种二分建图模型吗....只是把网络流算法改成最短路就行了..但是两个地方手残了没能在实验室当场A掉..sad... 这题就是二分最小容量,对满足容量的加边,对时间求最短路.如果最短时间比规定时间少的话就可以继续增加容量,直到不能增加为止. 代码如下: #include <iostrea

【HDU 1839】 Delay Constrained Maximum Capacity Path(二分+最短路)

[HDU 1839] Delay Constrained Maximum Capacity Path(二分+最短路) Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 481 Problem

洛谷P1462 通往奥格瑞玛的道路 二分答案+最短路SPFA

洛谷P1462 通往奥格瑞玛的道路二分答案+最短路SPFA 二分交费最多的一次的钱数 然后只将符合要求的边加入图中 如果到终点的最短路大于等于血量 或者直接起点不能到达终点那么说明不符合要求 需要加大答案 时间复杂度 (log答案)* Ek 需要注意如果本来就不能到达 那么直接输出AFK 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define For(i,j,k) for(int i=j;i<=k;i++) 4 using

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][

二分+最短路

二分+最短路 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 3662 Description 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人.该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线.