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. 基于Go实现的秒杀系统

    这是基于Go语言的一个秒杀系统,这个系统分三层,接入层.逻辑层.管理层.项目源码:https://github.com/BlueSimle/SecKill 系统架构图 秒杀接入层 从Etcd中加载秒杀 ...

  2. Android下如何计算要显示的字符串所占的宽度和高度

    Rect bounds = new Rect(); String text = "Hello World"; TextPaint paint; paint = findViewBy ...

  3. 截图上传功能 imageAreaselect

    前台: <script src="~/Scripts/jquery-2.1.4.min.js"></script> <link href=" ...

  4. 计总与排名SUM和RANK函数

    准备一些数据: CREATE TABLE [dbo].[SalesPerformance]( ,) NOT NULL, ) NOT NULL, [OrderDate] [DATE] NULL, ,) ...

  5. iOS风格的弹出框(alert,prompt,confirm)

    前两天,自己写了一个简单的插件,在移动端使用,不管是安卓手机还是iOS系统的手机,弹出框统一使用iOS风格的. 该弹出框是依赖于jQuery的,当然也可以将用jq写的几句代码转换为原生代码. 今天把代 ...

  6. UVA - 1330 City Game

    InputThe rst line of the input le contains an integer K | determining the number of datasets. Next l ...

  7. AtCoder Regular Contest 082 ABCD

    A #include<bits/stdc++.h> using namespace std; ]; int n,m; int main(){ cin>>n>>m; ...

  8. 15 Puzzle LightOJ - 1121

    https://cn.vjudge.net/problem/LightOJ-1121 #include<cstdio> #include<algorithm> #include ...

  9. tomcat jndi 数据源

    web.xml <!-- ================================================================================ --& ...

  10. spring boot 事务

    spring事务:默认自动提交只读:@Transactional(readOnly = true)读写:@Transactional(),因为等同于@Transactional(readOnly = ...