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 ...
随机推荐
- js基础(补10.10)
1.内嵌式: <html> <head> <title></title> </head> <body> <a href=& ...
- 2015年第六届蓝桥杯国赛试题(JavaA组)
1.结果填空 (满分15分)2.结果填空 (满分35分)3.代码填空 (满分31分)4.程序设计(满分41分)5.程序设计(满分75分)6.程序设计(满分103分) 1.标题:胡同门牌号 小明家住在一 ...
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38352503 ,本文出自[张鸿洋的博客] 1.概述 上一篇已经基本给大家介绍了如 ...
- ue4 打个log难如狗
注意: 把log相关两个宏写到类中,并编译后,在输出日志的位置的Categories关键字过滤的位置看不到自己的标签是因为需要先运行一次,输出一些这个标签的log后,这个自定义的标签才会显示在这 原文 ...
- Unity3D 自动添加Fbx Animation Event
http://blog.csdn.net/aa20274270/article/details/52528449 using UnityEngine; using System.Collections ...
- UnityShader实例09:Stencil Buffer&Stencil Test
http://blog.csdn.net/u011047171/article/details/46928463 Stencil Buffer&Stencil Test 在开始前先吐槽下uni ...
- 洛谷P2029 跳舞
P2029 跳舞 题目描述 小明今天得到一个跳舞毯游戏程序Dance.游戏每次连续出N个移动的“箭头”,箭头依次标号为1到N,并且的相应的分数S[1..N].如果你能“踏中”第i号箭头,你将获得相应的 ...
- 封装MySQL的单例,连接数据库并对数据进行增删改查操作
单例: 一个类只能有一个对象 应用场景:多次请求数据库只需要一个连接对象. 实现:三私一公 1.私有的静态属性用来保存对象的单例2.私有的构造方法用来阻止在类的外部实例化3.私有的__clone阻止在 ...
- 笔记-JavaWeb学习之旅16
增强对象的功能 动态代理:在内存中形成代理类 实现步骤: 代理对象和真实对象实现相同的接口 代理对象 = Proxy.newProxyInstance(); 使用代理对象调用真实对象的方法 增强方法 ...
- JQuery Easyui/TopJUI 创建多级联动下拉框(纯HTML实现!!!)
JQuery Easyui/TopJUI 创建多级联动下拉框(纯HTML实现!!!) 效果展示: 代码如下: <form data-toggle="topjui-form"& ...