2017 CCPC-WFinal&&HDOJ6026 Deleting Edges

来源:互联网 发布:免费刷空间留言软件 编辑:程序博客网 时间:2024/06/08 01:50


Deleting Edges

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:131072/131072 K (Java/Others)
Total Submission(s): 570    Accepted Submission(s): 212

Problem Description

LittleQ is crazy about graph theory, and now he creates a game about graphs andtrees.
There is a bi-directional graph with
n nodes, labeledfrom 0 ton−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 toget 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 tov 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 andj, 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 withn characters. These strings describes the adjacency matrix of the graph. Suppose thej -th number of thei -th line isc(0≤c≤9), ifc is a positive  integer, there is an edge between i and j with length ofc, ifc=0, then there isn't any edge between i and  j.
The input data ensure that the
i -th number of thei -th line is always 0, and the j -th number of the i -th line is always equal to thei -th number of thej -th line.

 

 

Output

For each test case, print a single line containing a single integer, denoting theanswer modulo 109+7.

 

 

Sample Input

2

01

10

4

0123

1012

2101

3210

 

 

Sample Output

1

6



题意:给出n个点以及它们的连接情况及边的权值,要求删除一些边后(可以不删除),使剩下的点构成一颗生成树(只有n-1条边,且所有点两两可达),且0到其余点的距离等于原来的图中它们之间的距离。


思路:我们先用dis[i]来表示0到点i的最短距离,由于要求删边前后距离不变,容易想到对于(i,j)两点,如果dis[i]+dis_(i->j)==dis[j]那么i到j的这一条边便可以作为我们要求的生成树的边。那么问题就转化为对应于每个点,有多少满足要求的边链接到它,有几条边就有几种情况,最后把每个点的情况数相乘即可。


上面说到的dis[i]只要跑一遍SPFA即可。


PS:在做求最短路类似的问题时,利用vector向量容器存边能显著提高运行效率



#include <cstdio>#include <vector>#include <queue>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define f(i,a,b) for(int i=(a);i<=(b);++i)#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn= 1005;const int mod = 1e9+7;const int INF = 0x3f3f3f3f;const double eps = 1e-6;char a[maxn][maxn];vector<int>vec[maxn];int vis[maxn];int dis[maxn];int num[maxn];void spfa(){    mst(vis,0);    mst(dis,0x3f);    dis[0]=0;    vis[0]=1;    queue<int>q;    q.push(0);    while(q.size())    {        int cur=q.front();        q.pop();        vis[cur]=0;        for(int i=0;i<vec[cur].size();i++)        {            int nex=vec[cur][i];            int w=a[cur][nex]-'0';            if(dis[nex]>dis[cur]+w)            {                dis[nex]=dis[cur]+w;                if(vis[nex]==0)                {                    vis[nex]=1;                    q.push(nex);                }            }        }    }}int main(){    int n;    while(~scanf("%d",&n))    {        mst(num,0);        for(int i=0;i<n;i++)        {            scanf("%s",a[i]);            vec[i].clear();        }        for(int i=0;i<n;i++)        {            for(int j=0;j<i;j++)            {                if(a[i][j]!='0')                {                    vec[i].push_back(j);                    vec[j].push_back(i);                }            }        }        spfa();        for(int i=0;i<n;i++)        for(int j=0;j<n;j++)        {            if(a[i][j]=='0')    continue;            int w=a[i][j]-'0';            if(dis[i]+w==dis[j])                num[j]++;        }        ll ans=1;        for(int i=1;i<n;i++)        {            ans=(ans*num[i])%mod;        }        printf("%I64d\n",ans);    }    return 0;}












阅读全文
0 0
原创粉丝点击