hdu 6026 最短路变形+删边构造树的方案数
2017-06-25 17:49
295 查看
题目:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 548 Accepted Submission(s): 198
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
给一个图,删除其中一些边使得剩下的边将N个点连成一棵树,并保证沿着树的边走,各个点到达0点的最短路径和原图一致
问有几种删边的方法
分析:
需要用到最短距离,需要先跑一遍单源最短路
求删边的方法,也就是求树的构造方法 树的构造方法=每条边的选择方法累乘
每条边(两点之间连边)的选择方案数即上一个点到达这个点同时能保持最短路径的方法数
即枚举i,j之间的边,如果其边权w满足d[i]+w=d[j],则由上一个顶点(不一定是i)到达j点的方案数ans[j]++
代码:
一开始floyd循环由外到内写成了i,j,k,狂wa不止.....
floyd思想是对于每一个中转点K都对任意两点i,j之间的最短路径进行松弛 如果i,j放在外层,则不能重复松弛 因为i只遍历了一遍
Deleting Edges
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 548 Accepted Submission(s): 198
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
给一个图,删除其中一些边使得剩下的边将N个点连成一棵树,并保证沿着树的边走,各个点到达0点的最短路径和原图一致
问有几种删边的方法
分析:
需要用到最短距离,需要先跑一遍单源最短路
求删边的方法,也就是求树的构造方法 树的构造方法=每条边的选择方法累乘
每条边(两点之间连边)的选择方案数即上一个点到达这个点同时能保持最短路径的方法数
即枚举i,j之间的边,如果其边权w满足d[i]+w=d[j],则由上一个顶点(不一定是i)到达j点的方案数ans[j]++
代码:
一开始floyd循环由外到内写成了i,j,k,狂wa不止.....
floyd思想是对于每一个中转点K都对任意两点i,j之间的最短路径进行松弛 如果i,j放在外层,则不能重复松弛 因为i只遍历了一遍
#include<bits/stdc++.h> using namespace std; const int maxn=1e5+20; const int inf=0x3f3f3f3f; const int mod=1e9+7; #define LL long long int n,d[55][55]; LL ans,cnt[55]; char s[55][55]; void floyd(){ for(int k=0;k<n;++k){ for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } } } } int main(){// while(scanf("%d",&n)==1){//少写了个==1 TLE for(int i=0;i<n;++i){ scanf("%s",s[i]); cnt[i]=0; } for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ if(i==j) d[i][j]=0; else if(s[i][j]=='0') d[i][j]=inf; else d[i][j]=s[i][j]-'0'; } } floyd(); for(int i=0;i<n;++i){//枚举所有边 for(int j=0;j<n;++j){ //if(i==j) continue; if(s[i][j]=='0') continue; if(d[0][i]+(s[i][j]-'0')==d[0][j]) cnt[j]++; } } ans=1; for(int i=1;i<n;++i){ ans*=cnt[i]; ans%=mod; } //ans%=mod; printf("%lld\n",ans); } return 0; }
相关文章推荐
- hdu 1284 钱币兑换问题(完全背包 变形---求方案总数)
- HDU 4318 Power transmission(最短路变形)
- HDU 5385(The path-构造最短路树)
- hdu 1595(最短路变形好题)
- hdu 1596(最短路变形)
- hdu 3499 最短路变形
- HDU 1548 A strange lift(构造+最短路Dijkstra)
- hdu 5385 The path(最短路+构造)
- HDU 2433 Travel (最短路,BFS,变形)
- hdu 3986 Harry Potter and the Final Battle(最短路变形)
- HDU 4360 最短路变形
- (阶段三 dijkstra1.4)HDU 1596 find the safest road(最短路的变形题:求乘积,求最大值)
- HDU 1546 Idiomatic Phrases Game 最短路变形(成语接字)
- HDU 1596 find the safest road(最短路变形)
- hdu 4360(最短路变形)
- hdu 2189 悼念512汶川大地震遇难同胞——来生一起走(完全 背包变形--求方案总数)
- hdu 4360(最短路变形)
- HDU 1596find the safest road(最短路变形)
- hdu 3832 Earth Hour (最短路变形)
- HDU 5361 最短路变形