UVALive 4223 / HDU 2962 spfa + 二分

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVALive 4223 / HDU 2962 spfa + 二分相关的知识,希望对你有一定的参考价值。

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 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 <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<queue>
using namespace std ;
typedef long long ll;

const int N = 2000000 + 10;
const int inf = 1e9 + 7;

int dis[N],head[N],vis[N],t,n,m,T;
struct ss{
    int to,h,v,next;
}e[N];
void add(int u,int v,int h,int w) {
    e[t].to = v;
    e[t].next = head[u];
    e[t].v = w;
    e[t].h = h;
    head[u] = t++;
}
int spfa(int x,int limt) {
    queue<int >q;
    for(int i = 0; i <= n; i++) dis[i] = inf, vis[i] = 0;
    dis[x] = 0;
    q.push(x);
    vis[x] = 1;
    while(!q.empty()) {
        int k = q.front();
        q.pop();vis[k] = 0;
        for(int i = head[k]; i; i = e[i].next) {
            if(e[i].h < limt) continue;
            if(dis[e[i].to] > dis[k] + e[i].v) {
                dis[e[i].to] = dis[k] + e[i].v;
                if(!vis[e[i].to]) {
                    vis[e[i].to] = 1;
                    q.push(e[i].to);
                }
            }
        }
    }
    return dis[T];
}
int main() {
    int a,b,h,v,S,cas =  1;
    while(~scanf("%d%d",&n,&m)) {
        if(!n || !m) break;
         if (cas > 1) printf ("\n");
        t = 1; memset(head,0,sizeof(head));
        for(int i = 1; i <= m; i++) {
            scanf("%d%d%d%d",&a,&b,&h,&v);
            if(h == -1) h = inf;
            add(a,b,h,v);
            add(b,a,h,v);
        }
        scanf("%d%d%d",&S,&T,&h);
        int l = 0, r = h, ans = inf;
        while(l < r) {
            int mid = (l + r + 1) >> 1;
            if(spfa(S,mid) != inf) l = mid, ans = dis[T];
            else r = mid - 1;
        }
         printf ("Case %d:\n", cas++);
         if(ans != inf)  printf ("maximum height = %d\nlength of shortest route = %d\n", l, ans);
         else {
           printf("cannot reach destination\n");
         }
    }
    return 0;
}

 

以上是关于UVALive 4223 / HDU 2962 spfa + 二分的主要内容,如果未能解决你的问题,请参考以下文章

UVALive - 4223(hdu 2926)

hdu 2962 题解

HDU1887 ZOJ2982 UVALive3958 Weird Numbers进制

POJ3994 HDU3354 UVALive4736 Probability One水题

two-sat hdu3062 UVALive 3211

hdu 4223 Dynamic Programming?