HDU2962-Trucking(二分+最短路)

    xiaoxiao2022-07-03  111

    题目链接

    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


    题目大意

    n个城市,m条路径,路径有长度和载货最高高度,-1是无限高度。卡车载货高度高于路径的载货最高高度就不能通过。求卡车最高的载货高度和最短路径。

    思路

    用二分列出一系列的高度,假设卡车的高度就是二分得到的数,然后用dijkstra计算最小路径(如果是inf就是不能到达)。

    这道题目是前几天西安邀请赛的类似的题目。当时用的DFS暴力搜索出全部从起点到终点的路径,然后计算结果ans,求个最优的ans。但是超时了.....听说队长用BFS就过了........后来找了HDU这道类似题目用DFS和BFS写,提交,果然DFS还是超时,可能是递归调用太费时间??BFS内存超了。不过ACM好像内存有512MB,不太会爆内存,然后那道M题用BFS就过了= = ,DFS就是超时。我......

    上面是BFS,下面是DFS,使用二分+搜索写的,不用二分也差不多....HDU对内存限制了,二分+BFS内存超了一点。所以得用dijkstra。

    对DFS、BFS和dijkstra的理解:DFS和BFS类似,但是DFS超时严重,以后用BFS!!DFS和BFS虽然慢,但是他们可以实现的功能比dijkstra强太多,dijkstra只能求最短路,DFS和BFS不止是图的搜索了。

    这种题目是图的搜索,但是有一些限制条件,然后求个最优的解。方法就是二分+dijkstra。二分列出可能的最优解,用dijkstra去判断是否可行。而且这个二分的范围是递增的,比如说1-10,1-8用dijkstra判断都是可行的,9和10是不可行的,最大的就是8。即大于某个数都是不可行,小于某个数都是可行。这是二分的基础。

    另外Presentation Error的错误,需要注意最后一个测试的后面没有\n,所以需要对测试的个数T进行判断,T!=1时在输出之前输出一个\n 。

    代码参考:https://blog.csdn.net/Lionel_D/article/details/44874443


    #include<bits/stdc++.h> #define ll long long #define inf 0x3f3f3f3f using namespace std; const int Max = 1010; int n,m,sx,ex,limit; struct node { int h,step; }; node maze[Max][Max]; int vis[Max],dis[Max]; int dijkstra(int lim) { memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) dis[i] = inf; dis[sx] = 0; while(1){ int u,Min=inf; for(int i=1;i<=n;i++){ if(!vis[i]&&Min>dis[i]){ Min = dis[i]; u = i; } } if(Min==inf) return dis[ex]; vis[u] = 1; for(int i=1;i<=n;i++){ if(dis[i]>dis[u]+maze[i][u].step && (maze[i][u].h>=lim||maze[i][u].h==-1 ) ){ dis[i] = dis[u]+maze[i][u].step; } } } } int main() { int T=1; while(~scanf("%d %d",&n,&m)) { if(n==0&&m==0) return 0; if(T!=1) printf("\n"); memset(maze,0,sizeof(maze)); for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) maze[i][j].step = maze[j][i].step = inf; int a,b,h,x; for(int i=0;i<m;i++){ scanf("%d%d%d%d",&a,&b,&h,&x); maze[a][b].h = maze[b][a].h = h; maze[a][b].step = maze[b][a].step = x; } scanf("%d%d%d",&sx,&ex,&limit); int l=1,r=limit,mid,ans=inf; while(l<=r) { mid = (l+r)/2; int min_step = dijkstra(mid); if(min_step!=inf){ ans = min_step; l = mid+1; } else r = mid-1; } printf("Case %d:\n",T++); if(ans!=inf)printf("maximum height = %d\nlength of shortest route = %d\n",r,ans); else printf("cannot reach destination\n"); } return 0; }

     


     

    最新回复(0)