QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#55772#1972. JJ Rallytriplem5dsWA 56ms208428kbC++233.6kb2022-10-15 01:29:592022-10-15 01:30:02

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-15 01:30:02]
  • 评测
  • 测评结果:WA
  • 用时:56ms
  • 内存:208428kb
  • [2022-10-15 01:29:59]
  • 提交

answer

#include <bits/stdc++.h>

#define ll long long
#define ld  double
using namespace std;

const int N = 500 + 5, M = (1 << 20), mod = 998244353;


int dp[M + 5][25][2];
ll f[M + 5];


int n, mp[30], s1, s2, t1, t2, cnt[M + 5], dis[30][30], Edge[30][30];

vector<int> go[30][2];
int solve(int mask, int last, bool t) {
    int lastNode = mp[last], target = t1;
    if(t == 1) target = t2;
    if(cnt[mask] == 0) {
        if(dis[lastNode][target] == Edge[lastNode][target]) return 1;
        return 0;
    }
    if(dp[mask][last][t] != -1) return dp[mask][last][t];

    int ans = 0;

    for(int bit : go[last][t]) {
        if(mask >> bit & 1) {
            ans += solve(mask ^ (1 << bit), bit, t);
        }
    }
    return dp[mask][last][t] = ans;
}





int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int m;
    cin >> n >> m;

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            Edge[i][j] = dis[i][j] = mod;
        }
    }


    for(int i = 0; i < m; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        u--; v--;
        dis[u][v] = dis[v][u] = Edge[u][v] = Edge[v][u] = min(dis[u][v], c);
    }

    cin >> s1 >> t1 >> s2 >> t2;
    s1--; t1--;
    s2--; t2--;

    for(int k = 0; k < n; k++) {
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
            }
        }
    }

    for(int i = 1; i < (1 << (n - 4)); i++) {
        cnt[i] = __builtin_popcount(i);
    }

    // cout << dis[0][1] << ' ' << dis[2][3] << '\n';
    int curId = 0;
    for(int i = 0; i < n; i++) {
        if(i == s1 || i == s2 || i == t1 || i == t2) continue;
        mp[curId] = i;
        curId++;
    }
    mp[n - 4] = s1;
    mp[n - 3] = s2;
    mp[n - 2] = t1;
    mp[n - 1] = t2;

    memset(dp, -1, sizeof dp);

    for(int mask = 0; mask < (1 << (n - 4)); mask++) {
        bool found = 0;
        for(int bit = 0; bit < (n - 4); bit++) {
            if(mask >> bit & 1) {
                int curNode = mp[bit];
                if(dis[s1][t1] != dis[s1][curNode] + dis[curNode][t1]) {
                    found = 1;
                    break;
                }
            }
        }
        if(found) continue;
        f[mask] = solve(mask, n - 4, 0);
    }

    for(int bit = 0; bit < (n - 4); bit++) {
        for(int mask = 0; mask < (1 << (n - 4)); mask++) {
            if(mask >> bit & 1) {
                f[mask] += f[mask ^ (1 << bit)];
            }
        }
    }

    for(int i = 0; i < (n - 4); i++) {
        for(int j = 0; j < 2; j++) {
            for(int bit = 0; bit < (n - 4); bit++) {
                int curNode = mp[bit];
                int lastNode = mp[i], target = (j ? t2 : t1);
                if(dis[lastNode][target] == Edge[lastNode][curNode] + dis[curNode][target]) {
                    go[i][j].push_back(j);
                }
            }
        }
    }

    ll ans = 0;
    for(int mask = 0; mask < (1 << (n - 4)); mask++) {
        int compMask = (1 << (n - 4)) - 1 - mask;
        if(f[mask] == 0) continue;
        bool found = 0;
        for(int bit = 0; bit < (n - 4); bit++) {
            if(mask >> bit & 1) {
                int curNode = mp[bit];
                if(dis[s2][t2] != dis[s2][curNode] + dis[curNode][t2]) {
                    found = 1;
                    break;
                }
            }
        }
        if(found) continue;
        ans += 1ll * solve(mask, n - 3, 1) * f[compMask];
    }
    cout << ans;






    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 44ms
memory: 208408kb

input:

4 4
1 2 2
2 3 1
1 3 1
3 4 1
1 2 3 4

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 56ms
memory: 208352kb

input:

4 3
1 2 1
2 3 1
3 4 1
1 3 2 4

output:

0

result:

ok single line: '0'

Test #3:

score: -100
Wrong Answer
time: 24ms
memory: 208428kb

input:

6 8
1 4 1
1 3 1
4 2 1
3 2 1
1 2 2
1 5 1
5 2 1
5 6 2
1 2 6 5

output:

1

result:

wrong answer 1st lines differ - expected: '3', found: '1'