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 ...
随机推荐
- (原)一段看似美丽的for循环,背后又隐藏着什么
之前很长一段时间,潜心修炼汇编,专门装了一个dos7,慢慢玩到win32汇编,再到linux的AT&A汇编,尝试写mbr的时候期间好几次把centos弄的开不了机,又莫名其妙的修好了,如今最大 ...
- 虚拟现实-VR-UE4-认识UE4
VR的火热,让每个人都想参与一下, 公司在展会上面搞了一个VR的Demo,关注度超出预期,使得公司高层决定来个VR项目 所以 关于UE4 百度百科地址:http://baike.baidu.com/l ...
- 第十篇 Python的字符串格式化
字符串格式化:就是按照你的意愿做一个拼接的过程. 1. 字符串格式化的第一种方式:百分号方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. %[ ...
- xamdin: 添加小组件报错: render() got an unexpected keyword argument 'renderer'
查找到 xadmin里面的 dashboard.py文件内render方法,增加一个rdnderer默认参数是None一般路径在 本机虚拟环境\Lib\site-packages\xadmin\vie ...
- Hadoop2.5.2集群部署(完全分布式)
环境介绍 硬件环境 CPU 4 MEM 4G 磁盘 60G 软件环境 OS:centos6.5版本 64位 Hadoop:hadoop2.5.2 64位 JDK: JDK 1.8.0_91 主机配置 ...
- lintcode-83-落单的数 II
83-落单的数 II 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字. 样例 给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4 挑战 一次遍历,常数级 ...
- 【EasyNetQ】- 基于topic的路由
RabbitMQ具有非常酷的功能,基于主题的路由,允许订户根据多个标准过滤消息.主题是由与消息一起发布的点分隔的单词列表.例如,“stock.usd.nyse”或“book.uk.london”或“a ...
- 【Python】PYTHON九九乘法表
python2.7 for i in range(1,10): for j in range(1,i+1): print j,'x',i,'=',j*i,'\t', print '\n'pr ...
- 服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana
服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana https://www.cnblogs.com/xishuai/p/elk- ...
- CSS继承—深入剖析
CSS的继承是指被包在内部的标签将拥有外部标签的样式性质.继承特性最典型的应用通常发挥在整个网页的样式预设,即整体布局声明.而需要要指定为其它样式的部份设定在个别元素里即可达到效果.这项特性可以给网页 ...