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 ;
}

HDU5772 String problem的更多相关文章

  1. HDU5772 String problem(最大权闭合子图)

    题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...

  2. HDU5772 String problem 最大权闭合图+巧妙建图

    题意:自己看吧(不是很好说) 分析: 网络流:最大权闭合子图. 思路如下: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得 ...

  3. 【HDU5772】String Problem [网络流]

    String Problem Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Ou ...

  4. hdu String Problem(最小表示法入门题)

    hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...

  5. HDU 3374 String Problem(KMP+最大/最小表示)

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. 【HDU3374】 String Problem (最小最大表示法+KMP)

    String Problem Description Give you a string with length N, you can generate N strings by left shift ...

  7. HDOJ3374 String Problem 【KMP】+【最小表示法】

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  9. String Problem hdu 3374 最小表示法加KMP的next数组

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. MyEclipse - 问题集 - Java compiler level does not match the version of the installed Java project facet

    右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面. 在页面中的“Java”下拉列表中,选择相应 ...

  2. python基础训练营01

    一.基础讲解: 1.1 文件末尾的.py后缀,指出这个文件,是一个python文件,因此,系统将使用python解释器来运行该文件,确定文件中每一个单词的含义. 1.2 python编辑/运行方法: ...

  3. 一个简单的ipfs音乐播放器的实现

    IPFS音乐播放器 IPFS相关 IPFS第一次亲密接触 什么是IPFS IPFS对比HTTP/FTP等协议的优势 IPFS应用场景 -移动数据 交易 路由 网络 定义数据 命名 使用数据 具体场景; ...

  4. 数据结构6——DFS

    一.相关定义 深度优先遍历,也有称为深度优先搜索,简称DFS.其实,就像是一棵树的前序遍历. 初始条件:图G所有顶点均未被访问过,任选一点v. 思想:是从一个顶点V1开始,沿着一条路一直走到底,如果发 ...

  5. java连接mysql底层封装

    package com.dao.db; import java.sql.Connection; import java.sql.SQLException; /** * 数据库连接层MYSQL * @a ...

  6. ActiveMQ入门代码

    Hello world程序演示: 生产者: package com.mq.helloworld; import javax.jms.Connection; import javax.jms.Conne ...

  7. 建议 里面的sql查找单列 外面的sql查找所有列 这样方便查找数据

  8. [CF1031E]Triple Flips

    题目大意:给你一个长度为$n$的$01$串,一次操作定义为:选取$3$个等距的元素,使其$0$变$1$,$1$变$0$,要求在$\Big\lfloor \dfrac n 3\Big\rfloor+12 ...

  9. [ZJOI2005]沼泽鳄鱼 矩阵乘法

    ---题面--- 题解: 乍一看还是挺懵逼的.和HH去散步很像,思路也是类似的. 复制一段我在HH去散步的题解里面写的一段话吧: 考虑f[i][j]表示i和j是否右边相连,有为1,否则为0,那么f同时 ...

  10. Jlink下载u-boot

    由于各种原因我的fl2440无启动代码,无任何程序,这时要通过jlink来烧录相关boot程序. 准备工作: 1.Jlink使用jlink commander工具 2.初始化sdram的程序boot. ...