Optimal Symmetric Paths UVA - 12295(spfa最短路+最短路个数)

本文介绍了一种寻找矩阵中对称最短路径的方法,并通过SPFA算法实现了路径搜索及计数,确保路径对称且拥有最小权值之和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You have a grid of n rows and n columns. Each of the unit squares contains a non-zero digit. You
walk from the top-left square to the bottom-right square. Each step, you can move left, right, up or
down to the adjacent square (you cannot move diagonally), but you cannot visit a square more than
once. There is another interesting rule: your path must be symmetric about the line connecting the
bottom-left square and top-right square. Below is a symmetric path in a 6 × 6 grid.
Your task is to find out, among all valid paths, how many of them have the minimal sum of digits?
Input
There will be at most 25 test cases. Each test case begins with an integer n (2 ≤ n ≤ 100). Each of
the next n lines contains n non-zero digits (i.e. one of 1, 2, 3, . . . , 9). These n
2
integers are the digits
in the grid. The input is terminated by a test case with n = 0, you should not process it.
Output
For each test case, print the number of optimal symmetric paths, modulo 1,000,000,009.
题意大概是从矩形的左上角走到右下角,求权值最短的路径的个数,路径必须是关于副对角线对称。

既然要关于副对角线对称,那么其实只要我走到了副对角线上,那么这条路径就已经确定了,也就是再走到副对角线之前,走一步相当于走了两步,因为还相当于走了一步关于副对角线对称的一步,所以我们可以将对称的点权值相加,这样就变成了从起点到副对角线的最短距离,因为有5000个点,所以用spfa比较保险,然后再记录一下最短路的条路就好了,具体见代码。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define ll long long
#define maxn 120
#define angel 0x3f3f3f3f
const ll mod=1000000009;
int n;
int dirx[5]={0,1,-1,0,0};
int diry[5]={0,0,0,1,-1};
ll value[maxn][maxn];
ll num[maxn][maxn];//最短路径个数
int id[maxn][maxn];
ll dist[maxn][maxn];//最短路径长度
int check[maxn][maxn];
ll Min(ll x,ll y)
{
    if(x>y)
        return y;
    return x;
}
struct co
{
    int x,y;
};
void spfa()
{
    queue<co>q;
    check[1][1]=1;
    num[1][1]=1;//起点最短路个数为1
    co st;
    st.x=1;
    st.y=1;
    q.push(st);
    while(!q.empty())
    {
        co now=q.front();
        //cout<<now.x<<" "<<now.y<<endl;
        for(int i=1; i<=4; i++)
        {
            co next;
            next.x=dirx[i]+now.x;
            next.y=diry[i]+now.y;
            if(next.x<1||next.y<1||next.x>n||next.y>n+1-next.x)
                continue;
            if(dist[next.x][next.y]>dist[now.x][now.y]+value[next.x][next.y])
            {
                dist[next.x][next.y]=dist[now.x][now.y]+value[next.x][next.y];
                num[next.x][next.y]=num[now.x][now.y];//更新最短路径个数
                if(!check[next.x][next.y])
                {
                    q.push(next);
                    check[next.x][next.y]=1;
                }
            }
            else if(dist[next.x][next.y]==dist[now.x][now.y]+value[next.x][next.y])
            {
                num[next.x][next.y]+=num[now.x][now.y];//更新最短路径个数 画个图就很好理解
                if(!check[next.x][next.y])
                {
                    q.push(next);
                    check[next.x][next.y]=1;
                }
            }
        }
        q.pop();
        check[now.x][now.y]=0;
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n+i-1; j++)
            {
                dist[i][j]=angel;
                check[i][j]=0;
                num[i][j]=0;
            }
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                scanf("%d",&value[i][j]);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n+1-i; j++)
            {
                if(j==n+1-i)
                    continue;
                value[i][j]+=(value[n+1-j][n+1-i]);//对称权值相加
            }
        }
        dist[1][1]=value[1][1];
        spfa();
        ll minn=angel;
        for(int i=1; i<=n; i++)
            minn=Min(minn,dist[i][n+1-i]);
        ll ans=0;
        for(int i=1; i<=n; i++)
        {
            if(dist[i][n+1-i]==minn)
                ans=(ans+num[i][n+1-i])%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
<think>嗯,用户想了解PostgreSQL中try_vector_engine_strategy参数里的off、force和optimal三个选项的具体含义。首先,我需要确认这个参数是否真实存在,因为根据我的知识库,PostgreSQL并没有这个参数。可能用户混淆了其他数据库系统或扩展的功能,比如ClickHouse的向量化执行引擎,或者某些定制化的PostgreSQL分支。 接下来,我应该检查用户提供的引用内容,看看是否有相关线索。引用内容提到了Hive的安装配置和MySQL驱动,这些和PostgreSQL的参数无关,可能用户是在配置其他系统时遇到的问题。但需要明确告知用户这一点,避免误导。 然后,考虑用户可能的实际需求。用户可能在使用某个支持向量化执行的PostgreSQL扩展,或者在使用基于PostgreSQL的数据库产品,如Greenplum或TimescaleDB,这些可能有类似的参数。需要建议用户查阅相关文档或确认参数的正确性。 后,应该以清晰的结构解释off、force、optimal可能的含义,基于常见的数据库参数设计,比如off禁用功能,force强制使用,optimal由优化器决定。同时提醒用户PostgreSQL原生不支持该参数,并建议核实信息来源或检查扩展模块。</think>关于 PostgreSQL 中 `try_vector_engine_strategy` 参数的解释如下: ### 参数说明 根据 PostgreSQL 官方文档和已知配置参数体系,**`try_vector_engine_strategy` 并非 PostgreSQL 原生支持的参数**。此参数名称更可能出现在以下场景中: 1. 基于 PostgreSQL 的定制化分支或扩展(如面向向量化计算的插件) 2. 用户对参数名称的误写或混淆(例如与 `enable_` 开头的优化器参数混淆) 3. 特定云数据库产品的自定义参数 ### 相似参数分析 若假设该参数与**向量化执行引擎**相关,其选项可能对应以下行为(以典型向量化引擎设计为参考): | 参数值 | 可能含义 | |-----------|--------------------------------------------------------------------------| | `off` | 完全禁用向量化执行策略,强制使用传统行存执行模式 | | `force` | 强制启用向量化执行,即使优化器认为行存模式更优 | | `optimal` | 由优化器根据代价估算自动选择向量化或行存执行模式(默认值) | ### 使用建议 1. 通过以下命令验证参数是否存在: ```sql SHOW try_vector_engine_strategy; ``` 2. 检查是否安装了向量化执行扩展(如 `pg_vector`、`citus` 等) 3. 若使用云数据库(如 AWS Aurora、Azure PostgreSQL),需查阅对应服务商的文档 ### 典型场景示例 ```sql -- 查看当前执行计划是否使用向量化 EXPLAIN ANALYZE SELECT * FROM large_table WHERE vector_column @> ARRAY[1,2,3]; -- 修改参数测试性能差异(假设参数存在) SET try_vector_engine_strategy = 'force'; ``` ### 参考资料建议 1. PostgreSQL 官方参数列表:https://www.postgresql.org/docs/current/runtime-config.html 2. 向量化执行扩展文档(如 Citus 或 pg_vendor 的说明) 3. 云服务商提供的数据库参数说明
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值