带权最短路计数,洛谷1608

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int inf=99999999;
int e[2001][2001],dis[10000000],book[10000000],ans[1000000];
int main(){
    int i,j,n,m,t1,t2,t3,u,v,mmin;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++){
        if(i==j)e[i][j]=0;
        else e[i][j]=inf;
    }
    if(m==0){
    printf("No answer");
    return 0;
    }
    for(i=1;i<=m;i++)
     {
        scanf("%d%d%d",&t1,&t2,&t3);
        e[t1][t2] = min(e[t1][t2], t3);
     }
    for(i=1;i<=n;i++)
    dis[i]=e[1][i];
    ans[1]=1;   dis[1]=0;
   for(i=1;i<=n-1;i++){
    mmin=inf;
    u=0; 
    for(j=1;j<=n;j++){
        if(book[j]==0&&dis[j]<mmin){
            mmin=dis[j];
            u=j;
        }}
        if(u==0)break;
        book[u]=1;
        for(j=1;j<=n;j++){
            if(book[j]==0&&dis[j]>dis[u]+e[u][j])
            {dis[j]=dis[u]+e[u][j];
               ans[j]=ans[u];
                   }
               else if(book[j]==0&&dis[j]==dis[u]+e[u][j]&&j!=1)
                    ans[j]+=ans[u];
        }
   }
   if(dis[n]==inf)printf("No answer");
   printf("%d ",dis[n]);
   printf("%d",ans[n]);
    return 0;
}

 

上一篇:为什么对1取反为-2?


下一篇:位运算