meaning of the title
There is an undirected graph with n nodes, the nodes are numbered from 0-n-1, and the length of each edge is a positive integer from 1 to 9. Now we need to delete some edges (or not delete them) so that the resulting new graph satisfies the following two requirements.
1. The new graph is a tree with n-1 edges 2. For each node v (0ton-1), the distance from 0 to v should be equal to the shortest path from 0 to v on the original graph
Now count how many different graphs satisfy the above two conditions
analyze
Count the in-degree values that each point can reach the shortest path, and then multiply. This question has been adjusted for a long time because of the wrong spelling of spfa...
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 7 using namespace std; 8 const int INF=2147483600; 9 const int maxn=55; 10 const int MOD=1000000007; 11 int n; 12 char g[maxn][maxn]; 13 int G[maxn][maxn]; 14 int d[maxn],vis[maxn]; 15 long long in[maxn]; 16 long long ans; 17 void spfa(){ 18 memset(vis,0,sizeof(vis)); 19 for(int i=0;i<=n;i++)d[i]=INF; 20 queue<int>q; 21 q.push(0);vis[0]=1; 22 d[0]=0; 23 while(!q.empty()){ 24 int u=q.front();q.pop(); 25 vis[u]=0; 26 for(int i=0;i<n;i++){ 27 if(G[u][i]){ 28 if(d[i]>=d[u]+G[u][i]){ 29 d[i]=d[u]+G[u][i]; 30 if(!vis[i]){ 31 vis[i]=1; 32 q.push(i); 33 } 34 } 35 } 36 } 37 } 38 return; 39 } 40 int main(){ 41 while(scanf("%d",&n)!=EOF){ 42 memset(G,0,sizeof(G)); 43 for(int i=0;i<n;i++){ 44 for(int j=0;j<n;j++){ 45 scanf(" %c",&g[i][j]); 46 } 47 } 48 for(int i=0;i<n;i++){ 49 for(int j=0;j<n;j++){ 50 G[i][j]=g[i][j]-'0'; 51 } 52 } 53 spfa(); 54 memset(in,0,sizeof(in)); 55 for(int i=0;i<n;i++){ 56 for(int j=0;j<n;j++){ 57 if(i!=j){ 58 if(G[i][j]&&d[j]==d[i]+G[i][j]){ 59 in[j]++; 60 } 61 } 62 } 63 } 64 ans=1; 65 for(int i=0;i<n;i++){ 66 if(in[i]) 67 ans=(ans%MOD*in[i]%MOD)%MOD; 68 } 69 printf("%lld\n",ans); 70 } 71 return 0; 72 }