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. QDUOJ 河老师的新年礼物(尺取法)

    河老师的新年礼物 发布时间: 2017年1月1日 15:11   最后更新: 2017年1月1日 15:13   时间限制: 1000ms   内存限制: 256M 描述 河老师的新年礼物是一个长度为 ...

  2. SQL SERVER动态列名

    在ms sql server实现动态呈现列的方法很多.下面Insus.NET解决也算是另外一种参考. 如: 准备实现功能的数据: ) NOT NULL PRIMARY KEY) INSERT INTO ...

  3. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  4. jzoj6004. 【PKUWC2019模拟2019.1.17】集合 (组合数学)

    题面 题解 这种题目就是要好好推倒 我们枚举最小的数是哪一个,那么答案就是\[Ans=\sum_{i=1}^nT^i{n-i\choose k-1}\] 因为有\[\sum_{i=p}^n{n-i\c ...

  5. 使用docker搭建项目环境

    # 清屏 clear # 查看当前文件夹下的列表 ls # 跳目录 cd ~ 代表当前用户文件夹 cd / 代表根目录 cd..返回上一级目录 cd #sudo 使用超级管理员创建文件夹 不加sudo ...

  6. redis单机上部署集群

    一.安装单机redis  redis的安装:版本至少是3.2.8及其以上,这里以3.2.8版本为例说明 1.安装redis wget http://download.redis.io/releases ...

  7. CentOS 7 部署 nginx-1.14.2

    参考:http://www.linuxe.cn/post-168.html 链接:https://pan.baidu.com/s/1NzHIY7mYgHJ6yMF_rdd0ZQ 提取码:n8o9 下载 ...

  8. Junit使用总结

    对Junit的使用总结,后期会更新! 1.做自动单元测试用的,在方法前面加一个@Test(准备 一个测试用例),这是必须要加上的. 判定是成功还是失败. 最后是加一个断点,Assert.assertE ...

  9. Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number

    链接:https://codeforces.com/contest/1167/problem/A 题意: A telephone number is a sequence of exactly 11  ...

  10. Codeforces Round #527-D1. Great Vova Wall (Version 1)(思维+栈)

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...