其他分享
首页 > 其他分享> > HDU - 6026 D - Deleting Edges

HDU - 6026 D - Deleting Edges

作者:互联网

最短路计数

https://vjudge.net/contest/464901#problem/D

题意是给一个图,问最后这个图使得每个点到 \(1\) 点距离都是原图中最短的生成树的个数。

虽然说很明显只要求最短路个数就行了,但是我也不懂为啥最短路计数是这样记的。
题解都说是最短路计数的模板...话说咱也不知道为啥啊。答案是将每个点的贡献乘起来。
需要再理解下。。

点击查看代码
#include <algorithm>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
#define P pair<int, int>
const int N = 50 + 5;
const ll INF = 1e18;
const int mod = 1e9 + 7;
int n;
ll mp[N][N], dis[N], vis[N];
ll cnt[N];
void dij() {
    priority_queue<P, vector<P>, greater<P> > q;
    memset(dis, 0x3f, sizeof dis);
    memset(vis, 0, sizeof vis);
    memset(cnt, 0, sizeof cnt);

    q.push(P{0, 1});
    dis[1] = 0;
    while (q.size()) {
        P now = q.top();
        q.pop();
        int o = now.second, d = now.first;
        if (vis[o]) continue;
        vis[o] = 1;
        for (int i = 1; i <= n; i++) {
            if (i == o || mp[o][i] == 0) continue;
            if (dis[i] > dis[o] + mp[o][i]) {
                dis[i] = dis[o] + mp[o][i];
                q.push(P{dis[i], i});
                cnt[i] = 1;
            } else if (dis[i] == dis[o] + mp[o][i]) {
                cnt[i]++;
            }
        }
    }
}
string str;
int main() {
    while (cin >> n) {
        for (int i = 1; i <= n; i++) {
            cin >> str;
            for (int j = 1; j <= n; j++) {
                mp[i][j] = str[j-1] - '0';
                if (mp[i][j] == 0 && i != j) mp[i][j] = INF;
            }
        }
        // cout << "---\n";

        dij();


        ll ans = 1;
        for (int i = 2; i <= n; i++) {
            if (dis[i] == INF) ans = 0;
            ans *= cnt[i];
            ans %= mod;
        }

        cout << ans << endl;
    }
}

标签:HDU,int,ll,cnt,vis,Edges,include,Deleting,dis
来源: https://www.cnblogs.com/FushimiYuki/p/15484374.html