观光---最短路条数统计+状态转移+次小路径

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
const int N = 1010,M = 10010;
vector<PII>g[N];
int n,m,s,t;
int dist[N][2];
int cnt[N][2];
bool st[N][2];
struct node
{
    int ver,type,w;
    bool operator > (const node &W)const
    {
        return w>W.w;
    }
};
int dij()
{
    memset(dist,0x3f,sizeof dist);
    memset(cnt,0,sizeof cnt);
    memset(st,0,sizeof st);
    dist[s][0]=0;cnt[s][0]=1;
    priority_queue<node,vector<node>,greater<node> >heap;
    heap.push({s,0,0});
    while(heap.size())
    {
        auto t=heap.top();
        heap.pop();
        int u=t.ver,type=t.type,distance=t.w;
        if(st[u][type])continue;
        st[u][type]=true;
        for(auto [v,w] : g[u])
        {
            if(dist[v][0]>distance+w)
            {
                dist[v][1]=dist[v][0],cnt[v][1]=cnt[v][0];
                heap.push({v,1,dist[v][1]});
                dist[v][0]=distance+w,cnt[v][0]=cnt[u][type];
                heap.push({v,0,dist[v][0]});
            }
            else if(dist[v][0]==distance+w)
            {
                cnt[v][0]+=cnt[u][type];
            }
            else if(dist[v][1]>distance+w)
            {
                dist[v][1]=distance+w,cnt[v][1]=cnt[u][type];
                heap.push({v,1,dist[v][1]});
            }
            else if(dist[v][1]==distance+w) 
            {
                cnt[v][1]+=cnt[u][type];
            }
        }
    }
    int res=cnt[t][0];
    if(dist[t][0]==dist[t][1]-1)res+=cnt[t][1];
    return res;
}
signed main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        int a,b,c;
        for(int i=1;i<=n;i++)g[i].clear();
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b>>c;
            g[a].push_back({b,c});
        }
        cin>>s>>t;
        cout<<dij()<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_WAWA鱼_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值