2017 CCPC-WFinal&&HDOJ6026 Deleting Edges
来源:互联网 发布:免费刷空间留言软件 编辑:程序博客网 时间:2024/06/08 01:50
Deleting Edges
TimeLimit: 2000/1000 MS (Java/Others) Memory Limit:131072/131072 K (Java/Others)
Total Submission(s): 570 Accepted Submission(s): 212
Problem Description
LittleQ is crazy about graph theory, and now he creates a game about graphs andtrees.
There is a bi-directional graph with n nodes, labeledfrom 0 ton−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 toget 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 tov 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 andj, 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 withn characters. These strings describes the adjacency matrix of the graph. Suppose thej -th number of thei -th line isc(0≤c≤9), ifc is a positive integer, there is an edge between i and j with length ofc, ifc=0, then there isn't any edge between i and j.
The input data ensure that the i -th number of thei -th line is always 0, and the j -th number of the i -th line is always equal to thei -th number of thej -th line.
Output
For each test case, print a single line containing a single integer, denoting theanswer modulo 109+7.
Sample Input
2
01
10
4
0123
1012
2101
3210
Sample Output
1
6
题意:给出n个点以及它们的连接情况及边的权值,要求删除一些边后(可以不删除),使剩下的点构成一颗生成树(只有n-1条边,且所有点两两可达),且0到其余点的距离等于原来的图中它们之间的距离。
思路:我们先用dis[i]来表示0到点i的最短距离,由于要求删边前后距离不变,容易想到对于(i,j)两点,如果dis[i]+dis_(i->j)==dis[j]那么i到j的这一条边便可以作为我们要求的生成树的边。那么问题就转化为对应于每个点,有多少满足要求的边链接到它,有几条边就有几种情况,最后把每个点的情况数相乘即可。
上面说到的dis[i]只要跑一遍SPFA即可。
PS:在做求最短路类似的问题时,利用vector向量容器存边能显著提高运行效率
#include <cstdio>#include <vector>#include <queue>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define f(i,a,b) for(int i=(a);i<=(b);++i)#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn= 1005;const int mod = 1e9+7;const int INF = 0x3f3f3f3f;const double eps = 1e-6;char a[maxn][maxn];vector<int>vec[maxn];int vis[maxn];int dis[maxn];int num[maxn];void spfa(){ mst(vis,0); mst(dis,0x3f); dis[0]=0; vis[0]=1; queue<int>q; q.push(0); while(q.size()) { int cur=q.front(); q.pop(); vis[cur]=0; for(int i=0;i<vec[cur].size();i++) { int nex=vec[cur][i]; int w=a[cur][nex]-'0'; if(dis[nex]>dis[cur]+w) { dis[nex]=dis[cur]+w; if(vis[nex]==0) { vis[nex]=1; q.push(nex); } } } }}int main(){ int n; while(~scanf("%d",&n)) { mst(num,0); for(int i=0;i<n;i++) { scanf("%s",a[i]); vec[i].clear(); } for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { if(a[i][j]!='0') { vec[i].push_back(j); vec[j].push_back(i); } } } spfa(); for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(a[i][j]=='0') continue; int w=a[i][j]-'0'; if(dis[i]+w==dis[j]) num[j]++; } ll ans=1; for(int i=1;i<n;i++) { ans=(ans*num[i])%mod; } printf("%I64d\n",ans); } return 0;}
- 2017 CCPC-WFinal&&HDOJ6026 Deleting Edges
- 2017CCPC女生赛 hdu 6026 Deleting Edges
- 2017 CCPC-WFinal&&HDOJ 6024 Building Shops
- 2017 CCPC-WFinal&&HDOJ6025 Coprime Sequence(前缀后缀GCD问题)
- 2017 CCPC-WFinal&&HDOJ 6030 Happy Necklace(矩阵快速幂)
- HDU6026-Deleting Edges
- HDU6026 Deleting Edges
- HDU 6026 Deleting Edges
- hdu 6026 Deleting Edges
- HDU6026 Deleting Edges(Dijkstra)
- HDU 6026 Deleting Edges【图论】
- hdu 6026 Deleting Edges(最短路)
- 最短路径-Deleting Edges-hdu6026
- Hdu 6026 Deleting Edges【思维+最短路】
- HDU 6026 Deleting Edges【最短路】【思维题】
- HDU6026 Deleting Edges (最短路树计数)
- 2017 CCPC
- 2017CCPC秦皇岛 E:String of CCPC
- <s:checkbox id="doSele" name="tags" value="true" 的 fieldValue= 取值问题
- Self powers Problem 48
- java读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
- 修改AfxMessageBox对话框标题
- null value was assigned to a property of primitive type setter of原因急解决方法
- 2017 CCPC-WFinal&&HDOJ6026 Deleting Edges
- eclipse运行报java.lang.OutOfMemoryError: PermGen space解决方法
- 耐心积累
- java某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
- 用PHP Demo 学 RabbitMq
- Flask系列教程(2)——URL和视图
- nginx反向代理服务器搭建,应用服务器集群负载均衡
- 编写简单php,实现网络收音机web转台功能
- java 计算字符串中子串出现的次数