Hdu 6026 Deleting Edges【思维+最短路】
来源:互联网 发布:友盟数据统计 编辑:程序博客网 时间:2024/06/14 04:57
Deleting Edges
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 456 Accepted Submission(s): 162
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 withn![]()
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 withn−1![]()
edges.
(2) For every verticev(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 nodesi![]()
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 modulo10
9
+7![]()
.
There is a bi-directional graph with
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
(2) For every vertice
Little Q wonders the number of ways to delete edges to get such a satisfied graph. If there exists an edge between two nodes
Since the answer may be very large, please print the answer modulo
Input
The input contains several test cases, no more than 10 test cases.
In each test case, the first line contains an integern(1≤n≤50)![]()
, denoting the number of nodes in the graph.
In the followingn![]()
lines, every line contains a string with n![]()
characters. These strings describes the adjacency matrix of the graph. Suppose thej![]()
-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 thei![]()
-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.
In each test case, the first line contains an integer
In the following
The input data ensure that the
Output
For each test case, print a single line containing a single integer, denoting the answer modulo10
9
+7![]()
.
Sample Input
2011040123101221013210
Sample Output
16
题目大意:
给你N个点,然后给你一个邻接矩阵,让你求一共有多少种删除边的方式,使得构成一个生成树之后,从0到任意点v的距离都是原图的最短路的距离。
思路:
首先我们预处理出从点0到其他各个点的最短路的距离dist【i】.
那么对应如果有dist【i】+w==dist【j】,那么从i到j这条边就可以作为构成生成树的一条边。
那么我们将所有可以未做生成树的边的个数处理出来,那么对应在每个点上边取一条边出来作为答案即可。
那么累成结果就行了。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;char a[55][55];int dist[55];int vis[55];int ans[55];int n;void SPFA(){ memset(vis,0,sizeof(vis)); queue<int >s; for(int i=0;i<n;i++)dist[i]=0x3f3f3f3f; dist[0]=0; s.push(0); while(!s.empty()) { int u=s.front(); vis[u]=0; s.pop(); for(int i=0;i<n;i++) { if(a[u][i]!='0') { int v=i; int w=a[u][i]-'0'; if(dist[v]>dist[u]+w) { dist[v]=dist[u]+w; if(vis[v]==0) { vis[v]=1; s.push(v); } } } } }}int main(){ while(~scanf("%d",&n)) { for(int i=0;i<n;i++)scanf("%s",a[i]); SPFA(); memset(ans,0,sizeof(ans)); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(a[i][j]=='0')continue; else { int w=a[i][j]-'0'; if(dist[i]+w==dist[j]) { ans[j]++; } } } } __int64 output=1; for(int i=1;i<n;i++) { output*=(__int64)ans[i]; output%=1000000000+7; } printf("%I64d\n",output); }}
阅读全文
0 0
- Hdu 6026 Deleting Edges【思维+最短路】
- HDU 6026 Deleting Edges【最短路】【思维题】
- hdu 6026 Deleting Edges(最短路)
- HDU 6026 Deleting Edges
- hdu 6026 Deleting Edges
- HDU 6026 Deleting Edges【图论】
- HDU6026 Deleting Edges (最短路树计数)
- 2017CCPC女生赛 hdu 6026 Deleting Edges
- 最短路径-Deleting Edges-hdu6026
- HDU6026-Deleting Edges
- HDU6026 Deleting Edges
- HDU 6071 最短路思维题
- HDU6026 Deleting Edges(Dijkstra)
- hdu2433 最短路+思维
- hdu 4435 charge-station(最短路+思维,5级)
- hdu 5876 Sparse Graph【最短路+思维】好题
- Hdu 6166 Senior Pan【思维+随机化+最短路】好题~
- HDU 6166 Senior Pan(思维 最短路)
- 协方差矩阵
- Android 技术重温系列 (11)- 对话框的变化
- 尼采的哲学
- SC4.0基础笔记一
- 反走样
- Hdu 6026 Deleting Edges【思维+最短路】
- 函数和多维数组--c/c++
- <!DOCTYPE html>作用
- 线程的同步与互斥:互斥锁
- 机器学习&深度学习入门学习资料大全(一)
- Android Notification 的四种使用方式
- apache,tomcat,servlet概念整理
- JavaScript面向对象和高级01
- parallel_for_each