Enter a description Input Description
The first line in the input file has two integers, N and m, representing the number of nodes and non-forward edges in the social network. In the no-map, we numbered all the nodes from 1 to N. Next m line, each line with three integers a, B, C describes a connection node A and B, the weight of C is a non-forward edge. Note that there is a maximum of one non-tangential edge between any two nodes, and no self-loops are present in the graph (i.e., the two endpoints that do not have a non-forward edge are the same nodes).
Output Description Output Description
The output file consists of n rows, one real number per line, and 3 digits after the decimal point. The real number in line I indicates how important the node I is in social networks
Sample Input Sample Input
4 4
1 2 1
2 3 1
3 4 1
4 1 1
Sample Output Sample Output
1.000
1.000
1.000
1.000
is 1
The FOLYD algorithm handles the C array at the same time as the shortest path, and then computes the I array. See code for details.
Code:
#include <iostream>#include<cstring>#include<cstdio>#defineSize 105using namespacestd;intn,m;DoubleG[size][size];DoubleC[size][size];DoubleI[size];intMain () {CIN>>n>>m; for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) G[i][j]=1e15,c[i][j]=0; intb;DoubleW; for(intI=1; i<=m;i++) {cin>>a>>b>>W; G[A][B]=g[b][a]=W; C[A][B]=c[b][a]=1; } for(intk=1; k<=n;k++){ for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++){ if(k==i| | k==j| | I==J)Continue; if(g[i][k]+g[k][j]<G[i][j]) {G[i][j]=g[i][k]+G[k][j]; C[I][J]=0; } if(g[i][k]+g[k][j]==G[i][j]) {C[i][j]+=c[i][k]*C[k][j]; } } } } /*for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {cout<<c[i][j]<< "; } cout<<endl; } for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {cout<<g[i][j]<< "; } cout<<endl; } */ for(intk=1; k<=n;k++){ for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++){ if(k==i| | k==j| | I==J)Continue; if(g[i][k]+g[k][j]==g[i][j]&&c[i][j]>0) {I[k]+=c[i][k]*c[k][j]/C[i][j]; } } } } for(intI=1; i<=n;i++) printf ("%.3lf\n", I[i]); return 0;}
code1796 Social Network