qscqesze
Published on 2017-09-02 11:31 in 暂未分类 with qscqesze

# String problem

## 题目连接：

http://acm.hdu.edu.cn/showproblem.php?pid=5772

## Description

This is a simple problem about string. Now a string S contains only ‘0’-‘9’. ?? wants to select a subsequence from this string. And makes this subsequence score maximum. The subsequence’s score is calculated as follows:
Score= Value – Total_Cost
The calculation of the Cost is as follows:
If the number of characters x in the subsequence is kx, And the two coefficients are ax,bx,The cost of character x calculated as follows:

{cost[x]=0,kx=0cost[x]=ax∗(kx−1)+bx,kx≠0

TotalCost=∑i=09cost[i]

The calculation of the Value is as follows:

Value=0;
for(int i=1;i<=length(substr);++i){
for(int j=1;j<=length(substr);++j){
if(i!=j)
Value+=w[id[i]][id[j]];
}
}

id[i] is the position of the subsequence’s ith character in the original string,for example,if the original string is “13579”,and the subsubquence is “159”,then the array id ={1,3,5}. The w is a weight matrix.

## Input

The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains one integers n, the length of a string.
Next line contains the string S.
Next ten lines,each line contains ai,bi,denote the char i’s(0-9) coefficients
Next is a n*n matrix w.
Limits:
T<=20,
0<=n<=100
0<=ai<=bi<=1000
0<=w[i][j]<=50

## Output

Each test output one line “Case #x: y” , where x is the case number ,staring from 1. y is the Maximum score.

1
3
135
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
0 0 3
1 0 0
4 0 0

## Sample Output

Case #1: 3

Hint
we can choose “15”，id[]={1,3} then Value=w[1][3]+w[3][1]=7,
Total_Cost=2+2=4，Score=7-4=3

## 代码

#include<bits/stdc++.h>
#define Maxn 60009
#define Maxm 400009
#define inf 100000000
#define LL int
using namespace std;
const int MAXN=100000,MAXM=100000;
struct Edge
{
int v,c,f,nx;
Edge() {}
Edge(int v,int c,int f,int nx):v(v),c(c),f(f),nx(nx) {}
} E[MAXM];
int G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],N,sz;
void init(int _n)
{
N=_n,sz=0; memset(G,-1,sizeof(G[0])*N);
}
{
E[sz]=Edge(v,c,0,G[u]); G[u]=sz++;
E[sz]=Edge(u,0,0,G[v]); G[v]=sz++;
}
int ISAP(int S,int T)
{//S -> T
int maxflow=0,aug=inf,flag=false,u,v;
for (int i=0;i<N;++i)cur[i]=G[i],gap[i]=dis[i]=0;
for (gap[S]=N,u=pre[S]=S;dis[S]<N;flag=false)
{
for (int &it=cur[u];~it;it=E[it].nx)
{
if (E[it].c>E[it].f&&dis[u]==dis[v=E[it].v]+1)
{
if (aug>E[it].c-E[it].f) aug=E[it].c-E[it].f;
pre[v]=u,u=v; flag=true;
if (u==T)
{
for (maxflow+=aug;u!=S;)
{
E[cur[u=pre[u]]].f+=aug;
E[cur[u]^1].f-=aug;
}
aug=inf;
}
break;
}
}
if (flag) continue;
int mx=N;
for (int it=G[u];~it;it=E[it].nx)
{
if (E[it].c>E[it].f&&dis[E[it].v]<mx)
{
mx=dis[E[it].v]; cur[u]=it;
}
}
if ((--gap[dis[u]])==0) break;
++gap[dis[u]=mx+1]; u=pre[u];
}
return maxflow;
}
bool bfs(int S,int T)
{
static int Q[MAXN]; memset(dis,-1,sizeof(dis[0])*N);
dis[S]=0; Q[0]=S;
for (int h=0,t=1,u,v,it;h<t;++h)
{
for (u=Q[h],it=G[u];~it;it=E[it].nx)
{
if (dis[v=E[it].v]==-1&&E[it].c>E[it].f)
{
dis[v]=dis[u]+1; Q[t++]=v;
}
}
}
return dis[T]!=-1;
}
int dfs(int u,int T,int low)
{
if (u==T) return low;
int ret=0,tmp,v;
for (int &it=cur[u];~it&&ret<low;it=E[it].nx)
{
if (dis[v=E[it].v]==dis[u]+1&&E[it].c>E[it].f)
{
if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f)))
{
ret+=tmp; E[it].f+=tmp; E[it^1].f-=tmp;
}
}
}
if (!ret) dis[u]=-1; return ret;
}
int dinic(int S,int T)
{
int maxflow=0,tmp;
while (bfs(S,T))
{
memcpy(cur,G,sizeof(G[0])*N);
while (tmp=dfs(S,T,inf)) maxflow+=tmp;
}
return maxflow;
}

struct st
{
int u,v;
LL value;
}e[Maxm];
int cost[MAXN];
int mp[105][105];
int a[10],b[10],nn;
int Ans = 0;
string s;
LL get(int zt){
vector<int> tmp;
LL sum=0;
int cccc = 0;
for(int i=0;i<10;i++){
if((1<<i)&zt){
sum-=(b[i]-a[i]);
}
}
for(int i=0;i<s.size();i++){
int num = s[i]-'0';
if((1<<num)&zt){
tmp.push_back(i+1);
cost[i+1]=a[s[i]-'0'];
//   cout<<i<<" ";
}
}
int m = 1;

for(int i=0;i<tmp.size();i++){
for(int j=i+1;j<tmp.size();j++){
e[m].u=tmp[i];
e[m].v=tmp[j];
e[m].value=mp[tmp[i]][tmp[j]]+mp[tmp[j]][tmp[i]];
sum+=e[m].value;
m++;
}
}
if(sum<=Ans)return 0;
m--;
init(12000);
int n = tmp.size();
for(int i=1;i<=m;i++)
{
}
for(int i=1;i<=n;i++)
LL ans=ISAP(0,m+nn+1);
return sum-ans;
}
int cas = 0;
int vvv[15];
int ccc = 0;
int times = 0 ;

void dfs1(int x,int tmp,int k){
if(x==10){

if( (k>=ccc-7||ccc<=2) )Ans=max(Ans,get(tmp));
return;
}
if(vvv[x])dfs1(x+1,tmp|(1<<x),k+1);
dfs1(x+1,tmp,k);
}

void solve(){
memset(vvv,0,sizeof(vvv));
Ans=0;
times=0;
scanf("%d",&nn);
cin>>s;
ccc=0;
for(int i=0;i<s.size();i++){
if(vvv[s[i]-'0']==0)ccc++;
vvv[s[i]-'0']=1;
}
for(int i=0;i<10;i++){
scanf("%d%d",&a[i],&b[i]);
}
for(int i=1;i<=nn;i++){
for(int j=1;j<=nn;j++){
scanf("%d",&mp[i][j]);
}
}

dfs1(0,0,0);

cout<<"Case #"<<++cas<<": "<<Ans<<endl;
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}
posted @ 2016-07-28 19:05  qscqesze  阅读(625)  评论(0编辑  收藏