HDU 2962 Trucking: Second-band Limited shortest circuit

Source: Internet
Author: User
Tags cas int size integer printf time limit

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=2962

Topic:

Trucking
Time limit:20000/10000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1211 accepted submission (s): 428

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 shortest route:some roads could 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 the can be used to transport that amount.

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

Input
The input consists of a number of cases. Each case is starts with two integers, and separated by a spaces, 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 "road height maximum Wed on this road, and the length of that road. The maximum height for each road was a positive integer, except that a height of-1 indicates this there is no height limit On that road. The length of each road are a positive integer at most 1000. Every Road can be travelled in both directions, and there are 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 as the "the" Height limit (a positive in Teger) 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 EST route. Use the format as shown in the sample output. If It isn't possible to reach the end of the 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 2 5 2 4 3 4 1 4 5 8 5 1 5 5 6 1 2 7 5 1 3 4 2
2 4-1 2 5 2 4 3 4 1 4 5 8 5 1 5 4 3 1 1
2-1
1
3 0 0

Sample Output

Case 1:
Maximum height = 7
length of shortest route =
  
2:
Maximum height = 4 length of
Sho Rtest route = 8 Case
  
3:
cannot reach destination

The main effect of the topic:

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

On a map, each path has a length, and its limit height. A truck is going from A to B, the truck's maximum can load H-height cargo. What is the shortest path when the truck can hold the highest cargo from a to B?
Analysis and Summary:

Similar to the HDU 1839 phase. The cargo height of the truck load h from small to large, the number of paths to A to B that satisfies the condition is decremented in descending order until there is no path. Therefore, the tabulation of the truck can be divided into two points in order to find the shortest way, which satisfies the monotonicity. If you can find the shortest, then left=mid+1, otherwise right=mid.

Note that you can find the shortest possible time, but also to save the answer.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std  
    
;  
const int INF = 0x7fffffff;  
const int VN = 1005;  
    
const int EN = VN*VN/2;  
    struct edge{int v,next;  
int h, Len;  
    
}e[en];  
int n;  
int m;  
int size;  
int HEAD[VN];  
int D[VN];  
int limit;  
    
BOOL INQ[VN];  
    void Init () {size=0;  
Memset (Head,-1, sizeof (head));  
    } void Addedge (int u,int v,int h,int l) {e[size].v=v;  
    if (h!=-1) e[size].h=h;  
    else E[size].h=inf;  
    E[size].len=l;  
    E[size].next=head[u];  
head[u]=size++;  
    int SPFA (int src, int end) {for (int i=1; i<=n; ++i) D[i]=inf;  
    memset (inq, 0, sizeof (INQ));  
    queue<int>q;  
    D[SRC] = 0;  
    Q.push (SRC);  
        while (!q.empty ()) {int U=q.front (); Q.pop ();  
        Inq[u]=false; for (int e=head[u]; E!=-1 e=e[e].next) if (e[e].h>=limit) {int tmp = D[u]+e[e].len;  
                if (D[E[E].V] > tmp) {D[E[E].V] = tmp;  
                    if (!INQ[E[E].V]) {inq[e[e].v]=true;  
                Q.push (E[E].V);  
}}} return d[end];  
    int main () {int cas=1,u,v,h,l,start,end,hh;  
        while (~SCANF ("%d%d", &n,&m) && n+m) {if (cas!=1) puts ("");  
        Init ();  
            for (int i=0; i<m; ++i) {scanf ("%d%d%d%d", &u,&v,&h,&l);  
            Addedge (u,v,h,l);  
        Addedge (v,u,h,l);  
            
        } scanf ("%d%d%d", &start,&end,&hh);  
    
        int ans,ans_h=0,ans_len=inf,left=0, right=hh+1;  
            while (left<right) {limit = (left+right) >>1;  
            ANS=SPFA (start, end);  
                if (ans!=inf) {left=limit+1;  
              if (limit>ans_h) {ans_h=limit;      Ans_len=ans;  
                else if (Limit==ans_h&&ans<ans_len) {Ans_len=ans;      
            }} else{Right=limit;  
        } printf ("Case%d:\n", cas++);  
        if (Ans_len==inf) {puts ("Cannot reach destination");  
            else{printf ("Maximum height =%d\n", ans_h);  
        printf ("Length of shortest route =%d\n", Ans_len);  
} return 0; }

Author: csdn Blog shuangde800

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.