HDU-6026 Deleting Edges(dijkstra最短路)

题目链接:Deleting Edges

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

 

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

 

题意:给出一个图,要求删除一些边,然后使得删除后的图是一颗树,并且各个点离原点0的距离为原来图中的最短距离

注意删除可以删除X条边,也可以不删除,给出有多少种删法。只要在一个图里存在一条边在另一个图里不存在就称为不同的图

题解:先求出0点到每个点的最短距离,然后统计有多少种最短距离,最后相乘就是答案

算法:dijkstra顺带优化了一下

#include<bits/stdc++.h>
using namespace std;
const int maxn=100;
int a[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int to[maxn];
int n;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
struct node
{
	int x,d;
	node(int a,int b)
	{
		x=a,d=b;
	}
	bool operator < (const node&w) const
	{
		return d>w.d;
	}
};
vector<node>ar[maxn];
void dijkstra(int s)
{
	for(int i=0; i<n; i++)
	{
		dis[i]=INF;
	}
	priority_queue<node>q;
	dis[0]=0;
	q.push(node(s,0));
	while(!q.empty())
	{
		node u=q.top();
		q.pop();
		if(vis[u.x]==1) continue;
		vis[u.x]=1;
		for(int i=0; i<ar[u.x].size(); i++)
		{
			node y=ar[u.x][i];
			if(dis[y.x]>u.d+y.d)
			{
				to[y.x]=1;
				dis[y.x]=u.d+y.d;
				q.push(node(y.x,dis[y.x]));
			}
			else if(dis[y.x]==u.d+y.d)
			{
				to[y.x]++;
			}
		}
	}
//	for(int i=0;i<n;i++)
//	{
//		cout<<dis[i]<<endl;
//	}
}
int main()
{
	while(~scanf("%d",&n))
	{
		memset(to,0,sizeof to);
		memset(vis,0,sizeof vis);
		memset(a,0,sizeof a);
		for(int i=0;i<=n;i++)
			ar[i].clear();
		for(int i=0; i<n; i++)
		{
			for(int j=0; j<n; j++)
			{
				int x;
				scanf("%1d",&x);
				a[i][j]=x;
				if(a[i][j]==0) continue;
				else ar[i].push_back(node(j,x));
			}
		}
		dijkstra(0);
		long long ans=1;
		for(int i=1; i<n; i++)
		{
			//cout<<to[i]<<endl;
			ans=(ans*to[i])%mod;
		}
		cout<<ans<<endl;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值