https://www.luogu.org/problemnew/show/P2047
https://www.lydsy.com/JudgeOnline/problem.php?id=1491
也可以用floyed做掉
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 using namespace std; 6 #define fi first 7 #define se second 8 #define mp make_pair 9 #define pb push_back 10 typedef long long ll; 11 typedef unsigned long long ull; 12 typedef pair<int,int> pii; 13 ll n,m; 14 ll a[110][110],d[110][110]; 15 ll n1[110][110];//最短路计数 16 double an[110]; 17 int main() 18 { 19 ll i,j,k,x,y,z; 20 scanf("%lld%lld",&n,&m); 21 memset(a,0x3f,sizeof(a)); 22 for(i=1;i<=n;++i) 23 a[i][i]=0; 24 for(i=1;i<=m;++i) 25 { 26 scanf("%lld%lld%lld",&x,&y,&z); 27 a[x][y]=a[y][x]=min(a[x][y],z); 28 } 29 memcpy(d,a,sizeof(d)); 30 for(i=1;i<=n;++i) 31 for(j=1;j<=n;++j) 32 n1[i][j]=1; 33 for(k=1;k<=n;++k) 34 for(i=1;i<=n;++i) 35 for(j=1;j<=n;++j) 36 if(i!=k&&i!=j&&k!=j) 37 { 38 if(d[i][j]>d[i][k]+d[k][j]) 39 { 40 d[i][j]=d[i][k]+d[k][j]; 41 n1[i][j]=n1[i][k]*n1[k][j]; 42 } 43 else if(d[i][j]==d[i][k]+d[k][j]) 44 { 45 n1[i][j]+=n1[i][k]*n1[k][j]; 46 } 47 } 48 //for(i=1;i<=n;++i) 49 // for(j=1;j<=n;++j) 50 // printf("1t%lld %lld %lld %lld ",i,j,d[i][j],n1[i][j]); 51 for(i=1;i<=n;++i) 52 { 53 for(j=1;j<=n;++j) 54 for(k=1;k<=n;++k) 55 if(i!=j&&j!=k&&i!=k) 56 { 57 if(d[j][i]+d[i][k]==d[j][k]) 58 { 59 an[i]+=double(n1[j][i])*n1[i][k]/n1[j][k]; 60 } 61 } 62 printf("%.3f ",an[i]); 63 } 64 return 0; 65 }