Deleting Edges

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 567    Accepted Submission(s): 210

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  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 .
 
Input
The input contains several test cases, no more than 10 test cases.
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.
 
Output
For each test case, print a single line containing a single integer, denoting the answer modulo .
 
Sample Input

2 01 10 4 0123 1012 2101 3210
 
Sample Output

1 6
 
Source

题意:给你一个图,让你删掉一些边后变成一棵树,这棵树的根是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(思维+最短路)的更多相关文章

  1. 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)

    Happy Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  2. 2017中国大学生程序设计竞赛 - 女生专场(Graph Theory)

    Graph Theory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  3. 2017中国大学生程序设计竞赛 - 女生专场(dp)

    Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...

  4. 2017中国大学生程序设计竞赛 - 女生专场 1002 dp

    Building Shops Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  5. 2017中国大学生程序设计竞赛 - 女生专场C【前后缀GCD】

    C HDU - 6025 [题意]:去除数列中的一个数字,使去除后的数列中所有数字的gcd尽可能大. [分析]: 数组prefixgcd[],对于prefixgcd[i]=g,g为a[0]-a[i]的 ...

  6. 2017中国大学生程序设计竞赛 - 女生专场B【DP】

    B HDU - 6024 [题意]:n个教室,选一些教室建造糖果商店. 每个教室,有一个坐标xi和在这个教室建造糖果商店的花费ci. 对于每一个教室,如果这个教室建造糖果商店,花费就是ci,否则就是与 ...

  7. 2017中国大学生程序设计竞赛 - 女生专场A【模拟】

    A HDU - 6023 [题意]:求AC题数和总时长. [分析]:模拟.设置标记数组记录AC与否,再设置错题数组记录错的次数.罚时罚在该题上,该题没AC则不计入总时间,AC则计入.已经AC的题不用再 ...

  8. HDU 6024(中国大学生程序设计竞赛女生专场1002)

    这是CCPC女生专场的一道dp题.大佬们都说它简单,我并没有感到它有多简单. 先说一下题意:在一条直线上,有n个教室,现在我要在这些教室里从左到右地建设一些作为糖果屋,每个教室都有自己的坐标xi 和建 ...

  9. "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场

    Combine String #include<cstdio> #include<cstring> #include<iostream> #include<a ...

随机推荐

  1. Ubuntu 通过APT安装Tomcat

    Ubuntu 通过APT安装Tomcat 安装 sudo apt-get install tomcat8 tomcat8-docs tomcat8-examples tomcat8-admin # s ...

  2. 红米手机.驱动.XP安装

    1.发现 官网上下载的 驱动在 XP下安装不上去... (Win7 记得 貌似 没有问题...) 1.1.网上搜到的 解决方案为:解决手机不能连电脑 XP系统无法安装MTP设备驱动的终极解决_小米No ...

  3. scala学习手记32 - trait选择性混入

    继续上一节. 狗当然是人类的好朋友.但是藏獒呢?这玩意儿又蠢又笨又凶狠,肯定不能算很多人的好朋友了.其实,刚才那句话还可以修正一下下:我们接受的狗才是我们的好朋友. 用程序怎么实现呢?在java里面, ...

  4. 配置mybatis-config.xml出现过很诡异的现象

    1 首先得保证包的导入正确 2 然后如果把mybatis-config.xml放在src的某个文件夹下,最后能够build path 3 之后一直报 Archive for required libr ...

  5. C++(十八) — 内存模式、堆和栈

    1.内存模式 一个程序执行时,先复制到内存,然后CPU逐句读取指令执行. 每个存储单元存放一个字节(8bit)数据,每个有一个唯一的地址,地址是顺序编码的.比如:一台计算机256MB内存,则有256* ...

  6. Nginx优化思路

    对于高性能网站 ,请求量大,如何支撑? 1方面,要减少请求 对于开发人员----合并css, 背景图片, 减少mysql查询等. 2: 对于运维 nginx的expires ,利用浏览器缓存等,减少查 ...

  7. flask bootstrap IE11的 兼容性问题

    在子模板内添加如下代码确保IE11的兼容性问题. {% extends 'bootstrap/base.html' %}{% block metas %} <meta http-equiv=&q ...

  8. LeetCode OJ:Merge Two Sorted Lists(合并两个链表)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  9. [置顶] Android逆向从未如此简单

    哈,又标题党了..不过我一定竭尽所能,写一篇最亲民的入门文章. 本文仅供学习交流之用,切勿用于非法用途,读者若运用所学知识,进行非法任何商业目的或者非法牟利,一切责任由操作者自行承担,与本人无关.希望 ...

  10. 你真的对java static了解吗,代码优化可能更加简单

    static修饰的成员变量和成员方法独立于该类的任何对象.也就是说,它不依赖类特定的实例,被类的所有实例共享. 只要这个类被加载,Java虚拟机就能根据类名在运行时数据区的方法区内定找到他们.因此,s ...