Problem L

Problem Description
The Gorelians are a warlike race that travel the universe conquering new worlds as a form of recreation. Given their violent, fun-loving nature, keeping their leaders alive is of serious concern. Part of the Gorelian security plan involves changing the traffic patterns of their cities on a daily basis, and routing all Gorelian Government Officials to the Government Building by the fastest possible route.<br><br>Fortunately for the Gorelian Minister of Traffic (that would be you), all Gorelian cities are laid out as a rectangular grid of blocks, where each block is a square measuring 2520 rels per side (a rel is the Gorelian Official Unit of Distance). The speed limit between two adjacent intersections is always constant, and may range from 1 to 9 rels per blip (a blip, of course, being the Gorelian Official Unit of Time). Since Gorelians have outlawed decimal numbers as unholy (hey, if you're the dominant force in the known universe, you can outlaw whatever you want), speed limits are always integer values. This explains why Gorelian blocks are precisely 2520 rels in length: 2520 is the least common multiple of the integers 1 through 9. Thus, the time required to travel between two adjacent intersections is always an integer number of blips.<br><br>In all Gorelian cities, Government Housing is always at the northwest corner of the city, while the Government Building is always at the southeast corner. Streets between intersections might be one-way or two-way, or possibly even closed for repair (all this tinkering with traffic patterns causes a lot of accidents). Your job, given the details of speed limits, street directions, and street closures for a Gorelian city, is to determine the fastest route from Government Housing to the Government Building. (It is possible, due to street directions and closures, that no route exists, in which case a Gorelian Official Temporary Holiday is declared, and the Gorelian Officials take the day off.)<br><center><img src=../../data/images/C170-1005-1.png></center><br>The picture above shows a Gorelian City marked with speed limits, one way streets, and one closed street. It is assumed that streets are always traveled at the exact posted speed limit, and that turning a corner takes zero time. Under these conditions, you should be able to determine that the fastest route from Government Housing to the Government Building in this city is 1715 blips. And if the next day, the only change is that the closed road is opened to two way traffic at 9 rels per blip, the fastest route becomes 1295 blips. On the other hand, suppose the three one-way streets are switched from southbound to northbound (with the closed road remaining closed). In that case, no route would be possible and the day would be declared a holiday.
 

Input
The input consists of a set of cities for which you must find a fastest route if one exists. The first line of an input case contains two integers, which are the vertical and horizontal number of city blocks, respectively. The smallest city is a single block, or 1 by 1, and the largest city is 20 by 20 blocks. The remainder of the input specifies speed limits and traffic directions for streets between intersections, one row of street segments at a time. The first line of the input (after the dimensions line) contains the data for the northernmost east-west street segments. The next line contains the data for the northernmost row of north-south street segments. Then the next row of east-west streets, then north-south streets, and so on, until the southernmost row of east-west streets. Speed limits and directions of travel are specified in order from west to east, and each consists of an integer from 0 to 9 indicating speed limit, and a symbol indicating which direction traffic may flow. A zero speed limit means the road is closed. All digits and symbols are delimited by a single space. For east-west streets, the symbol will be an asterisk '*' which indicates travel is allowed in both directions, a less-than symbol '<' which indicates travel is allowed only in an east-to-west direction, or a greater-than symbol '>' which indicates travel is allowed only in a west-to-east direction. For north-south streets, an asterisk again indicates travel is allowed in either direction, a lowercase "vee" character 'v' indicates travel is allowed only in a north-to-south directions, and a caret symbol '^' indicates travel is allowed only in a south-to-north direction. A zero speed, indicating a closed road, is always followed by an asterisk. Input cities continue in this manner until a value of zero is specified for both the vertical and horizontal dimensions.
 

Output
For each input scenario, output a line specifying the integer number of blips of the shortest route, a space, and then the word "blips". For scenarios which have no route, output a line with the word "Holiday".
 

Sample Input
  
  
2 2 9 * 9 * 6 v 0 * 8 v 3 * 7 * 3 * 6 v 3 * 4 * 8 * 2 2 9 * 9 * 6 v 9 * 8 v 3 * 7 * 3 * 6 v 3 * 4 * 8 * 2 2 9 * 9 * 6 ^ 0 * 8 ^ 3 * 7 * 3 * 6 ^ 3 * 4 * 8 * 0 0
 

Sample Output
1715 blips
1295 blips
Holiday

简单题意:
  这长篇的英文题意读的头疼,不过思想是很简单的。也就是说,存在一个N*M的矩形,代表街区的集合,然后从左上角作为起点,向右下角驶去,分成的每个小矩形就是街区,并且有速度限制,如果街道,即小矩形的边为0的时候不能行驶,边长为2520.现在需要编写一个程序,符合这些限制并且是最短路径。
解题思路形成过程:
  这个题目的思想是好的,求出最短路径,但是题意太过于冗长。什么各种限制条件以及输入格式代表什么,是解题的关键。理解了题目,才能编写程序,让代码AC。求最短路径的那么多方法,应该都可以得到最终的结果,下面采用Dijkstra算法求出最短路径。
感想:
  原来审题,理解题意也是ACMER的必不可少的一项基本素质。耐心的审完题目,就会发现,其实所有的东西还是那些简单的思想以及模板化的代码。
AC代码;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<utility>
using namespace std;

typedef pair<int,int>pii;
const int INF = 0x7fffffff;
const int VN = 445;
const int EN = VN*VN/2;

struct Edge{
    int v, next, w;
}E[EN];

int n;
int m;
int vn;
int size;
int head[VN];
int d[VN];

void addEdge(int u,int v,int w){
    E[size].v=v;
    E[size].w=w;
    E[size].next=head[u];
    head[u]=size++;
}
void init(){
    vn=(m+1)*(n+1);
    size=0;
    memset(head, -1, sizeof(head));
}

void Dijkstra(int src){
    for(int i=1; i<=vn; ++i)d[i]=INF;
    d[src]=0;
    priority_queue<pii,vector<pii>,greater<pii> >q;
    q.push(make_pair(d[src],src));
    while(!q.empty()){
        pii x = q.top(); q.pop();
        int u=x.second;
        if(d[u]!=x.first)continue;
        for(int e=head[u]; e!=-1; e=E[e].next){
            int tmp=d[u]+E[e].w;
            if(d[E[e].v] > tmp){
                d[E[e].v] = tmp;
                q.push(make_pair(tmp,E[e].v));
            }
        }
    }
}


int main(){
    char str[100];
    int u,v,w;
    while(~scanf("%d%d%*c",&n,&m)&&n+m){
        // input
        init();
        for(int i=1; i<=n*2+1; ++i){
            gets(str);
            int len=strlen(str);
            if(i&1){
                for(int j=0,k=1; j<len; j+=4,++k){
                    u=(m+1)*(i/2)+k;
                    w = str[j]-'0';
                    if(w==0)continue;
                    if(str[j+2]=='*'){
                        addEdge(u,u+1,2520/w);
                        addEdge(u+1,u,2520/w);
                    }
                    else if(str[j+2]=='<'){
                        addEdge(u+1,u,2520/w);
                    }
                    else{
                        addEdge(u,u+1,2520/w);
                    }
                }
            }
            else{
                for(int j=0,k=1; j<len; j+=4,++k){
                    u = (m+1)*(i/2-1)+k;
                    w = str[j]-'0';
                    if(w==0) continue;
                    if(str[j+2]=='*'){
                        addEdge(u, u+m+1, 2520/w);
                        addEdge(u+m+1, u, 2520/w);
                    }
                    else if(str[j+2]=='v'){
                        addEdge(u, u+m+1, 2520/w);
                    }
                    else if(str[j+2]=='^'){
                        addEdge(u+m+1, u, 2520/w);
                    }
                }
            }
        }
        Dijkstra(1);
        if(d[vn]!=INF) printf("%d blips\n", d[vn]);
        else puts("Holiday");
    }
    return 0;
}
### 对偶问题在优化中的概念 对偶问题是数学优化理论中的一个重要概念,在许多实际应用中具有重要意义。其核心思想在于,任何优化问题都可以通过转换视角来表示为两个相互关联的问题:原始问题(primal problem)和对偶问题(dual problem)。这种关系不仅提供了新的求解途径,还能够帮助分析原问题的性质。 #### 原始问题与对偶问题的关系 在一个标准形式的凸优化问题中,目标是最小化某个函数 \( f(x) \),其中 \( x \in \mathbb{R}^n \) 是决策变量,并受到一组约束条件的影响。通过对该问题引入拉格朗日乘子并构建拉格朗日函数,可以得到对应的对偶问题。具体而言: \[ L(x, \lambda) = f(x) + \sum_{i=1}^{m} \lambda_i g_i(x), \] 这里 \( L(x, \lambda) \) 表示拉格朗日函数,\( \lambda_i \geq 0 \) 是与不等式约束 \( g_i(x) \leq 0 \) 相关的拉格朗日乘子[^1]。 随后定义对偶函数为: \[ g(\lambda) = \inf_x L(x, \lambda). \] 最终形成的对偶问题即为最大化此对偶函数: \[ \max_\lambda g(\lambda), \quad \text{s.t. } \lambda \geq 0. \] 这种方法的核心优势之一是对偶问题通常更易于处理,尤其是在涉及复杂约束的情况下。 #### 平滑化的改进策略 为了进一步提升算法性能,可以通过向拉格朗日项添加一个关于 \( \lambda \) 的二次正则化项实现平滑效果。这一技术被称为“增加不等式约束使 \( \lambda \) 变化更加平稳”,从而改善数值稳定性以及收敛速度[^2]。 以下是基于上述思路的一个简单伪代码框架展示如何调整参数以促进光滑过渡: ```python def optimize_with_smoothing(f, constraints, initial_lambda): lambda_ = initial_lambda while not converged: # 更新拉格朗日乘数时加入二次惩罚项 gradient_update = compute_gradient(lambda_) quadratic_penalty = alpha * (lambda_ ** 2) new_lambda = project_to_nonnegative(lambda_ - learning_rate * (gradient_update + quadratic_penalty)) return lambda_ ``` 在此过程中,`alpha` 控制着正则化强度,而 `project_to_nonnegative` 函数用于确保更新后的 \( \lambda \) 非负性满足要求。 #### 关于带宽线性优化的研究进展 近年来,针对在线学习环境下的带宽线性优化领域也取得了显著成果。例如,《Competing in the Dark: An Efficient Algorithm for Bandit Linear Optimization》一文中提出了高效的随机梯度下降变体,能够在未知损失函数分布的前提下达到接近最优的表现水平[^3]。 这些研究方向表明即使面对高度不确定性的场景下,合理利用对偶结构仍然可以帮助我们设计出强大且实用的解决方案。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值