hdu 6026 Deleting Edges 江苏徐州邀请赛K

Deleting Edges

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

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中国大学生程序设计竞赛 - 女生专场

Recommend

jiangzijing2015   |   We have carefully selected several similar problems for you:  6297 6296 6295 6294 6293

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
string s[100];
int mp[100][100],n;
int dis[100],vis[100],in_[100];
struct node{
    int to,v;
    friend bool operator < (node a,node b){return a.v<b.v;};
    friend bool operator > (node a,node b){return a.v>b.v;};
};
void dij(){
    priority_queue<node,vector<node>,greater<node> >q;
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[0]=0;vis[0]=1;q.push({0,0});
    while(!q.empty()){
        node u=q.top();q.pop();
        if(u.v>dis[u.to]) continue;
        for(int i=0;i<n;i++){
            if(mp[u.to][i]==0) continue;
            if(dis[i]>dis[u.to]+mp[u.to][i]){
                dis[i]=dis[u.to]+mp[u.to][i];
                q.push({i,dis[i]});
            }
        }
    }
}
int main(){
    while(cin>>n){
        for(int i=0;i<n;i++) cin>>s[i];
        memset(in_,0,sizeof(in_));
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                mp[i][j]=s[i][j]-‘0‘;
        dij();
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(mp[i][j]==0) continue;
                if(dis[i]+mp[i][j]==dis[j]) in_[j]++;
            }
        }
        long long ans=1;
        for(int i=1;i<n;i++){
            if(in_[i])(ans*=in_[i])%=mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
} 

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
string s[100];
int mp[100][100],n;
int dis[100],vis[100],in_[100];
void dij(){
    //priority_queue<int,vector<int>,greater<int> >q;
    queue<int>q;
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[0]=0;vis[0]=1;q.push(0);
    while(!q.empty()){
        int u=q.front();q.pop();vis[u]=0;
        for(int i=0;i<n;i++){
            if(mp[u][i]==0) continue;
            if(dis[i]>dis[u]+mp[u][i]){
                dis[i]=dis[u]+mp[u][i];
                if(!vis[i]){
                    q.push(i);
                    vis[i]=1;
                }
            }
        }
    }
}
int main(){
    while(cin>>n){
        for(int i=0;i<n;i++) cin>>s[i];
        memset(in_,0,sizeof(in_));
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                mp[i][j]=s[i][j]-‘0‘;
        dij();
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(mp[i][j]==0) continue;
                if(dis[i]+mp[i][j]==dis[j]) in_[j]++;
            }
        }
        long long ans=1;
        for(int i=1;i<n;i++){
            if(in_[i])(ans*=in_[i])%=mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
} 


原文地址:https://www.cnblogs.com/vainglory/p/9142280.html

时间: 2024-10-17 00:15:13

hdu 6026 Deleting Edges 江苏徐州邀请赛K的相关文章

HDU 6026 Deleting Edges

最短路. 先建一个只包含最短路的有向无环图,每一个点选择任意一条入边即可生成一个树形图,那么树的种类就等于每个点的入度乘积. #include <bits/stdc++.h> using namespace std; const long long mod = 1e9+7; int n; char s[60][60]; int dis[60],f[60]; int in[60]; void spfa() { queue<int>Q; for(int i=0;i<n;i++)

江苏徐州邀请赛 I题 类矩阵快速幂

题意:即一个m个点的图,求走n步,所能获得的最大权值n(2e5),m(100)时间限制:2s,样例:10组思路1:dp dp[i][j]表示i步到达j所能获得的最大权值 for i=1:n for j=1:m for k=1:m dp[i][j]=max(dp[i][j],dp[i][k]+v[k][j]);复杂度为 n*m^2, 复杂度会爆的思路2:考虑下最短路中松弛的说法,我们定义为扩张for i=1:m for j=1:m for k=1:m f[i][j]=max(f[i][j],f[i

2017中国大学生程序设计竞赛 - 女生专场 Deleting Edges(思维+最短路)

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 grap

HDU 4849 Wow! Such City!陕西邀请赛C(最短路)

HDU 4849 Wow! Such City! 题目链接 题意:按照题目中的公式构造出临接矩阵后,求出1到2 - n最短路%M的最小值 思路:就根据题目中方法构造矩阵,然后写一个dijkstra,利用d数组取求答案即可 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const long long I

2013 南京邀请赛 K题 yet another end of the world

1 /** 2 大意:给定一组x[],y[],z[] 确定有没有两个不同的x[i], x[j] 看是否存在一个ID使得 3 y[i]<=ID%x[i]<=z[i] 4 y[j]<=ID%x[j]<=z[j] 5 设ID%x[i] = a ID%x[j] = b 6 ===〉ID+x[i]*x=a, ID+x[j]*y = b; 7 两式相减得 x[j]*y - x[i]*x = b-a; 8 若是有解 就是(b-a)%gcd(x[i],x[j]) == 0 9 就是看b-a的范围内

HDU 6121 Build a tree(完全K叉树)

http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:给你一颗完全K叉树,求出每棵子树的节点个数的异或和. 思路: 首先需要了解一些关于完全K叉树或满K叉树的一些知识: 对于每棵子树,只有三种情况: ①是满K叉树  ②不是满K叉树  ③叶子节点 并且非满K叉树最多只有一个,所以只需要将它进行特殊处理,具体看代码吧,说不清楚.代码参考了http://blog.csdn.net/my_sunshine26/article/details/77200282

HDU 5412 CRB and Queries(区间第K大 树套数 按值建树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5412 Problem Description There are N boys in CodeLand. Boy i has his coding skill Ai. CRB wants to know who has the suitable coding skill. So you should treat the following two types of queries. Query 1:

hdu 4848 搜索+剪枝 2014西安邀请赛

http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,其实不难.... 但是说实话,现在对题意还是理解不太好...... 犯的错误: 1.floy循环次序写错, 2.搜索的时候,应该先判断i是不是可以搜(就是可不可能产生解),然后标记vis[i]=1,我二逼的先标记vis[i]=1,然后判断i是不是可搜,这样肯定会导致有些时候,cnt!=n 我的剪枝方法(2546MS AC): 搜下一个结点之前,确保时间小于所有的未访问的结点的

HDU 2852 (树状数组+无序第K小)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小数. 解题思路: 由于模型是盒子,而不是序列,所以可以用树状数组的顺序维护+逆序数思想. 对应的树状数组Solution: 放一个数 $Add(val,1)$ 类似维护逆序数的方法,对应位置上计数+1. 删一个数 判断:$getSum(val)-getSum(val-1)=0$ 可以Hash处理,但