路径统计

其实坑挺多的 主要是重边的问题
还有自己的一个错误 就是 else 的问题 在代码中有注释

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define LL long long
#define RE register
#define pii pair<int,int>
using namespace std ;
inline int read() {
    int x = 0,f = 1;char k = getchar() ;
    while(k < '0'||k > '9') {if(k == '-')f = -1;k = getchar() ;}
    while(k >= '0' && k <= '9') {x = x * 10 + k - '0';k = getchar() ;}
    return x * f;
}
const int maxn = 2005;
int n,m,head[maxn],TOP,dis[maxn],num[maxn],ed[maxn][maxn];
struct Edge {int to,next,value;}e[maxn * maxn];
void add_edge(int u,int v,int w) {
    e[++TOP].to = v;
    e[TOP].next = head[u];
    e[TOP].value = w;
    head[u] = TOP ;
}
priority_queue< pii,vector<pii>,greater<pii> > q;
void dijie(int s) {
    for(RE int i = 1;i <= n ;++i) dis[i] = 1e9 ;
    dis[s] = 0;num[s] = 1; q.push(pii(dis[s],s)) ;
    while(!q.empty()) {
     int u = q.top().second;
     if(dis[u] != q.top().first) {q.pop();continue;}
     q.pop() ;
     for(RE int i = head[u]; i ;i = e[i].next) {
      if(dis[e[i].to] > dis[u] + e[i].value) {
       dis[e[i].to] = dis[u] + e[i].value ;
       num[e[i].to] = num[u] ;
       q.push(pii(dis[e[i].to],e[i].to)) ;
   }
   else if(dis[e[i].to] == dis[u] + e[i].value) num[e[i].to] += num[u] ; /*  else 不能忘  否则的话会导致第一个if已经赋过值 紧接着进行第二个if 则一定成立*/
     }
 }
}
int main() {
 scanf("%d%d",&n,&m) ;
 for(RE int i = 1;i <= m ;++i) {
     int u = read(),v = read(),w = read() ;
     if(!ed[u][v] || ed[u][v] > w) add_edge(u,v,w),ed[u][v] = w; //注意这个题有重边 为了去重 只能多记录一个数组 
 }
 dijie(1) ;
 if(dis[n] == 1e9) printf("No answer");
 else printf("%d %d",dis[n],num[n]) ;
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值