HDU 2962 最短路+二分

Trucking

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1488    Accepted Submission(s): 515


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
 

 

解题思路: 和前一题一样。

代码:

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#include<list>
#include<set>
#include<queue>
#include<stack>
using namespace std;
const int inf=100000000;
const int maxn=300010;
int dist[maxn],head[maxn],tol,m,n,ed;
struct Edge
{
        int to,next,val,cap;
}edge[10*maxn];
void add(int u,int v,int p,int w)
{
        edge[tol].to=v;
        edge[tol].val=w;
        edge[tol].cap=p;
        edge[tol].next=head[u];
        head[u]=tol++;
}
struct Node
{
        int id,dist;
        Node(int a=0,int b=0):id(a),dist(b){}
        bool operator < (const Node &b) const
        {
                return dist>b.dist;
        }
};
int fun(int st,int cap)
{
        int i,j,u,v;
        priority_queue<Node> q;
        for(i=1;i<=n;i++)dist[i]=inf;dist[st]=0;
        q.push(Node(st,0));
        while(!q.empty())
        {
                Node ret=q.top();q.pop();
                u=ret.id;
                if(u==ed)return 1;
                if(dist[u]<ret.dist)continue;
                for(i=head[u];i!=-1;i=edge[i].next)
                if(cap<=edge[i].cap)
                {
                        v=edge[i].to;
                        if(dist[v]>dist[u]+edge[i].val)
                        {
                                dist[v]=dist[u]+edge[i].val;
                                q.push(Node(v,dist[v]));
                        }
                }
        }
        return 0;
}
int main()
{
        int i,j,k,T,p,ncase=0;
        while(~scanf("%d%d",&n,&m))
        {
                if(n==0&&m==0)break;
                if(ncase!=0)puts("");
               memset(head,-1,sizeof(head));tol=0;
               while(m--)
               {
                       scanf("%d%d%d%d",&i,&j,&k,&p);
                       if(k==-1)k=inf;
                       add(i,j,k,p);
                       add(j,i,k,p);
               }
               int left=0,right,mid;
               int st;
               scanf("%d%d%d",&st,&ed,&p);
               right=p;
               while(left<right)
               {
                       mid=(left+right+1)>>1;
                       if(fun(st,mid))left=mid;
                       else right=mid-1;
               }
                printf("Case %d:\n",++ncase);
                if(left==0) puts("cannot reach destination");
                else
                {
                           printf("maximum height = %d\n", left);
                           fun(st,left);
                          printf("length of shortest route = %d\n", dist[ed]);
                }
               
        }
        return 0;
}

 

posted @ 2013-09-16 23:05  线性无关  阅读(121)  评论(0编辑  收藏  举报