Title Description
After successfully breaking through Lord lsp's defense line, lqr the party came to the bottom of Lord lsp's castle.
After Lord lsp blackened, although he has powerful super ability and can make buildings with his mind, his IQ level has not increased much.
Now lqr it is clear that the dark castle has N rooms, M two-way channels that can be made, and the length of each channel.
lqr is well aware of Lord lsp's idea. In order to avoid thinking about the shortest path between two rooms every time, Lord lsp will build the castle into a tree.
However, in order to improve their mobility efficiency as much as possible, Lord lsp will make the castle meet the following conditions:
Let D[i] be the shortest path length between room I and room 1 if all channels are built; S[i] is the path length between room I and room 1 in the actually built tree castle; It is required that S[i]=D[i] holds for all integers I.
In order to defeat Lord lsp, lqr wants to know how many different castle building schemes there are.
You need to output the result after the answer is modeled on 231 – 1.
Input format
The first line has two integers N and M.
After that, there are three integers x, Y and l in line M, indicating that a channel with length L between X and Y can be built.
Output format
An integer that represents the result after the answer modulo 231 – 1.
Data range
2
≤
N
≤
1000
,
2≤N≤1000,
2≤N≤1000,
N
−
1
≤
M
≤
N
(
N
−
1
)
/
2
,
N−1≤M≤N(N−1)/2,
N−1≤M≤N(N−1)/2,
1
≤
L
≤
100
1≤L≤100
1≤L≤100
Input sample:
3 3
1 2 2
1 3 1
2 3 1
Output example:
2
Problem solution
This problem is actually the number of spanning trees that require the shortest path
Firstly, a concept is introduced. What is the shortest path spanning tree?
That is, in the tree we generated, the path from the source point to each point is equal to the shortest path from the source point to each point in the undirected graph
Therefore, we first need to know the shortest path size from the source point to each point in an undirected graph
Run Dijkstra once and you'll be ok
We can traverse each edge if
d
i
s
[
x
]
+
e
d
g
e
[
i
]
.
v
a
l
dis[x]+edge[i].val
dis[x]+edge[i].val is exactly equal to
d
i
s
[
y
]
dis[y]
dis[y], then we can have an arrival node
y
y
y path, we can count the number of paths to each node, that is, whenever the above conditions are met, we make
c
n
t
[
y
]
+
+
cnt[y]++
cnt[y] + +, according to the multiplication principle, multiplies the number of paths to each node, that is, the number of minimum path spanning trees that can be generated
code
#include<bits/stdc++.h> using namespace std; const int N=1010; const int M=N*(N-1)/2; int n,m; struct node { int nxt,to,val; }edge[2*M]; int head[N],tot=0; void Lian(int x,int y,int z) { tot++; edge[tot].to=y; edge[tot].nxt=head[x]; edge[tot].val=z; head[x]=tot; } struct Point { int d,p; bool operator<(const Point &x)const{ return d>x.d; } }; int dis[N]; bool vis[N]; void dijkstra() { memset(dis,0x3f,sizeof(dis)); priority_queue<Point> q; dis[1]=0; Point a; a.d=0,a.p=1; q.push(a); while(!q.empty()) { a=q.top(); q.pop(); int x=a.p; if(vis[x]) continue; vis[x]=1; for(int i=head[x];i;i=edge[i].nxt) { int y=edge[i].to; if(dis[x]+edge[i].val<dis[y]) { dis[y]=edge[i].val+dis[x]; a.p=y,a.d=dis[y]; q.push(a); } } } } long long cnt[N]; const int Mod=(1<<31)-1; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { int x,y,l; scanf("%d%d%d",&x,&y,&l); Lian(x,y,l); Lian(y,x,l); } dijkstra(); cnt[1]=1; for(int i=1;i<=n;i++) for(int j=head[i];j;j=edge[j].nxt) { int y=edge[j].to; if(dis[i]+edge[j].val==dis[y]) cnt[y]++; } long long ans=1; for(int i=1;i<=n;i++) ans=ans*cnt[i]%Mod; cout<<ans; return 0; }