Deleting Edges HDU - 6026 (最短路)

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) 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(1n50) 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 jth j − t h number of the ith i − t h line is c(0c9) 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到各点的最短路,然后枚举点,查看他的的临点,有哪些点到加上到这点的权值的和等于他的最短路。然后记录这些点的数量。最后每个点的数量相乘就是答案。
代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define mod 1000000007
#define INF 0x7f7f7f7f
using namespace std;
int graph[55][55];
int dis[55];
int n;
char inQ[55];
void spfa()
{
    queue<int>que;
    memset(dis,INF,sizeof(dis));
    dis[0]=0;
    que.push(0);
    inQ[0]=true;
    while(que.size())
    {
        int now=que.front();que.pop();
        inQ[now]=false;
        for(int i=0;i<n;i++)
        {
            if(!graph[now][i])
                continue;
            if(dis[i]>dis[now]+graph[now][i])
            {
                dis[i]=dis[now]+graph[now][i];
                if(!inQ[i])
                {
                    que.push(i);
                    inQ[i]=true;
                }
            }
        }
    }
}
int main()
{
    while(scanf("%d",&n)==1)
    {
        char s[55];
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            for(int j=0;j<n;j++)
                graph[i][j]=s[j]-'0';
        }

        spfa();
        long long ans=1;
        for(int i=1;i<n;i++)
        {
            long long countt=0;
            for(int j=0;j<n;j++)
                if(graph[i][j])
                {
                    if(dis[i]==dis[j]+graph[i][j])
                        countt++;
                }
            ans=ans*countt%mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值