• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

yongchaoD

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

洛谷P1144 最短路计数(最短到达一个点的路径方案)

题意:起点到每个点的最短路有多少条。
思路:就是一个dij的板子题,至于路径,就用一个数组ans[]存就行,在dij中判断时,有更优的ans[y]=1,相同路径的ans[y]+=ans[u]。记得初始化ans[1]=1。

点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+9;
const int Mod=100003;

vector<pair<int,int>>G[N];
vector<int>dis(N,INT64_MAX),vis(N,0);
vector<int>ans(N,0);

int n,m;

void dij(){
     //cout<<"1"<<endl;
   priority_queue<pair<int,int>>q;
    q.push({0,1});
     dis[1]=0;
     ans[1]=1;

      while(!q.empty()){
          int u=q.top().second;
           q.pop();
           if(vis[u]) continue;
            vis[u]=1;

             for(auto[x,y]:G[u]){
                 if(dis[y]>dis[u]+x){
                     dis[y]=dis[u]+x;
                      q.push({-dis[y],y});
                      ans[y]=ans[u];
                 }
                 else if(dis[y]==dis[u]+1){//重边
                     ans[y]+=ans[u];
                     ans[y]%=Mod;
                 }
             }
      }
}

signed main()
{

    int u,v;
     scanf("%lld%lld",&n,&m);
     //cin>>n>>m;
    for(int i=0;i<m;i++){
      scanf("%lld%lld",&u,&v);
      //cin>>u>>v;
      G[u].push_back({1,v});
      G[v].push_back({1,u});
    }
      dij();

      for(int i=1;i<=n;i++){
          if(dis[i]==INT64_MAX){printf("0\n");}
           else printf("%lld\n",ans[i]);
      }
    return 0;
}

posted on 2024-07-10 11:22  yongchaoD  阅读(51)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3