HDU6026 Deleting Edges (最短路树计数)

题目链接

题意:

给定一个完全图,要删去一些边使之成为一棵树(n-1条边),并且每个点到0号点的距离等于原图中到0号点的最短距离

分析:

没有负权边,明显的最短路树计数

代码:

#define inf 0x3f3f3f3f
#define mod 1000000007
#define maxn 55
int w[maxn][maxn], d[maxn], n, v[maxn];
void dijk(int st) {
    memset(d, 0x3f, sizeof d);
    memset(v, 0, sizeof v);
    d[st] = 0;
    for (int i = 0; i < n; i++) {
        int m = inf, tmp = -1;
        for (int j = 0; j < n; j++) {
            if (!v[j] && m > d[j]) {
                m = d[tmp = j];
            }
        }
        if (tmp == -1) {
            break;
        }
        v[tmp] = 1;
        for (int j = 0; j < n; j++) {
            if (!v[j] && d[j] > d[tmp] + w[tmp][j]) {
                d[j] = d[tmp] + w[tmp][j];
            }
        }
    }
}
char a[maxn];
int c[maxn];
int cmp(int i, int j) {
    return d[i] < d[j];
}
int main()
{
    while (~scanf("%d", &n)) {
        for (int i = 0; i < n; i++) {
            scanf("%s", a);
            for (int j = 0; j < n; j++) {
                w[i][j] = a[j] - '0';
                if (w[i][j] == 0) {
                    w[i][j] = inf;
                }
            }
            c[i] = i;
        }
        dijk(0);
        sort(c, c + n, cmp);
        ll ans = 1;
        for (int i = 1; i < n; i++) {
            ll p  = 0;
            for (int j = 0; j < i; j++) {
                if (d[c[i]] == d[c[j]] + w[c[i]][c[j]]) {
                    p++;
                }
            }
            ans = ans * p % mod;
        }
        printf("%lld\n", ans);
    }
}

你可能感兴趣的:(HDU6026 Deleting Edges (最短路树计数))