HDU5772 String problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 614 Accepted Submission(s): 282
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:
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.
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
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
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
给定一个只由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的更多相关文章
- HDU5772 String problem(最大权闭合子图)
题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...
- HDU5772 String problem 最大权闭合图+巧妙建图
题意:自己看吧(不是很好说) 分析: 网络流:最大权闭合子图. 思路如下: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得 ...
- 【HDU5772】String Problem [网络流]
String Problem Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Ou ...
- hdu String Problem(最小表示法入门题)
hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...
- HDU 3374 String Problem(KMP+最大/最小表示)
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 【HDU3374】 String Problem (最小最大表示法+KMP)
String Problem Description Give you a string with length N, you can generate N strings by left shift ...
- HDOJ3374 String Problem 【KMP】+【最小表示法】
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- String Problem hdu 3374 最小表示法加KMP的next数组
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- LARK BOARD开发板入门学习-第2篇
1. 本次主要研究下HDMI接口,使用芯片是CH7033,这个芯片可以接VGA和HDMI两种接口,和FPGA的接口是地址数据总线 2. 值得注意的地方,下图的D1,双二极管BAT54S在电路中一般用于 ...
- HBase全网最佳学习资料汇总
HBase全网最佳学习资料汇总 摘要: HBase这几年在国内使用的越来越广泛,在一定规模的企业中几乎是必备存储引擎,互联网企业阿里巴巴.百度.腾讯.京东.小米都有数千台的HBase集群,中国电信的话 ...
- 第三十四篇 Python面向对象之 反射(自省)
什么是反射? 反射的概念是由Smith在1982年提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序语 ...
- Navicat oracle to postgresql ERR
可能的解决思路是设好源数据和目标数据库后,先建立表结构,然后修改表结构字段数据类型 参考 https://www.cnblogs.com/stephen-liu74/archive/2012/04/3 ...
- python------- IO 模型
IO模型介绍 ...
- Tensorflow Serving介绍及部署安装
TensorFlow Serving 是一个用于机器学习模型 serving 的高性能开源库.它可以将训练好的机器学习模型部署到线上,使用 gRPC 作为接口接受外部调用.更加让人眼前一亮的是,它支持 ...
- LeetCode 622——设计循环队列
1. 题目 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列 ...
- k8s第一个实例创建redis集群服务
1.创建redis-master-controller.yaml apiVersion: v1 kind: ReplicationController metadata: name: redis-ma ...
- xml解析标签
//获取两个标签之间的值 private static string GetStr(string message, string strStart, string strEnd) { ; ; star ...
- DES(Data Encryption Standard)数据加密标准
DES算法入口参数 DES算法的入口参数有三个:Key.Data.Mode.其中Key为7个字节共56位,是DES算法的工作密钥.Data为8个字节64位,是要被加密或解密的数据;Mode为DES的工 ...