HDU - 6026 Deleting Edges

Little Q is crazy about graph theory, and now he creates a game about graphs and trees. 
There is a bi-directional graph with  nn nodes, labeled from 0 to  n1n−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  n1n−1 edges. 
(2) For every vertice  v(0<v<n)v(0<v<n), the distance between 0 and  vv on the tree is equal to the length of shortest path from 0 to  vv 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  ii and  jj, 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+7109+7.
InputThe input contains several test cases, no more than 10 test cases. 
In each test case, the first line contains an integer  n(1n50)n(1≤n≤50), denoting the number of nodes in the graph. 
In the following  nn lines, every line contains a string with  nncharacters. These strings describes the adjacency matrix of the graph. Suppose the  jj-th number of the  ii-th line is  c(0c9)c(0≤c≤9), if  ccis a positive integer, there is an edge between  ii and  jj with length of  cc, if  c=0c=0, then there isn't any edge between  ii and  jj
The input data ensure that the  ii-th number of the  ii-th line is always 0, and the  jj-th number of the  ii-th line is always equal to the  ii-th number of the  jj-th line.OutputFor each test case, print a single line containing a single integer, denoting the answer modulo  109+7109+7.Sample Input
2
01
10
4
0123
1012
2101

3210

给出一个图,要求把它删减成一个树,并且保证每给点与0点的距离是原来那个图的最小距离。

对于每一个点,它与0点的最小距离的个数,我们是通过枚举出来的。最小距离我们提前先通过最短路

算法求出来。之后枚举每一个j,如果d【j】+mp【i】【j】==d【i】,那么说明这是一条路径。我们

累加后再累乘即可。

#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<math.h> #include<queue> #include<stack> #include <vector> #include<iostream> using namespace std; char mp[110][110]; int d[110]; int n; void dijstra() {     priority_queue< pair<int,int>  ,vector< pair<int,int>  >,greater< pair<int,int>  > >q;     q.push(make_pair(0,0));     memset(d,1e9+8,sizeof(d));     d[0] = 0;     while(!q.empty())     {         pair<int,int> t = q.top();         q.pop();         int u = t.second;         if(d[u] < t.first)             continue;         for(int i = 0; i < n; i ++)         {             if(mp[u][i] && d[u] + mp[u][i] < d[i])             {                 d[i] = d[u] +mp[u][i];                 q.push(make_pair(d[i],i));             }         }     } } int main() {     while(cin>>n)     {         for(int i=0;i<n;i++)         {             cin>>mp[i];             for(int j=0;j<n;j++)             {                 mp[i][j]-='0';             }         }         dijstra();         long long ans=1;         long long cnt;         for(int i=1;i<n;i++)         {             cnt=0;             for(int j=0;j<n;j++)             {                 if(mp[i][j]&&d[i]==d[j]+mp[i][j])                 {                     cnt++;                 }             }             ans=ans*cnt%1000000007;         }         cout<<ans<<endl;     } }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值