Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 614    Accepted Submission(s): 282

Problem 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.
 
Sample Input
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

 
Author
FZU
 
Source
 
Recommend
wange2014
 

给定一个只由0~9数字组成的串,要求从其中选出一个价值最大的子序列

网络流 最大权闭合子图

连边有点复杂,本质上还是个最大权闭合子图。

可以看出主要关系有三种:

1、选点i和j,可以得到的组合权值

2、选点的代价,代价随着相同数字使用次数累计。

3、子序列包含某个数字就需要付出的代价

如果把第2类点的权值都设成a[i],各自容量为1,第3类点的权值设成-(b[i]-a[i]),各自容量为INF,选2就必须选对应的3,那么代价问题就解决了。

割1舍弃收益,割2+3舍弃数字,显然是一个最大权闭合子图问题。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline int min(int a,int b){return a<b?a:b;}
struct edge{
int v,nxt,f;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int w){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].f=w;hd[u]=mct;return;
}
void insert(int u,int v,int w){
add_edge(u,v,w);add_edge(v,u,);
return;
}
int S,T;
int d[mxn];
queue<int>q;
bool BFS(){
memset(d,,sizeof d);
q.push(S);d[S]=;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(!d[v] && e[i].f){
d[v]=d[u]+;
q.push(v);
}
}
}
return d[T];
}
int DFS(int u,int lim){
if(u==T)return lim;
int f=,tmp;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(d[v]==d[u]+ && e[i].f && (tmp=DFS(v,min(lim,e[i].f)))){
e[i].f-=tmp;
e[i^].f+=tmp;
lim-=tmp;
f+=tmp;
if(!lim)return f;
}
}
d[u]=;
return f;
}
int Dinic(){
int res=;
while(BFS()){res+=DFS(S,INF);}
return res;
}
int n,m,ed,cnt;
char s[];
int mp[][];
int a[],b[];
void Build(){
ed=n*(n-)/;cnt=;
S=;T=ed+n++;
int i,j;
for(i=;i<=n;i++){
for(j=i+;j<=n;j++){
++cnt;
insert(S,cnt,mp[i][j]+mp[j][i]);
insert(cnt,ed+i,INF);
insert(cnt,ed+j,INF);
}
}
for(i=;i<=n;i++){
int c=s[i]-''+;
insert(ed+i,T,a[c-]);
insert(ed+i,ed+n+c,INF);
}
for(i=;i<=;i++){
int u=ed+n+i+;
insert(u,T,b[i]-a[i]);
}
return;
}
void init(){
memset(hd,,sizeof hd);mct=;
return;
}
int cas;
int main(){
int i,j,u,v;
cas=read();int cct=;
while(cas--){
int ans=;
init();
n=read();
scanf("%s",s+);
for(i=;i<=;i++){a[i]=read();b[i]=read();}
for(i=;i<=n;i++)
for(j=;j<=n;j++)
mp[i][j]=read(),ans+=mp[i][j];
Build();
ans-=Dinic();
printf("Case #%d: %d\n",++cct,ans);
}
return ;
}

最新文章

  1. 【C#】Json数据 排版算法
  2. esc安装数据库 sqlserver mssql
  3. CentOS 6.6安装LAMP和Subversion服务器
  4. 已收录的帝国cms文章被误删除了怎么办?
  5. POJ 3026 Borg Maze
  6. JavaScript经典面试题系列
  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)
  8. 【socket.io研究】1.官网的一些相关说明,概述
  9. PHP读取Excel文件(PHPExcel)
  10. Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Around
  11. Java和Flex整合报错(三)
  12. Sublime text 3 注册码激活码 版本号3143
  13. java ArrayList去重
  14. Java中equals()和“==”区别
  15. [SQL]注释
  16. discuz密码找回:[1]忘记UCENTER创始人密码
  17. WebBench源码分析
  18. Kattis - wheretolive 【数学--求质心】
  19. js选择文件夹路径
  20. oracle 操作实例(一)----rman 全备恢复

热门文章

  1. SQL On Streaming
  2. 第十六篇 Python之迭代器与生成器
  3. linux学习总结---web前端③
  4. python学习总结----内置函数及数据持久化
  5. 机器视觉必知-GenICam相机通用接口标准
  6. BZOJ 4592 SHOI2015 脑洞治疗仪 线段树
  7. Manacher算法——最长回文子串
  8. JavaWeb笔记(十二)日志
  9. 福大软工1816:Alpha(3/10)
  10. 浅谈c语言和c++中struct的区别