您的位置:首页 > 其它

HDU 6026 Deleting Edges【最短路】【思维题】

2017-07-05 14:48 337 查看


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 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

 

Source

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

题意:给你一个图,让你删掉一些边后变成一棵树,这棵树的根是0号节点,要满足在树上从0到任意节点的最短距离和原图相等。问总共有多少种删法?

做法:

用dijkstra求出原图中0点到每个点的最短路的长度,再暴力的跑一遍判断能否通过别的点同样使得0点到当前点的距离仍是最短路径,如果有一个可替代点,则表示对于当前点存在一种可以删边的方法,将所有的可能删的方法相乘即可。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<functional>
#define INF 0x3f3f3f3f
#define ms(a,b) memset(a,b,sizeof(a))

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 55;

int a[maxn][maxn];
int d[maxn], n;

void dij()
{
ms(d, INF);
d[0] = 0;
bool book[maxn] = { 0 };
while (1)
{
int v = -1;
for (int i = 0; i < n; i++)
{
if (!book[i] && (v == -1 || d[i] < d[v]))
v = i;
}
if (v == -1) break;
book[v] = 1;
for (int i = 0; i < n; i++)
{
if (!book[i])
{
if (d[i] > d[v] + a[v][i])
{
d[i] = d[v] + a[v][i];
}
}
}
}
}

void solve()
{
ll ans = 1;
for (int i = 1; i < n; i++)
{
ll tmp = 0;
for (int j = 0; j < n; j++)
{
if (d[i] == d[j] + a[j][i])
{
tmp++;
}
}
ans = (ans*tmp) % mod;
}
printf("%lld\n", ans);
}

int main()
{
while (~scanf("%d", &n))
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
char p;
scanf(" %c", &p);
int q = p - '0';
if (q == 0)
q = INF;
a[i][j]= q;
}
}
dij();
solve();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: