40372023-03-09 14:01:19gortomiBarátnők (50 pont)cpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1828 KiB
2Elfogadva0/0136ms23288 KiB
3Elfogadva2/24ms2656 KiB
4Elfogadva2/23ms2712 KiB
5Elfogadva2/23ms2824 KiB
6Elfogadva2/23ms3048 KiB
7Elfogadva2/24ms3208 KiB
8Elfogadva1/148ms15732 KiB
9Elfogadva2/275ms24820 KiB
10Elfogadva2/297ms23396 KiB
11Elfogadva2/296ms24128 KiB
12Elfogadva2/287ms24556 KiB
13Elfogadva2/2103ms23912 KiB
14Elfogadva2/298ms24964 KiB
15Elfogadva2/297ms25492 KiB
16Elfogadva1/1108ms24704 KiB
17Elfogadva1/1114ms25296 KiB
18Elfogadva1/154ms16972 KiB
19Elfogadva2/2112ms25544 KiB
20Elfogadva2/2112ms24224 KiB
21Elfogadva2/2116ms24940 KiB
22Elfogadva2/298ms24684 KiB
23Elfogadva2/2112ms25920 KiB
24Elfogadva2/283ms24632 KiB
25Elfogadva2/278ms23088 KiB
26Elfogadva2/2103ms25148 KiB
27Elfogadva2/2108ms25520 KiB
28Elfogadva2/2112ms26260 KiB
29Elfogadva2/2103ms24844 KiB