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. SHELL用法六(Find语句)

    1.SHELL编程Find语句案例实战 1)SHELL编程四剑客工具:Find.Grep.Sed.Awk,通过四剑客可以完成常规Linux指令无法完成或者比较复杂的功能,学好SHELL编程四剑客有助于 ...

  2. CSA|EI

    信息检索 CSA是学科特色的包含相关学科的内容,其网址是https://search.proquest.com/ 可以使用命令行检索: 分类的限制检索: 寻找检索线索可使用百科全书 EI是工程领域最全 ...

  3. 3DSMAX安装未完成,某些产品无法安装的解决方法

    3DSMAX提示安装未完成,某些产品无法安装该怎样解决呢?,一些朋友在win7或者win10系统下安装3DSMAX失败提示3DSMAX安装未完成,某些产品无法安装,也有时候想重新安装3DSMAX的时候 ...

  4. Inventor 卸载工具,完美彻底卸载清除干净Inventor各种残留注册表和文件

    一些同学安装Inventor出错了,也有时候想重新安装Inventor的时候会出现这种本电脑windows系统已安装Inventor,你要是不留意直接安装,只会安装Inventor的附件,Invent ...

  5. 饿了么vue实现学习笔记

    技术栈:vue2 + vuex + vue-router + webpack + ES6/7 + fetch + sass + flex + svg以功能实现着手学习1. 定位功能 home.vue ...

  6. if分支判断

    # 控制语句 分支 循环语句 # 判断语句 if ..elif..else # if 条件语句(比较 逻辑 成员运算) # 空数据 == False # 非空数据 == True age = 20 i ...

  7. babel-loader的原理

    本文转载自默语的博客. Babel包的构成 核心包 babel-core:是babel转译器本身,提供转译的API,例如babel.transform等,webpack的babel-loader就是调 ...

  8. JavaScript if为true的情况

    变量如果不为0,null,undefined,false,都会被处理为true.只要变量有非0的值或是某个对象,数组,字符串,都会认为true

  9. gcc编译器常用选项的含义

    -w: 关闭编译时的警告, 也就是编译后不显示任何warning,因此有时编译中会出现一些诸如数据转换之类的可忽略警告, -Wall: 显示编译后所有警告 -W: 显示警告,但是只是显示编译器认为的会 ...

  10. 连接器巨头Molex莫仕大裁员,CEO更迭

    序言:中美贸易战的大环境下,美国多方面限制对华出口电子科技,其中影响最大的莫过于限制芯片出口,中国本土芯片和电子产业也在蓬勃的发展.根据正能量电子了解连接器巨头MOLEX莫仕公司收入的1/3是来自于对 ...