题意就是让你算找一个得分最大的子串
代码:
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 1000005
#define MAXN 1000005
#define maxnode 205
#define sigma_size 26
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-4;
const LL mod = 772002;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
const int N = 11111;
const int M = 100005;
struct Isap{
int tot;
int d[N],pre[N],cur[N],gap[N];
int head[N];
struct Edge{
int v,next;
int cap,flow;
}edge[M*2];
void init(){
mem(head,-1);
tot=0;
}
void add_edge(int a, int b, int c){
edge[tot]=(Edge){b,head[a],c,0};
head[a]=tot++;
}
void add(int a,int b, int c){
add_edge(a,b,c);
add_edge(b,a,0);
}
void set_d(int t){
queue<int> q;
mem(d,-1);
mem(gap,0);
d[t]=0;
q.push(t);
while(!q.empty()) {
int u=q.front();q.pop();
++gap[d[u]];
for(int i=head[u];i!=-1;i=edge[i].next) {
int v=edge[i].v;
if(d[v]==-1) {
d[v]=d[u]+1;
q.push(v);
}
}
}
}
int sap(int s,int t,int num) {
set_d(t);
int ans=0,u=s;
int flow=INF;
memcpy(cur,head,sizeof(head));
while(d[s]<num){
int &i=cur[u];
for(;i!=-1;i=edge[i].next) {
int v=edge[i].v;
if(edge[i].cap>edge[i].flow&&d[u]==d[v]+1) {
u=v;
pre[v]=i;
flow=min(flow,edge[i].cap-edge[i].flow);
if(u==t){
while(u!=s){
int j=pre[u];
edge[j].flow+=flow;
edge[j^1].flow-=flow;
u=edge[j^1].v;
}
ans+=flow;
flow=INF;
}
break;
}
}
if(i==-1) {
if(--gap[d[u]]==0) break;
int dmin=num-1;
cur[u]=head[u];
for(int j=head[u];j!=-1;j=edge[j].next)
if(edge[j].cap>edge[j].flow)
dmin=min(dmin,d[edge[j].v]);
d[u]=dmin+1;
++gap[d[u]];
if(u!=s) u=edge[pre[u]^1].v;
}
}
return ans;
}
}Sap;
/*调用方式:
Sap.init(); //建边前调用
Sap.add(u, v, c); //在u->v之间建一条容量为c的边
Sap.sap(s, t, n); //s为源点,t为汇点,n为点的数量*/
char s[105];
int a[15],b[15];
int main(){
//freopen("froggy.in","r",stdin);
//freopen("froggy.out","w",stdout);
int t,kase=0;
cin>>t;
while(t--){
int n;
kase++;
scanf("%d%s",&n,s+1);
Sap.init();
for(int i=0;i<10;i++) scanf("%d%d",&a[i],&b[i]);
for(int i=0;i<10;i++) Sap.add(n*n+n+i+1,n*n+n+11,b[i]-a[i]);
for(int i=1;i<=n;i++){
Sap.add(n*n+i,n*n+n+11,a[s[i]-'0']);
Sap.add(n*n+i,n*n+n+(s[i]-'0')+1,INF);
}
int sum=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
int x;
scanf("%d",&x);
Sap.add(0,(i-1)*n+j,x);
sum+=x;
//if(i==j) continue;
Sap.add((i-1)*n+j,n*n+i,INF);
Sap.add((i-1)*n+j,n*n+j,INF);
}
}
printf("Case #%d: %d\n",kase,sum-Sap.sap(0,n*n+n+11,n*n+n+12));
}
return 0;
}