栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


#include 
#include 
#include 
#include 
#include 
using namespace std;
#define x first
#define y second
typedef pair PII;
const int N = 1010,M = 10010;
vectorg[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,greater >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<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号