0
点赞
收藏
分享

微信扫一扫

acwing 349

老罗话编程 2022-02-11 阅读 51
#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int >

using namespace std;

const int N = 1100;
const int mod = (1 << 31) - 1;

int a[N][N];
int n, m;
int d[N];
bool v[N];
int cnt[N];

void read()
{
    cin >> n >> m;
    memset(a, 0x3f, sizeof a);
    for (int i = 1; i <= m; i ++)
    {
        int x, y, z;
        cin >> x >> y >> z;
        a[x][y] = z, a[y][x] = z;
    }
    for (int i = 1; i <= n; i ++) a[i][i] = 0;
}

void spfa()
{
    memset(d, 0x3f, sizeof d);
    memset(v, 0, sizeof v);
    d[1] = 0, v[1] = 1;
    queue<int> q;
    q.push(1);
    while (q.size())
    {
        int x = q.front(); q.pop();
        v[x] = false;
        for (int i = 1; i <= n; i ++)
        {
            if (a[x][i] >= 0x3f3f3f3f) continue;
            int y = i, z = a[x][i];
            if (d[y] > d[x] + z)
            {
                d[y] = d[x] + z;
                if (!v[y]) q.push(y), v[y] = 1;
            }
        }
    }
}

void solve()
{
    vector<PII> cur;
    for (int i = 2; i <= n; i ++) cur.push_back({d[i], i});
    sort(cur.begin(), cur.end());
    memset(v, false, sizeof v);
    memset(cnt, 0, sizeof cnt);
    v[1] = true;
    cnt[1] = 1;
    for (auto k : cur)
    {
        int x = k.second;
        for (int i = 1; i <= n; i ++) if (d[x] == d[i] + a[i][x] && v[i]) cnt[x] ++;
        v[x] = 1;
    }
    int ans = 1;
    for (int i = 1; i <= n; i ++) ans = (ans * cnt[i]) % mod;
    cout << ans << endl;
    //for (int i = 1; i <= n; i ++) cout << d[i] << " " << cnt[i] << endl;
}

signed main()
{
    read();
    spfa();
    solve();
    return 0;
}
举报

相关推荐

jsp代码实例第349课

acwing 122

传送AcWing

acwing 120

acwing 341

0 条评论