Hold Your Hand

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 169    Accepted Submission(s): 38

Problem Description
She
walks in beauty, like the night of cloudless climes and starry skies.
And all that's best of dark and bright, meet in her aspect and her eyes.
Thus mellow'd to that tender light, which heaven to gaudy day denies.
Fang Fang says she is afraid of dark.

``Never fear, I will hold your hand," I reply.

Fang Fang says she hates some 8-digit binary numbers.
I ask Cupid for help. Cupid can sell me some supernatural powers.
Some of them can eliminate all 8-digit binary numbers in the world with a certain prefix, and some of them can eliminate all 8-dight binary numbers with a certain suffix.

``..., but you must offer your IQ in exchange for them."

``You have my permission", I say. True, I should minimize my damage, but maybe you can help me.

 
Input
The input contains several test cases. The first line of the input is a single integer t (t≤10) which is the number of test cases.

Then t test cases follow.
Each test case contains several lines.
The first line contains the integer n (1≤n≤256) and m (1≤m≤500).
Here, n corresponds to the number of 8-digit binary numbers which Fang Fang hates, and m corresponds to the number of supernatural powers.
The second line contains n integer numbers a1,a2,⋯,an where 0≤a1,⋯,an≤255, which are 8-digit binary numbers written by decimal representation.
The following m lines describe the supernatural powers one per line in two formats.
⋅ P s w: you can eliminate all 8-digit binary numbers by prefixing the string s, with w (1≤w≤1000) units of IQ.
⋅ S s w: you can eliminate all 8-digit binary numbers by suffixing the string s, with w (1≤w≤1000) units of IQ.

 
Output
For each test case, you should output the minimum cost of IQ as a unit, or ``-1" if Cupid could not help me.
 
Sample Input
1
8 7
0 1 2 3 4 5 6 7
P 000001 1
P 0000000 1
S 10 1
S 11 1
S 00 1
S 01 1
P 0000001 3
 
 
Sample Output
Case #1: 4
 

题意转化一下就是要割断所有的8位二进制。

将前缀插入以S为根的字典树,后缀插入以T为根的字典树。val保存最小的w。

一个前缀对应着割断所有包含这个前缀的二进制,后缀类似。

然后把两颗树的对应叶子结点相连跑最小割。

#include<bits/stdc++.h>
using namespace std; const int INF = 0x3f3f3f3f;
const int sigma_size = ,maxnds = (**+)*;
int S,T; struct Edge
{
int v,cap,nxt;
};
#define PB push_back
vector<Edge> edges;
int head[maxnds];
inline void AddEdge(int u,int v,int c)
{
edges.PB({v,c,head[u]});
head[u] = edges.size()-;
edges.PB({u,,head[v]});
head[v] = edges.size()-;
} bool vis[maxnds];
int lv[maxnds],cur[maxnds];
bool bfs()
{
memset(vis,,sizeof(vis));
queue<int> q; q.push(S); lv[S] = ;
vis[S] = true;
while(q.size()){
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = edges[i].nxt){
Edge &e = edges[i];
if(!vis[e.v] && e.cap>){
vis[e.v] = true;
lv[e.v] = lv[u]+;
q.push(e.v);
}
}
}
return vis[T];
} int aug(int u,int a)
{
if(u == T||!a) return a;
int flow = ,f;
for(int &i = cur[u]; ~i; i = edges[i].nxt){
Edge &e = edges[i];
if(lv[e.v] == lv[u]+ && (f=aug(e.v,min(a,e.cap)))>){
e.cap -= f; edges[i^].cap += f;
flow += f; a -= f;
if(!a) break;
}
}
return flow;
} int MaxFlow()
{
int flow = ;
while(bfs()){
memcpy(cur,head,sizeof(head));
flow += aug(S,INF);
if(flow >= INF) break;
}
return flow;
} struct Node
{
int ch[sigma_size],val;
void init(){}
}nd[maxnds];
const int nil = ;
int cnt; inline int newNode()
{
int i = ++cnt;
memset(nd[i].ch,nil,sizeof(nd[i].ch));
nd[i].val = INF;
head[i] = -;
return i;
} int add(int rt,char *s,int v = INF)
{
int u = rt;
for(int i = ; s[i]; i++){
int c = s[i]-'';
if(!nd[u].ch[c]){
nd[u].ch[c] = newNode();
}
u = nd[u].ch[c];
}
nd[u].val = min(nd[u].val,v);
return u;
} bool flag;
void dfs(int u)
{
for(int i = ; i < sigma_size; i++){
int v = nd[u].ch[i];
if(v){
int w = nd[v].val;
if(flag){
AddEdge(u,v,w);
}else {
AddEdge(v,u,w);
}
dfs(v);
}
}
} int main()
{
//freopen("in.txt","r",stdin); char s[];
int x[+];
int Test; scanf("%d",&Test);
for(int kas = ; kas<= Test; kas++){
edges.clear();
cnt = ;
S = newNode(); T = newNode(); int n,m; scanf("%d%d",&n,&m);
for(int i = ; i < n; i++){
scanf("%d",x+i);
} while(m--){
char ch[];
int w; scanf("%s%s%d",ch,s,&w);
if(*ch=='P'){
add(S,s,w);
}else {
reverse(s,s+strlen(s));
add(T,s,w);
}
}
s[] = '\0';
for(int i = ; i < n; i++){
int t = x[i];
for(int j = ; j < ; j++){
s[j] = (t&)+'';
t>>=;
}
int v = add(T,s);
reverse(s,s+);
AddEdge(add(S,s),v,INF);
}
flag = true;
dfs(S);
flag = false;
dfs(T);
printf("Case #%d: ",kas);
int flow = MaxFlow();
if(flow >= INF){
puts("-1");
}else printf("%d\n",flow);
}
return ;
}

HDU - 5457 Hold Your Hand (Trie + 最小割)的更多相关文章

  1. HDU - 3035 War(对偶图求最小割+最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3035 题意 给个图,求把s和t分开的最小割. 分析 实际顶点和边非常多,不能用最大流来求解.这道题要用 ...

  2. HDU - 3002 King of Destruction(最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=3002   最小割模板 #include<iostream> #include<cmath> ...

  3. HDU 5889 Barricade(最短路+最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意: 给出一个图,帝国将军位于1处,敌军位于n处,敌军会选择最短路到达1点.现在帝国将军要在路径上放置障 ...

  4. HDU 5889 Barricade(最短路+最小割水题)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  5. HDU 3691 Nubulsa Expo(全局最小割Stoer-Wagner算法)

    Problem Description You may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa ...

  6. hdu 4619 Warm up 2 网络流 最小割

    题意:告诉你一些骨牌,然后骨牌的位置与横竖,这样求最多保留多少无覆盖的方格. 这样的话有人用二分匹配,因为两个必定去掉一个,我用的是最小割,因为保证横着和竖着不连通即可. #include <s ...

  7. HDU 1569 方格取数(2) (最小割)

    方格取数(2) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  8. 【HDU 6126】Give out candies 最小割

    题意 有$n​$个小朋友,给每个人分$1~m​$个糖果,有k个限制 限制形如$(x,y,z)​$ 表示第$x​$个人分到的糖数减去第$y​$个人分到的糖数不大于$z​$,给第$i​$个人$j​$颗糖获 ...

  9. hdu 6214 Smallest Minimum Cut(最小割的最少边数)

    题目大意是给一张网络,网络可能存在不同边集的最小割,求出拥有最少边集的最小割,最少的边是多少条? 思路:题目很好理解,就是找一个边集最少的最小割,一个方法是在建图的时候把边的容量处理成C *(E+1 ...

随机推荐

  1. gitHub上传代码

    首先进入github官网注册一个帐号 00.png 注册完帐号之后创建一个项目 01.png 设置创建项目的信息 02.png 创建项目完之后复制项目的地址,以供后面下载项目使用 03.png 在桌面 ...

  2. day03-CSS(1)

    一 .Css概念 CSS 指层叠样式表 (Cascading Style Sheets)(级联样式表) Css是用来美化html标签的,相当于页面化妆. ◆样式表书写位置 二. 选择器 1. 写法 选 ...

  3. aimOffset注意事项

    AimOffset的记录 AimOffset是什么,就是动画(相对于某个具体姿势比如待机动作的)叠加. AimOffset有什么用,简单说就是叠加动作,比如无双中骑马挥刀动作叠加. 注意步骤 1所有分 ...

  4. 输入apt-get update时出现Could not open lock file /var/lib/apt/lists/lock - open

    我看了其它的资料发现不够清楚 我只报这些错误 1.1.ps-aux 查出apt-get进程的PID,通常是一个四位数字. 不好找apt-get进程 输入此代码就好找了 ps -aux|grep apt ...

  5. fatal pylint error : ......can't find '__main__'module in

    fatal pylint error : ......can't find '__main__'module in原因是没有安装pylint,所以提示没有找到__main__模块 解决方案:1.到官网 ...

  6. thinkphp5修改入口文件位置及相应的问题

    问题1:thinkphp5修改入口文件 解决:参考手册 http://www.kancloud.cn/manual/thinkphp5/129746,然后需要把.htaccess跟入口文件放到同一目录 ...

  7. 微信小程序实战

    为了积攒粉丝,公司决定做一个一分钱姓名测算的小程序引导大家关注公众号. 实现的需求就是  1 首页 用户编辑姓名和性别进行提交 2 测算结果页 实现分享和支付功能 3 测算历史页面 看到用户曾经测算记 ...

  8. ZROI WC Round1 题解

    ZROI WC Round1 题解 Problem A 题意 一个 \(n \times m\) 格子图,一个人从左上角出发,每次向右或者向下走一格,方法如下: 如果他在最下面一排,那么他会往右行走. ...

  9. 前端HTML(二/三)

    待补充 一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong>.<em>.<sup>.<s ...

  10. 定时任务crontab 详解

    cron 是一个可以用来根据时间.日期.月份.星期的组合来调度对重复任务的执行的守护进程. cron 假定系统持续运行.如果当某任务被调度时系统不在运行,该任务就不会被执行. 要使用 cron 服务, ...