QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#55798 | #1972. JJ Rally | Sa3tElSefr | TL | 8ms | 52776kb | C++20 | 4.2kb | 2022-10-15 09:16:21 | 2022-10-15 09:16:23 |
Judging History
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> Masks[30], Bits[M + 5], BitsOff[M + 5];
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] = 0;
for(int bit = 0; bit < (n - 4); bit++) {
if(i >> bit & 1) {
cnt[i]++;
Bits[i].push_back(bit);
}
else BitsOff[i].push_back(bit);
}
// cnt[i] = __builtin_popcount(i);
Masks[cnt[i]].push_back(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;
int lastNode, curNode;
for(int i = 0; i < (n - 2); i++) {
lastNode = mp[i];
dp[0][i][0] = (Edge[lastNode][t1] == dis[lastNode][t1]);
dp[0][i][1] = (Edge[lastNode][t2] == dis[lastNode][t2]);
}
// cout << dp[0][0][0] << ' ' << dp[0][1][1] << '\n';
for(int kam = 1; kam < (n - 4); kam++) {
for(int mask: Masks[kam]) {
for(int bit: Bits[mask]) {
curNode = mp[bit];
for(int last: BitsOff[mask]) {
lastNode = mp[last];
if(dis[lastNode][t1] == Edge[lastNode][curNode] + dis[curNode][t1]) {
dp[mask][last][0] += dp[mask ^ (1 << bit)][bit][0];
}
if(dis[lastNode][t2] == Edge[lastNode][curNode] + dis[curNode][t2]) {
dp[mask][last][1] += dp[mask ^ (1 << bit)][bit][1];
}
}
if(dis[s1][t1] == Edge[s1][curNode] + dis[curNode][t1]) {
dp[mask][n - 4][0] += dp[mask ^ (1 << bit)][bit][0];
}
if(dis[s2][t2] == Edge[s2][curNode] + dis[curNode][t2]) {
dp[mask][n - 3][1] += dp[mask ^ (1 << bit)][bit][1];
}
}
}
}
// 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 mask = 0; mask < (1 << (n - 4)); mask++) {
f[mask] = dp[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)];
}
}
}
ll ans = 0;
for(int mask = 0; mask < (1 << (n - 4)); mask++) {
int compMask = (1 << (n - 4)) - 1 - mask;
ans += dp[mask][n - 3][1] * f[compMask];
}
cout << ans;
return 0;
}
/*
for(int kam = 1; kam < (n - 4); kam++) {
for(auto mask: Masks[kam]) {
for(auto bit: Bits[mask]) {
}
}
}
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 52776kb
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: 8ms
memory: 52776kb
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: 0
Accepted
time: 4ms
memory: 52632kb
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:
3
result:
ok single line: '3'
Test #4:
score: -100
Time Limit Exceeded
input:
24 276 1 2 117 1 3 36 1 4 247 1 5 150 1 6 215 1 7 232 1 8 161 1 9 209 1 10 190 1 11 4 1 12 102 1 13 281 1 14 301 1 15 32 1 16 138 1 17 114 1 18 304 1 19 141 1 20 105 1 21 64 1 22 75 1 23 23 1 24 237 2 3 153 2 4 364 2 5 267 2 6 332 2 7 349 2 8 278 2 9 326 2 10 307 2 11 113 2 12 219 2 13 398 2 14 418 ...