40372023-03-09 14:01:19gortomiBarátnők (50 pont)cpp17Accepted 50/50136ms26260 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
const int MOD = 1e9 + 7;
vector<vector<pii > > g;
void dfs(int n, vector<int> & w, vector<int> & d, int go)
{
    if(w[n] != -1) return;
    w[n] = 0;
    if(go == n)
    {
        w[n] = 1;
        return;
    }
    for(auto x : g[n])
    {
        if(x.second + d[n] == d[x.first])
        {
            dfs(x.first, w, d, go);
            w[n] += w[x.first];
            w[n] %= MOD;
        }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<int> a(m), b(m), c(m);
    vector<int> w1(n + 1, -1), w2(n + 1, -1);
    g.resize(n + 1);
    for(int i = 0; i < m; i++)
    {
        int x, y, z;
        cin >> x >> y >> z;
        a[i] = x;
        b[i] = y;
        c[i] = z;
        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }
    int s, f;
    cin >> s >> f;
    priority_queue<pii, vector<pii >, greater<pii > > pq;
    vector<int> d1(n + 1, 1e15), d2(n + 1, 1e15);
    d1[s] = 0;
    pq.push({0, s});
    while(!pq.empty())
    {
        int v = pq.top().second, d_v = pq.top().first;
        pq.pop();
        if(d_v != d1[v]) continue;
        for(auto x : g[v])
        {
            int to = x.first, len = x.second;
            if(d1[to] > d1[v] + len)
            {
                d1[to] = d1[v] + len;
                pq.push({d1[to], to});
            }
        }
    }
    d2[f] = 0;
    pq.push({0, f});
    while(!pq.empty())
    {
        int v = pq.top().second, d_v = pq.top().first;
        pq.pop();
        if(d_v != d2[v]) continue;
        for(auto x : g[v])
        {
            int to = x.first, len = x.second;
            if(d2[to] > d2[v] + len)
            {
                d2[to] = d2[v] + len;
                pq.push({d2[to], to});
            }
        }
    }
    dfs(s, w2, d1, f);
    dfs(f, w1, d2, s);
    for(int i = 0; i <= n; i++)
    {
        if(w1[i] == -1) w1[i] = 0;
        if(w2[i] == -1) w2[i] = 0;
    }
    int ans = w2[s] * w2[s];
    for(int i = 0; i < m; i++)
    {
        int x = a[i], y = b[i], z = c[i];
        if(d1[x] > d1[y]) swap(x, y);
        if(d1[x] + z != d1[y]) continue;
        if(d1[x] < d2[x] && d1[y] > d2[y])
        {
            int d = w1[x] * w2[y];
            d %= MOD;
            d *= d;
            d %= MOD;
            ans -= d;
            ans %= MOD;
        }
    }
    for(int i = 1; i <= n; i++)
    {
        if(d1[i] == d2[i])
        {
            int d = w1[i] * w2[i];
            d %= MOD;
            d *= d;
            d %= MOD;
            ans -= d;
            ans %= MOD;
        }
    }
    ans += MOD;
    ans %= MOD;
    cout << ans;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1828 KiB
2Accepted0/0136ms23288 KiB
3Accepted2/24ms2656 KiB
4Accepted2/23ms2712 KiB
5Accepted2/23ms2824 KiB
6Accepted2/23ms3048 KiB
7Accepted2/24ms3208 KiB
8Accepted1/148ms15732 KiB
9Accepted2/275ms24820 KiB
10Accepted2/297ms23396 KiB
11Accepted2/296ms24128 KiB
12Accepted2/287ms24556 KiB
13Accepted2/2103ms23912 KiB
14Accepted2/298ms24964 KiB
15Accepted2/297ms25492 KiB
16Accepted1/1108ms24704 KiB
17Accepted1/1114ms25296 KiB
18Accepted1/154ms16972 KiB
19Accepted2/2112ms25544 KiB
20Accepted2/2112ms24224 KiB
21Accepted2/2116ms24940 KiB
22Accepted2/298ms24684 KiB
23Accepted2/2112ms25920 KiB
24Accepted2/283ms24632 KiB
25Accepted2/278ms23088 KiB
26Accepted2/2103ms25148 KiB
27Accepted2/2108ms25520 KiB
28Accepted2/2112ms26260 KiB
29Accepted2/2103ms24844 KiB