版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pengwill97/article/details/81905937
题意
对于n个数字,给出sum[j]-sumi的符号(正负零),求一组n个数的的可行解(n个数都在-10—10之间)
题解
初始化为-10,变量取值下界为-10,利用题目中的三角不等式建图,跑最短路即可。
注意计算出来的是前缀和。
代码
#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
const int nmax = 1e6+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const ull p = 67;
const ull MOD = 1610612741;
int t, n;
char op[20][20];
int mp[20][20];
int main(){
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
getchar();
for(int i = 0; i <= n; ++i) {
for(int j = 0; j <= n; ++j) {
mp[i][j] = -10;
}
}
for(int i = 1; i <= n; ++i) {
for(int j = i; j <= n; ++j) {
scanf("%c", &op[i][j]);
if(op[i][j] == '0') {
mp[i-1][j] = 0;
} else if(op[i][j] == '-') {
mp[j][i-1] = 1;
} else if(op[i][j] == '+') {
mp[i-1][j] = 1;
}
}
}
for(int j = 0 ; j <= n; ++j) {
for(int i = 0; i <= n; ++i) {
for(int k = 0; k <=n; ++k) {
mp[i][k] = max(mp[i][k], mp[i][j] + mp[j][k]);
}
}
}
for(int i = 1; i <= n; ++i) {
printf("%d ", mp[0][i] - mp[0][i-1]);
}
printf("\n");
}
return 0;
}