http://acm.hdu.edu.cn/showproblem.php?pid=5772

最大权闭合子图。

得到价值w[i][j]的条件是选了i,j这两个位置的字符。选择位置的i字符花费为

第一次选s[i]:a[s[i]]       不是第一次选s[i]:b[s[i]]

所以对于选i位置字符前提为选了花费为b[s[i]]-a[s[i]]的字符i。

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

 #include<cstdio>
#include<cstring>
#define M 120010
#define N 6010
#define inf 1000000000
int len[M <<],e[M <<],nex[M <<],other[M <<],head[N],last[N],d[N],num[N];
int te,sum,_,j,num1,num2,w[][],a[],b[],m,n,i,ans,tot,ss,tt,ee,u,v,c;
char s[];
int init()
{
memset(head,,sizeof(head));
memset(num,,sizeof(num));
memset(d,,sizeof(d));
ans=ee=;
}
int min(int a,int b)
{
return a<b?a:b;
}
void add(int u,int v,int c)
{
//printf("%d %d %d\n",u,v,c);
other[++ee]=ee+;
e[ee]=v;nex[ee]=head[u];head[u]=ee;len[ee]=c;
other[++ee]=ee-;
e[ee]=u;nex[ee]=head[v];head[v]=ee;len[ee]=;
}
int dfs(int x,int flow)
{
int rec,j,p;
if (x==tt) return flow;
rec=;j=last[x];
while (j!=)
{
if (len[j]> && d[x]==d[e[j]]+)
{
last[x]=j;
p=dfs(e[j],min(len[j],flow-rec));
len[j]-=p;len[other[j]]+=p;
rec+=p;
if (rec==flow) return rec;
}
j=nex[j];
}
if (d[ss]>tot) return rec;
if (--num[d[x]]==) d[ss]=tot;
last[x]=head[x];
num[++d[x]]++;
return rec;
}
int main()
{
scanf("%d",&_);
while (_--)
{
sum=;init();
scanf("%d",&n);
scanf("%s",s+);
for (i=;i<=;i++)
scanf("%d%d",&a[i],&b[i]);
for (i=;i<=n;i++)
for (j=;j<=n;j++)
scanf("%d",&w[i][j]);
num1=;ss=;
for (i=;i<=n;i++)
for (j=i+;j<=n;j++)
if (w[i][j]+w[j][i]!=)
{
add(ss,++num1,w[i][j]+w[j][i]);
sum+=w[i][j]+w[j][i];
}
num2=;tt=num1+n+;
for (i=;i<=n;i++)
for (j=i+;j<=n;j++)
if (w[i][j]+w[j][i]!=)
{
add(++num2,num1+i,inf);
add(num2,num1+j,inf);
}
for (i=;i<=n;i++)
{
add(num1+i,num1+n+s[i]-''+,inf);
add(num1+i,tt,a[s[i]-'']);
}
for (i=;i<=;i++)
add(num1+n+i+,tt,b[i]-a[i]);
tot=num[]=tt;
for (i=ss;i<=tt;i++)
last[i]=head[i];
while (d[ss]<tot)
ans+=dfs(ss,);
printf("Case #%d: %d\n",++te,sum-ans);
}
return ; }
Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
17819971 2016-07-29 15:40:45 Accepted 5772 15MS 2052K 2416 B G++ lbz007

2016 Multi-University Training Contest 4 T9的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. 2016 Al-Baath University Training Camp Contest-1 E

    Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...

  3. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  4. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  5. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  6. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

  7. 2016 Al-Baath University Training Camp Contest-1 G

    Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...

  8. 2016 Al-Baath University Training Camp Contest-1 F

    Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...

  9. 2016 Al-Baath University Training Camp Contest-1 D

    Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...

随机推荐

  1. 赫夫曼解码(day17)

    思路: 传入map(字节与对应字节出现的次数)和最后生成的要传送的字节.将他们先转换成对应的二进制字节,再转换成原来的字符串. 代码: 12345678910111213141516171819202 ...

  2. [Redis] Redis哨兵模式部署 - zz胖的博客

    1. 部署Redis集群 redis的安装及配置参考[redis部署] 本文以创建一主二从的集群为例. 1.1 部署与配置 先创建sentinel目录,在该目录下创建8000,8001,8002三个以 ...

  3. Spring返回jsp页面

    1.SpringMVC返回的jsp,需要配置相应的viewResolvers,如: <property name="viewResolvers"> <list&g ...

  4. 吴裕雄--天生自然 R语言开发学习:方差分析

    #-------------------------------------------------------------------# # R in Action (2nd ed): Chapte ...

  5. Replace into 与Insert into on duplicate key update的区别

    前提条件:除非表有一个PRIMARY KEY或UNIQUE索引,否则,使用这2条语句没有意义.该语句会与INSERT相同 1. Replace into (1)   添加相同的主键 操作前       ...

  6. 台式机安装CentOS7.6 Minimal ISO系统并增加图形化桌面

    需求:公司测试环境因业务原因,需要在台式电脑上安装带桌面的CentOS系统,因同事有一个7.6版本Minimal ISO镜像的安装U盘,为了图方便没有去下载everything ISO镜像,而是待同事 ...

  7. HINOC2.0标准介绍(1):概述

    本文首发于'瀚诺观察'微信公众号 摘要: 2016年3月18日,国家新闻出版广电总局批准发布了行业标准GY/T 297-2016<NGB宽带接入系统HINOC2.0物理层和媒体接入控制层技术规范 ...

  8. Java版飞机订票系统

    关注微信公众号:Worldhello 回复 飞机订票系统 可获得系统源代码并可加群讨论交流 数据结构课程设计题目:          [飞机订票系统]            通过此系统可以实现如下功能 ...

  9. Java入门教程三(流程控制)

    概述 程序有 3 种结构:顺序结构.选择结构和循环结构.分别为:if elseswitch case,while与do while,for, foreach,return,break ,continu ...

  10. git指令-管理修改

    git指令-管理修改 git管理的不是文件,而是修改 eg:对readme.txt文件进行修改一行 在最后追加一句git has to tracked 然后添加,并且查看状态 cat +文件名称 表示 ...