标签:邻接 jks std stream ret iostream 初始化 dijkstra ace
#include<stdio.h> #include<iostream> using namespace std; const int maxn=1010; const int INF=1000000000; int n,m,c1,c2; int G[maxn][maxn],d[maxn]; int weight[maxn],w[maxn]; int num[maxn]; int vis[maxn]; void dijkstra(int s){ fill(d,d+maxn,INF); fill(w,w+maxn,0); fill(num,num+maxn,0); d[s]=0; w[s]=weight[s]; num[s]=1; for(int i=0;i<n;i++){ int u=-1,min=INF; for(int j=0;j<n;j++){ if(vis[j]==0&&d[j]<min){ u=j; min=d[j]; } } if(u==-1)return; vis[u]=1; for(int v=0;v<n;v++){ if(vis[v]==0&&G[u][v]!=INF){ if(d[u]+G[u][v]<d[v]){ d[v]=d[u]+G[u][v]; w[v]=w[u]+weight[v]; num[v]=num[u]; } else if(d[u]+G[u][v]==d[v]){ if(w[v]<w[u]+weight[v]){ w[v]=w[u]+weight[v]; } num[v]+=num[u]; } } } } } int main() { scanf("%d %d %d %d",&n,&m,&c1,&c2); for(int i=0;i<n;i++){ scanf("%d",&weight[i]); } int a,b,c; fill(G[0],G[0]+maxn*maxn,INF); //这里的初始化图的方式需要注意 for(int i=0;i<m;i++){ scanf("%d %d %d",&a,&b,&c); G[a][b]=c; G[b][a]=G[a][b]; //2、5测试点一直错的原因 邻接矩阵的存储是关于斜对角线对称的 } dijkstra(c1); printf("%d %d",num[c2],w[c2]); }
标签:邻接 jks std stream ret iostream 初始化 dijkstra ace
原文地址:https://www.cnblogs.com/1012wenquan66/p/13660294.html