LA 4223 最短路 路径选择要求提高一点

F - Trucking

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

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题意:lLA给你n个节点(<=1000),节点之间最多一条路,路有长度l和高度限制h,现在一辆卡车准备从起点s到终点t,要求卡车的高度不得高于H,问卡车能达到终点的最高高度H和此最高高度下的最短路径;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
struct edge{
   int to,h,l;
};
vector<edge> G[1005];
int dis[1005],inq[1005];

bool ok(int s,int t,int h)
{
    queue<int> q;
    MM(dis,inf);
    MM(inq,0);

    q.push(s);
    inq[s]=1;
    dis[s]=0;

    while(q.size())
    {
        int u=q.front();q.pop();inq[u]=0;
        for(int i=0;i<G[u].size();i++)
        {
            edge e=G[u][i];
            if(e.h<h) continue;
            if(dis[e.to]>dis[u]+e.l)
               {
                   dis[e.to]=dis[u]+e.l;
                   if(!inq[e.to]) {q.push(e.to);inq[e.to]=1;}
            }
        }
    }
    return dis[t]!=inf;
}

int main()
{
    int n,m,kk=0;
    scanf("%d %d",&n,&m);
    while(1)
    {
        if(!(n||m)) break;
        for(int i=1;i<=n;i++) G[i].clear();
        for(int i=1;i<=m;i++)
        {
            int u,v,h,l;
            scanf("%d%d%d%d",&u,&v,&h,&l);
            if(h==-1) h=inf;
            G[u].push_back((edge){v,h,l});
            G[v].push_back((edge){u,h,l});
        }
        int s,t,H;scanf("%d%d%d",&s,&t,&H);
        int l=0,r=H+1,ansl=inf;
        while(r-l>1)
        {
            int mid=(l+r)/2;
            if(ok(s,t,mid)) {l=mid;ansl=dis[t];}
            else r=mid;
        }
        printf("Case %d:\n",++kk);
        if(ansl==inf) printf("cannot reach destination\n");
        else {
                printf("maximum height = %d\n",l);
                printf("length of shortest route = %d\n",ansl);
             }
        scanf("%d%d",&n,&m);if(n||m) printf("\n");
                            else break;
    }
    return 0;
}

  分析:代码有毒啊,,,SPFA最坏情况V*E,按理说应该会被卡的,,,,,,还是dijkstra+堆优化比较稳;

分析:很简单的图论题,最大化某个值二分就当然想到了,关键是找路,其实就是在最短路的代码里面,加上一个对

路径边的选择,就是如果当前路径的高度限制<二分枚举的值得话,那么这条路当然不能选择(其实也就是相当于在

原来的图中把限制高度<二分枚举的h边全部擦掉),,简单的一笔,比以前做的图论题简单不知道哪里去了,,比赛

被吓傻了

 
时间: 2024-08-05 10:23:23

LA 4223 最短路 路径选择要求提高一点的相关文章

逻辑运算符短路与,短路或

1.逻辑运算符说明 a:逻辑运算符一般用于连接boolean类型的表达式或者值. b:表达式:就是用运算符把常量或者变量连接起来的符合java语法的式子. 2.&&和&(遇false则false)的区别? a:最终结果一样. b:&&具有短路效果(可以提高一点效率).左边是false,右边不执行. &是无论左边是false还是true,右边都会执行 3.同理||和|(遇true则true)的区别?(学生自学) a:最终结果一样. b:||具有短路效果(可以提

辛巴学院-Unity-剑英的c#提高篇(一)主循环

这是测试版 辛巴学院:正大光明的不务正业. 最近刚刚离开了我服务了三年多的公司,因为一个无数次碰到的老问题,没钱了. 之前不知道做什么好的时候,机缘巧合之下和哒嗒网络的吴总聊了一下,发现了vr game这扇窗户,这里权当帮哒嗒网络打个广告吧.^_^ 回头看看仓惶的这一段时间,荒废了很多,抽空回来再和大家聊聊c#. 之前做了个入门系列,胡乱说了些东西.感觉入门这样子也就差不多了,该稍微提高一点了. ? 从写一段程序,到写一个app,写一个游戏,到底其中有什么不同呢?一段程序的执行时间很短,一个应用

14个优化网站性能提高网站访问速度技巧

相信互联网已经越来越成为人们生活中不可或缺的一部分.ajax,flex等等富客户端的应用使得人们越加“幸福”地体验着许多原先只能在C/S实 现的功能.比如Google机会已经把最基本的office应用都搬到了互联网上.当然便利的同时毫无疑问的也使页面的速度越来越慢.自己是做前端开发的,在性能方面,根据yahoo的调查,后台只占5%,而前端高达95%之多,其中有88%的东西是可以优化的. 以上是一张web2.0页面的生命周期图.工程师很形象地讲它分成了“怀孕,出生,毕业,结婚”四个阶段.如果在我们

雅虎十四条 - 14个优化网站性能提高网站访问速度的技巧

14个优化网站性能提高网站访问速度的技巧 又叫“雅虎十四条”,想起一年前那个懵懂的我,大四傻乎乎的跑到大学城面试前端,那个时候以为寒暑假看了两套CSS的视频,就很牛B了,出发先还把视频温了一下,嗯嗯,这是滑动门,嗯嗯这是绝对定位,嗯嗯这是浮动清除…… 当时是彪叔面试我的,当时我还不知道那个人,全身黑漆漆的,黑色T-shirt,黑色皮肤,黑色帽子,黑色墨镜,还有点黑色胡渣的人,就是彪叔,补做了试题后支支吾吾的跟他谈了一下,发现完全不行,第一个问题是“雅虎十四条”是什么?然后我蒙了,pardon?

网站优化提高访问速度的14个方法

1. 尽可能的减少 HTTP 的请求数 [content] 2. 使用 CDN(Content Delivery Network) [server] 3. 添加 Expires 头(或者 Cache-control ) [server] 4. Gzip 组件 [server] 5. 将 CSS 样式放在页面的上方 [css] 6. 将脚本移动到底部(包括内联的) [javascript] 7. 避免使用 CSS 中的 Expressions [css] 8. 将 JavaScript 和 CSS

95%的中国网站需要重写CSS

95%的中国网站需要重写CSS 很长一段时间,我都使用12px作为网站的主要字体大小.10px太小,眼睛很容易疲劳,14px虽容易看清,却破坏页面的美感.唯独12px在审美和视力方面都恰到好处. 谁对我的网站字体大小有意见?我老爸,他是第一个向我反映看不清我的网站文字的人.这使我意识到12px,其实只是让我觉得很不错而已,而对于那些视力下降明显的中年以上的人来讲,几乎等于10px对于我的感觉.于是我告诉他,在“查看”里调整“文字大小”就可以了.但是却发现这是徒劳的.在Firefox能轻易调整的字

Gradient Boost Decision Tree(&amp;Treelink)

http://www.cnblogs.com/joneswood/archive/2012/03/04/2379615.html 1.      什么是Treelink Treelink是阿里集团内部的叫法,其学术上的名称是GBDT(Gradient Boosting Decision Tree,梯度提升决策树).GBDT是“模型组合+决策树”相关算法的两个基本形式中的一个,另外一个是随机森林(Random Forest),相较于GBDT要简单一些. 1.1    决策树 应用最广的分类算法之一

Beetl2.2使用说明书20151201

李家智<[email protected]> Table of Contents 1. 什么是Beetl 2. 基本用法 2.1. 从GroupTemplate开始 2.2. 模板基础配置 2.3. 模板资源加载器 2.4. 定界符与占位符号 2.5. 注释 2.6. 临时变量定义 2.7. 全局变量定义 2.8. 共享变量 2.9. 模板变量 2.10. 引用属性 2.11. 算数表达式 2.12. 逻辑表达式 2.13. 循环语句 2.14. 条件语句 2.15. try-catch 2.

web前端开发规范

本文原创,这里首先声明,转载注明本文出处,翻版必究! web前端开发规范的现实意义 1.提高团队的协作能力 2.提高代码的重复利用率 3.可以写出质量更高,效率更好的代码 4.为后期维护提供更好的支持 5.可读性高 一.命名规则 1.html命名规则: a.文件名称命名规则:统一使用小写英文字母.数字.下划线的组合,不得包含汉字空格和特殊字符 2.命名原则:方便理解.方便查找 b.索引文件命名原则:index.html.index.htm.index.asp.index.aspx.index.j