Deleting Edges
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2210 Accepted Submission(s): 730
Problem Description
Little Q is crazy about graph theory, and now he creates a game about graphs and trees.
There is a bi-directional graph with n nodes, labeled from 0 to n−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−1 edges.
(2) For every vertice v(0<v<n), the distance between 0 and v on the tree is equal to the length of shortest path from 0 to v 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 i and j, 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+7.
Input
The input contains several test cases, no more than 10 test cases.
In each test case, the first line contains an integer n(1≤n≤50), denoting the number of nodes in the graph.
In the following n lines, every line contains a string with n characters. These strings describes the adjacency matrix of the graph. Suppose the j-th number of the i-th line is c(0≤c≤9), if c is a positive integer, there is an edge between i and j with length of c, if c=0, then there isn't any edge between i and j.
The input data ensure that the i-th number of the i-th line is always 0, and the j-th number of the i-th line is always equal to the i-th number of the j-th line.
Output
For each test case, print a single line containing a single integer, denoting the answer modulo 109+7.
Sample Input
2 01 10 4 0123 1012 2101 3210
Sample Output
1 6
最短路计数然后乘起来得出结果
#include<bits/stdc++.h>
using namespace std;
const long long maxn=55000;
const long long inf=0x3f3f3f3f;
const long long mod=1e9+7;
char a;
int n,cnt,dis[maxn],ans[maxn],head[maxn],vis[maxn];
struct aa
{
long long u,v,w,next;
}e[maxn*3];
struct node
{
long long u,w;
bool operator < (const node&x) const
{
return w>x.w;
}
};
void add(long long u,long long v,long long w)
{
cnt++;
e[cnt].u=u;
e[cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
int main()
{
while(cin>>n)
{
cnt=0;
memset(head,0,sizeof(head));
memset(e,0,sizeof(e));
for(long long i=1;i<=n;i++)
for(long long j=1;j<=n;j++)
{
cin>>a;
if(a!='0')
{
add(i,j,a-'0');//存边
}
}
//下面就是 dijkstr
for(long long i=1;i<=n;i++) dis[i]=inf;
dis[1]=0;
priority_queue<node> q;
q.push(node{1,0});
memset(vis,0,sizeof(vis));
memset(ans,0,sizeof(ans));
ans[1]=1;
while(!q.empty())
{
node w=q.top();
q.pop();
long long u=w.u;
if(vis[u]) continue;
vis[u]=1;
for(long long i=head[u];i;i=e[i].next)
{
long long v=e[i].v;
if(dis[v]>dis[u]+e[i].w)
{
dis[v]=dis[u]+e[i].w;
ans[v]=1;//最短路的边数
q.push(node{v,dis[v]});
}
else if(dis[v]==dis[u]+e[i].w)
{
ans[v]++;//边数加一
}
}
}
long long sum=1;
for(long long i=1;i<=n;i++)
{
sum*=ans[i];//让边数相乘得结果
sum%=mod;
}
cout<<sum<<endl;
}
return 0;
}