There is a bi-directional graph with nn nodes, labeled from 0 to n−1n−1. Every edge has its length, which is a positive integer ranged from 1 to 9.
Now, Little Q wants to delete some edges (or delete nothing) in the graph to get a new graph, which satisfies the following requirements:
(1) The new graph is a tree with n−1n−1 edges.
(2) For every vertice v(0<v<n)v(0<v<n), the distance between 0 and vv on the tree is equal to the length of shortest path from 0 to vv in the original graph.
Little Q wonders the number of ways to delete edges to get such a satisfied graph. If there exists an edge between two nodes ii and jj, while in another graph there isn't such edge, then we regard the two graphs different.
Since the answer may be very large, please print the answer modulo 109+7109+7.
In each test case, the first line contains an integer n(1≤n≤50)n(1≤n≤50), denoting the number of nodes in the graph.
In the following nn lines, every line contains a string with nncharacters. These strings describes the adjacency matrix of the graph. Suppose the jj-th number of the ii-th line is c(0≤c≤9)c(0≤c≤9), if ccis a positive integer, there is an edge between ii and jj with length of cc, if c=0c=0, then there isn't any edge between ii and jj.
The input data ensure that the ii-th number of the ii-th line is always 0, and the jj-th number of the ii-th line is always equal to the ii-th number of the jj-th line.OutputFor each test case, print a single line containing a single integer, denoting the answer modulo 109+7109+7.Sample Input
2 01 10 4 0123 1012 2101
3210
给出一个图,要求把它删减成一个树,并且保证每给点与0点的距离是原来那个图的最小距离。
对于每一个点,它与0点的最小距离的个数,我们是通过枚举出来的。最小距离我们提前先通过最短路
算法求出来。之后枚举每一个j,如果d【j】+mp【i】【j】==d【i】,那么说明这是一条路径。我们
累加后再累乘即可。
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<math.h> #include<queue> #include<stack> #include <vector> #include<iostream> using namespace std; char mp[110][110]; int d[110]; int n; void dijstra() { priority_queue< pair<int,int> ,vector< pair<int,int> >,greater< pair<int,int> > >q; q.push(make_pair(0,0)); memset(d,1e9+8,sizeof(d)); d[0] = 0; while(!q.empty()) { pair<int,int> t = q.top(); q.pop(); int u = t.second; if(d[u] < t.first) continue; for(int i = 0; i < n; i ++) { if(mp[u][i] && d[u] + mp[u][i] < d[i]) { d[i] = d[u] +mp[u][i]; q.push(make_pair(d[i],i)); } } } } int main() { while(cin>>n) { for(int i=0;i<n;i++) { cin>>mp[i]; for(int j=0;j<n;j++) { mp[i][j]-='0'; } } dijstra(); long long ans=1; long long cnt; for(int i=1;i<n;i++) { cnt=0; for(int j=0;j<n;j++) { if(mp[i][j]&&d[i]==d[j]+mp[i][j]) { cnt++; } } ans=ans*cnt%1000000007; } cout<<ans<<endl; } }