2017中国大学生程序设计竞赛 - 女生专场 Deleting Edges(思维+最短路)
Deleting Edges
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 567 Accepted Submission(s): 210
There is a bi-directional graph with nodes, labeled from 0 to . 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 edges.
(2) For every vertice , the distance between 0 and on the tree is equal to the length of shortest path from 0 to 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 and , 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 .
In each test case, the first line contains an integer , denoting the number of nodes in the graph.
In the following lines, every line contains a string with characters. These strings describes the adjacency matrix of the graph. Suppose the -th number of the -th line is , if is a positive integer, there is an edge between and with length of , if , then there isn't any edge between and .
The input data ensure that the -th number of the -th line is always 0, and the -th number of the -th line is always equal to the -th number of the -th line.
题意:给你一个图,让你删掉一些边后变成一棵树,这棵树的根是0号节点,要满足在树上从0到任意节点的最短距离和原图相等。问总共有多少种删法?
做法:
用dijkstra求出原图中0点到每个点的最短路的长度,再暴力的跑一遍判断能否通过别的点同样使得0点到当前点的距离仍是最短路径,如果有一个可替代点,则表示对于当前点存在一种可以删边的方法,将所有的可能删的方法相乘即可。
#include <iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<deque>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007;
using namespace std;
int n;
int e[][];
int d[];
bool book[];
void dij()
{
memset(d,inf,sizeof(d));
memset(book,,sizeof(book));
for(int i=;i<=n;i++) d[i]=e[][i];
book[]=;
d[]=;
int k=-;
int mi=inf;
while()
{
k=-;
mi=inf;
for(int i=;i<=n;i++)
{
if(!book[i]&&d[i]<mi)
{
mi=d[i];
k=i;
}
}
if(k==-) break;
book[k]=;
for(int i=;i<=n;i++)
{
if(!book[i]&&d[i]>d[k]+e[k][i])
{
d[i]=d[k]+e[k][i];
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
char s[];
for(int i=;i<=n;i++)
{
cin>>s;
for(int j=;j<n;j++)
{
if(s[j]=='')
{
e[i][j+]=inf;
}
else e[i][j+]=s[j]-'';
}
}
dij();
ll ans=;
ll temp=;
for(int i=;i<=n;i++)
{
temp=;
for(int j=;j<=n;j++)
{
if(d[i]==d[j]+e[i][j])
{
temp++;
}
}
ans=(ans*temp)%mod;
}
printf("%lld\n",ans);
}
return ;
}
2017中国大学生程序设计竞赛 - 女生专场 Deleting Edges(思维+最短路)的更多相关文章
- 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)
Happy Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 2017中国大学生程序设计竞赛 - 女生专场(Graph Theory)
Graph Theory Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- 2017中国大学生程序设计竞赛 - 女生专场(dp)
Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- 2017中国大学生程序设计竞赛 - 女生专场 1002 dp
Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 2017中国大学生程序设计竞赛 - 女生专场C【前后缀GCD】
C HDU - 6025 [题意]:去除数列中的一个数字,使去除后的数列中所有数字的gcd尽可能大. [分析]: 数组prefixgcd[],对于prefixgcd[i]=g,g为a[0]-a[i]的 ...
- 2017中国大学生程序设计竞赛 - 女生专场B【DP】
B HDU - 6024 [题意]:n个教室,选一些教室建造糖果商店. 每个教室,有一个坐标xi和在这个教室建造糖果商店的花费ci. 对于每一个教室,如果这个教室建造糖果商店,花费就是ci,否则就是与 ...
- 2017中国大学生程序设计竞赛 - 女生专场A【模拟】
A HDU - 6023 [题意]:求AC题数和总时长. [分析]:模拟.设置标记数组记录AC与否,再设置错题数组记录错的次数.罚时罚在该题上,该题没AC则不计入总时间,AC则计入.已经AC的题不用再 ...
- HDU 6024(中国大学生程序设计竞赛女生专场1002)
这是CCPC女生专场的一道dp题.大佬们都说它简单,我并没有感到它有多简单. 先说一下题意:在一条直线上,有n个教室,现在我要在这些教室里从左到右地建设一些作为糖果屋,每个教室都有自己的坐标xi 和建 ...
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场
Combine String #include<cstdio> #include<cstring> #include<iostream> #include<a ...
随机推荐
- ECS vs. Kubernetes 类似而又不同
C2Container Service (ECS)和Kubernetes (K8s) 都解决了同样的问题:跨越主机集群管理容器.ECS和Kubernetes之间的斗争让我想起了vi和Emacs之间的编 ...
- TiDB 在摩拜单车的深度实践及应用
一.业务场景 摩拜单车 2017 年开始将 TiDB 尝试应用到实际业务当中,根据业务的不断发展,TiDB 版本快速迭代,我们将 TiDB 在摩拜单车的使用场景逐渐分为了三个等级: P0 级核心业务: ...
- combo
什么是combo 上一节中我们有一行代码: <script src="http://g.tbcdn.cn/kissy/k/1.4.8/??seed-min.js,import-styl ...
- 深入理解Lambda
概述 Lambda是一个表达式,也可以说它是一个匿名函数.然而在使用它或是阅读Lambda代码的时候,却显得并不那么容易.因为它匿名,因为它删减了一些必要的说明信息(比如方法名).下面就来说说Lamb ...
- 用phpexcel插件导出excel2003
ob_end_clean();//清空缓冲区并关闭输出缓冲(清除脏数据). header('Content-Type:application/vnd.ms-execel'); header('Cont ...
- iOS-免证书真机调试
使用方法: 1.新建一个普通的项目 2.进入xcode,菜单栏选择xcode –> preferences (快捷键 command + ,) 3.在Accounts选项卡添加自己的Apple ...
- CrateDB——全文搜索使用的是lucene,尚不知其底层实现
CrateDB: The fast, scalable, easy to use SQL database with native full text search https://crate.io ...
- HDU 1410 PK武林盟主
Problem Description 枫之羽认为自己很强,想当武林盟主,于是找现任武林盟主氢氧化铜挑战.氢氧化铜欣然接受了挑战,两人约好于下个月的月圆之夜在HDU校园内的三根柱子上进行决战.这场PK ...
- Developing on Windows Phone 8 Devices
Developing on Windows Phone 8 Deviceshttp://docs.madewithmarmalade.com/native/platformguides/wp8guid ...
- [python] 排序的动态展示
两句闲话 本文所说的排序是指基于交换的排序.因此,按理来说,本文应该叫基于交换的排序的动态展示,但是这样太拗口了. 效果展示 最终效果如下. 实现方法 需要说明的是,在这里是通过pygame来实现图形 ...