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.

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

Hint

题意

给你一个字符串,只含有数字。

你需要选择出一个子序列,使得这个子序列的权值最大。

这个子序列如果这个数字第一次出现就ans-=bx,否则就-=ax

然后如果第i个字符和第j个字符都在子序列里面,那么ans+=w[i][j]

问你最大ans是多少

题解:

比较显然的就是最大闭合子图了,这个跑网络流就是了。

标准题解:

网络流:最大权闭合子图。

思路如下:

首先将点分为3类

第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分)

第二类:原串中的n个点每个点拆出一个点,第i个点权值为 –a[s[i]] (表示要花费)

第三类:对于10种字符拆出10个点,每个点的权值为 -(b[x]-a[x])

那么我们可以得到一个关系图 ,对于第一类中的点Pij,如果想要选择Pij,你就必须要选中第二类中的点i和j,对于第二类中的点如果你想选中第i个点,其对应的字符s[i],那么就必须选中第三类中s[i] 对应的点,因为每个种类的点第一次选中时花费是b[s[i]],而第二类中花费都是a[s[i]],一定要补上b[s[i]]-a[s[i]],而且只需要补上一次。

得到上面的关系图后然后就是普通的最大权闭合子图问题,直接求解即可。

但是我们队比较邪,我们先2^10枚举了一下,然后再跑的网络流,但是这样会TLE。

于是我们就XJB只搜了后面的几个状态和前面的状态,然后跑网络流就过了……

下面代码是我们队的谐星做法,非正解,正解按照前面的思路建图就好了。

另外这个图跑ISAP比跑dinic快的多。。。。

代码

#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);
}
void link(int u,int v,int c)
{
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++)
{
link(0,i,e[i].value);
link(i,m+e[i].u,inf);
link(i,m+e[i].v,inf);
}
for(int i=1;i<=n;i++)
link(tmp[i-1]+m,m+nn+1,cost[tmp[i-1]]);
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;
}

hdu 5772 String problem 最大权闭合子图的更多相关文章

  1. hdu 3917 Road constructions 最大权闭合子图

    样例说明: n(城市数目)   m(工程队数目) 每个工程队上交的税收 val[i] k(k个工程) xi   yi  ci  costi , 工程队ci承包由xi到yi,政府的补贴为costi 注意 ...

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

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

  3. HDU 5772 String problem

    最大权闭合子图.建图巧妙. 最大权闭合子图: #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...

  4. HDU 3879 Base Station(最大权闭合子图)

    将第i个用户和他需要的基站连边,转化成求二分图的最大权闭合子图. 答案=正权点之和-最小割. # include <cstdio> # include <cstring> # ...

  5. hdu5772-String problem(最大权闭合子图问题)

    解析: 多校标答 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分)第二类:原串中的n个点每个点拆出一个点,第i个点权值为 –a[s[i]] ...

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

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

  7. HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...

  8. HDU 5855 Less Time, More profit 最大权闭合子图

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5855 Less Time, More profit Time Limit: 2000/1000 MS ...

  9. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

随机推荐

  1. 那些年的 网络通信之 UDP 数据报包传输---

    下面是 一个多线程,基于 UDP 用户数据报包 协议 的 控制台聊天小程序 import java.io.*; import java.net.*; class Send implements Run ...

  2. 华中邀请赛现场赛F题 Seats

    题目链接:http://acm.whu.edu.cn/land/problem/detail?problem_id=1552 解题报告:题目意思应该很清楚,就是有n个人,分别属于7个班级,然后他们坐成 ...

  3. 第11月第20天 sqlite3_open xcode mysql connector

    1. sqlite3_open 死锁 * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP frame ...

  4. Ubuntu GNOME单击任务栏图标最小化设置

    在Ubuntu GNOME的发行版中,桌面使用的是GNOME,GNOME可以像Windows那样有一个底部任务栏,在Ubuntu GNOME中它称为 dash to dock,如下图: Windows ...

  5. 转:存储之直连存储Dell Powervault MD 3000

    存储之直连存储DellPowervault MD 3000 存储根据服务器类型可以分为:封闭系统的存储和开放系统的存储 1.封闭系统的存储:封闭系统主要指大型机,AS400等服务器 2.开放系统的存储 ...

  6. docker之安装和管理mongodb

    前言 折腾一些使用docker来配置和管理mongodb和mongodb集群. 安装mongodb 从docker网站拉取mongodb镜像 docker search mongo # 选择一个版本 ...

  7. Database Course Summary 001

    0x01. 基本概念 SQL:Structured English Query Language 1. 数据 Data 数据(Data):描述事物的符号记录:数据内容是事物特性的反应或描述:数据是符号 ...

  8. java iterator(迭代器)

    任何容器类,都必须有某种方式可以插入元素并将它们再次取出,毕竟持有事物是容器最基本的工作,对于List,add()插入fang,get()取出,如果从更高层的角度思考,会发现这里有个确定:要用容器,必 ...

  9. prometheus的平台侧和应用侧结合,实现应用的metrics的抓取

    这个细节,迷惑了我一段时间,前面也写过一篇blog,描述过这个问题. 今天看到一种更好的解决方法. 记录一下. prometheus在k8s集群里,抓取应用的metrics. 是需要平台侧和应用侧相互 ...

  10. Codeforces 466E Information Graph

    Information Graph 把询问离线之后就能随便搞了, 去check一下是不是祖先, 可以用倍增也能用dfs序. #include<bits/stdc++.h> #define ...