POJ 3463 Sightseeing (次短路经数)

本文详细解析了一个Dijkstra算法的变种题——求解从起点到终点的最短路径及次短路径的数量问题。该题目的特殊之处在于允许路径长度为最短路径长度或比最短路径长一个单位,提供了详细的AC代码实现。

                    Sightseeing

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions:10005 Accepted: 3523

Description

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.

Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.

There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.

For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.

Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with two integers N and M, separated by a single space, with 2 ≤ N ≤ 1,000 and 1 ≤ M ≤ 10, 000: the number of cities and the number of roads in the road map.

  • M lines, each with three integers AB and L, separated by single spaces, with 1 ≤ AB ≤ NA ≠ B and 1 ≤ L ≤ 1,000, describing a road from city A to city B with length L.

    The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.

  • One line with two integers S and F, separated by a single space, with 1 ≤ SF ≤ N and S ≠ F: the starting city and the final city of the route.

    There will be at least one route from S to F.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 109 = 1,000,000,000.

Sample Input

2
5 8
1 2 3
1 3 2
1 4 5
2 3 1
2 5 3
3 4 2
3 5 4
4 5 3
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2
5 2 7
5 2 7
4 1

Sample Output

3
2

Hint

The first test case above corresponds to the picture in the problem description.

 

思路


一开始我想了一下,用A*好像可以做,但是超了内存,看了一下discuss,原因应该是不断入队造成的,这让我感到十分无奈,毕竟刚刚才用a*过了一题。

具体的东西我写在注释里了,注意次短路的入队操作!

#include<iostream>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const ll inf = 210000000000000;
int n,m,s,t;
vector<int>u[1024];
vector<ll>w[1024];
ll dis[1024][2];
//虽然按照我的推理,这个题不会爆int ,可是不用int,就是会wa;
bool book[1024][2];
ll ans[1024][2];
struct node
{
    int num;
    ll dis;
    int flag;
    bool operator<(const node x)const
    {
        return x.dis<dis;
    }
};

ll Dijkstra()
{
    priority_queue<node>q;
    node exa;
    dis[s][0]=0;ans[s][0]=1;
    q.push(node{s,0,0});
    while(!q.empty()){
        exa=q.top();q.pop();
        int st = exa.num;//当前起点
        ll diss=exa.dis;//diss表示,当前节点的最短(或次短)
        int f=exa.flag;//表示这是最短还是次短
        if(book[st][f]){continue;}
        book[st][f]=true;
        int siz=u[st].size();
        for(int i=0;i<siz;i++){
            int y=u[st][i];
            ll ww=w[st][i];
            /*y是下一个节点,ww是路径*/
            if(dis[y][0]>diss+ww){//最短的距离可以更新
                q.push(node{y,dis[y][0],1});
                dis[y][1]=dis[y][0];//下一节点的次短路,就是更新前的最短路
                ans[y][1]=ans[y][0];//下一节点的次短路条数,就是更新前的最短路条数
                dis[y][0]=diss+ww;//更新最短路
                ans[y][0]=ans[st][0];//下一最短路的条数,就是当前节点最短/次短的条数
                                        //这个位置不可能是次短路更新
                q.push(node{y,diss+ww,0});
            }
            else if(dis[y][0]==diss+ww){//下一节点的最短距离,就是当前距离
                ans[y][0]+=ans[st][0];
            }
            else if(dis[y][1]>diss+ww){//次短路可更新
                dis[y][1]=diss+ww;//更新次短路
                ans[y][1]=ans[st][f];//下一节点的次短路条数,就是更新来源的路径数
                q.push(node{y,diss+ww,1});
            }
            else if(dis[y][1]==diss+ww){
                ans[y][1]+=ans[st][f];//下一节点的路径数,加上当前节点的路径数
            }
        }
    }
    if(dis[t][1]==dis[t][0]+1){
        return ans[t][0]+ans[t][1];
    }
    return ans[t][0];
}

void init()
{
    for(int i=0;i<=n+1;i++){
        for(int j=0;j<=1;j++){
            dis[i][j]=inf;
        }
    }
    memset(book,0,sizeof(book));
    memset(ans,0,sizeof(ans));
    for(int i=1;i<=n;i++){
        u[i].clear();
        w[i].clear();
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        init();
        int x,y;ll z;
        for(int i=1;i<=m;i++){
            scanf("%d%d%lld",&x,&y,&z);
            u[x].push_back(y);
            w[x].push_back(z);
        }
        scanf("%d%d",&s,&t);
        printf("%lld\n",Dijkstra());
    }
}

  妈呀,wa死我了,a个题真不容易

转载于:https://www.cnblogs.com/ZGQblogs/p/9380397.html

### 光流法C++源代码解析与应用 #### 光流法原理 光流法是一种在计算机视觉领域中用于追踪视频序列中运动物体的方法。它基于亮度不变性假设,即场景中的点在时间上保持相同的灰度值,从而通过分析连续帧之间的像素变化来估计运动方向和速度。在学上,光流场可以表示为像素位置和时间的一阶导,即Ex、Ey(空间梯度)和Et(时间梯度),它们共同构成光流方程的基础。 #### C++实现细节 在给定的C++源代码片段中,`calculate`函负责计算光流场。该函接收一个图像缓冲区`buf`作为输入,并初始化了几个关键变量:`Ex`、`Ey`和`Et`分别代表沿x轴、y轴和时间轴的像素强度变化;`gray1`和`gray2`用于存储当前帧和前一帧的平均灰度值;`u`则表示计算出的光流矢量大小。 #### 图像处理流程 1. **初始化和预处理**:`memset`函被用来清零`opticalflow`组,它将保存计算出的光流据。同时,`output`组被填充为白色,这通常用于可视化结果。 2. **灰度计算**:对每一像素点进行处理,计算其灰度值。这里采用的是RGB通道平均值的计算方法,将每个像素的R、G、B值相加后除以3,得到一个近似灰度值。此步骤确保了计算过程的鲁棒性和效率。 3. **光流向量计算**:通过比较当前帧和前一帧的灰度值,计算出每个像素点的Ex、Ey和Et值。这里值得注意的是,光流向量的大小`u`是通过`Et`除以`sqrt(Ex^2 + Ey^2)`得到的,再乘以10进行量化处理,以减少计算复杂度。 4. **结果存储与阈值处理**:计算出的光流值被存储在`opticalflow`组中。如果`u`的绝对值超过10,则认为该点存在显著运动,因此在`output`组中将对应位置标记为黑色,形成运动区域的可视化效果。 5. **状态更新**:通过`memcpy`函将当前帧复制到`prevframe`中,为下一次迭代做准备。 #### 扩展应用:Lukas-Kanade算法 除了上述基础的光流计算外,代码还提到了Lukas-Kanade算法的应用。这是一种更高级的光流计算方法,能够提供更精确的运动估计。在`ImgOpticalFlow`函中,通过调用`cvCalcOpticalFlowLK`函实现了这一算法,该函接受前一帧和当前帧的灰度图,以及窗口大小等参,返回像素级别的光流场信息。 在实际应用中,光流法常用于目标跟踪、运动检测、视频压缩等领域。通过深入理解和优化光流算法,可以进一步提升视频分析的准确性和实时性能。 光流法及其C++实现是计算机视觉领域的一个重要组成部分,通过对连续帧间像素变化的精细分析,能够有效捕捉和理解动态场景中的运动信息
微信小程序作为腾讯推出的一种轻型应用形式,因其便捷性与高效性,已广泛应用于日常生活中。以下为该平台的主要特性及配套资源说明: 特性方面: 操作便捷,即开即用:用户通过微信内搜索或扫描二维码即可直接使用,无需额外下载安装,减少了对手机存储空间的占用,也简化了使用流程。 多端兼容,统一开发:该平台支持在多种操作系统与设备上运行,开发者无需针对不同平台进行重复适配,可在一个统一的环境中完成开发工作。 功能丰富,接口完善:平台提供了多样化的API接口,便于开发者实现如支付功能、用户身份验证及消息通知等多样化需求。 社交整合,传播高效:小程序深度嵌入微信生态,能有效利用社交关系链,促进用户之间的互动与传播。 开发成本低,周期短:相比传统应用程序,小程序的开发投入更少,开发周期更短,有助于企业快速实现产品上线。 资源内容: “微信小程序-项目源码-原生开发框架-含效果截图示例”这一资料包,提供了完整的项目源码,并基于原生开发方式构建,确保了代码的稳定性与可维护性。内容涵盖项目结构、页面设计、功能模块等关键部分,配有详细说明与注释,便于使用者迅速理解并掌握开发方法。此外,还附有多个实际运行效果的截图,帮助用户直观了解功能实现情况,评估其在实际应用中的表现与价值。该资源适用于前端开发人员、技术爱好者及希望拓展业务的机构,具有较高的参考与使用价值。欢迎查阅,助力小程序开发实践。资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值