差分约束[SCOI2008]天平
这个差分约束真的想不到,说白了就是统计C和D
https://www.luogu.com.cn/problem/P2474
哎不说了,建议看看原来的题解
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; int dm[55][55]; int dx[55][55]; int n, a, b; char cn[55]; int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) { scanf("%s", cn + 1); int len = strlen(cn + 1); for (int j = 1; j<=len; j++) { if (i == j || cn[j] == '=') { dx[i][j] = 0; dm[i][j] = 0; } else if (cn[j] == '+') { dx[i][j] = 2; dm[i][j] = 1; } else if (cn[j] == '-') { dx[i][j] = -1; dm[i][j] = -2; } else { dx[i][j] = 2; dm[i][j] = -2; } } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dx[i][j] = min(dx[i][k] + dx[k][j], dx[i][j]); dm[i][j] = max(dm[i][k] + dm[k][j], dm[i][j]); } } } int cnt1 = 0, cnt2=0, cnt3 = 0; for (int i = 1; i <= n; i++) { if (i == a || i == b) continue; for (int j = 1; j< i ; j++) { if (j == a || j == b || i == j) continue; if (dm[a][i] > dx[j][b] || dm[b][i] > dx[j][a]) { cnt1++; } if ((dm[a][i] == dx[a][i] && dm[b][j] == dx[b][j] && dm[a][i] == dx[j][b]) || (dm[b][i] == dx[b][i] && dm[a][j] == dx[a][j] && dm[b][i] == dx[j][a])) { cnt2++; } if (dx[a][i] < dm[j][b] || dx[b][i] < dm[j][a]) { cnt3++; } } } cout << cnt1 << " " << cnt2 << " " << cnt3 << endl; return 0; }
寻找真正的热爱