博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU - 5457 Hold Your Hand (Trie + 最小割)
阅读量:6903 次
发布时间:2019-06-27

本文共 5392 字,大约阅读时间需要 17 分钟。

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 (t10) 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 (1n256) and m (1m500).
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 0a1,,an255, 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 (1w1000) units of IQ.
S s w: you can eliminate all 8-digit binary numbers by suffixing the string s, with w (1w1000) 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
using namespace std;const int INF = 0x3f3f3f3f;const int sigma_size = 2,maxnds = (256*8*2+56)*2;int S,T;struct Edge{ int v,cap,nxt;};#define PB push_backvector
edges;int head[maxnds];inline void AddEdge(int u,int v,int c){ edges.PB({v,c,head[u]}); head[u] = edges.size()-1; edges.PB({u,0,head[v]}); head[v] = edges.size()-1;}bool vis[maxnds];int lv[maxnds],cur[maxnds];bool bfs(){ memset(vis,0,sizeof(vis)); queue
q; q.push(S); lv[S] = 1; 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>0){ vis[e.v] = true; lv[e.v] = lv[u]+1; q.push(e.v); } } } return vis[T];}int aug(int u,int a){ if(u == T||!a) return a; int flow = 0,f; for(int &i = cur[u]; ~i; i = edges[i].nxt){ Edge &e = edges[i]; if(lv[e.v] == lv[u]+1 && (f=aug(e.v,min(a,e.cap)))>0){ e.cap -= f; edges[i^1].cap += f; flow += f; a -= f; if(!a) break; } } return flow;}int MaxFlow(){ int flow = 0; 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 = 0;int cnt;inline int newNode(){ int i = ++cnt; memset(nd[i].ch,nil,sizeof(nd[i].ch)); nd[i].val = INF; head[i] = -1; return i;}int add(int rt,char *s,int v = INF){ int u = rt; for(int i = 0; s[i]; i++){ int c = s[i]-'0'; 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 = 0; 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[10]; int x[256+6]; int Test; scanf("%d",&Test); for(int kas = 1; kas<= Test; kas++){ edges.clear(); cnt = 0; S = newNode(); T = newNode(); int n,m; scanf("%d%d",&n,&m); for(int i = 0; i < n; i++){ scanf("%d",x+i); } while(m--){ char ch[2]; 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[8] = '\0'; for(int i = 0; i < n; i++){ int t = x[i]; for(int j = 0; j < 8; j++){ s[j] = (t&1)+'0'; t>>=1; } int v = add(T,s); reverse(s,s+8); 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 0;}

 

转载于:https://www.cnblogs.com/jerryRey/p/4837012.html

你可能感兴趣的文章
计算机专业术语
查看>>
Leetcode-探索 | 移动零
查看>>
DBI 数据库模块剖析:Perl DBI 数据库通讯模块规范,工作原理和实例
查看>>
Tesseract+opencv+VS+win实现OCR
查看>>
android在activity中锁屏解锁后重走OnCreate的问题的解决办法
查看>>
[学习笔记]博弈论
查看>>
python os sys模块(二)
查看>>
一次linux启动故障记录
查看>>
linux 3.10内核 xfs的一次io异常导致的hung crash
查看>>
Castle ActiveRecord学习笔记(转)
查看>>
springboot+mybatis环境的坑和sql语句简化技巧
查看>>
Keil C编译器的变量存储分配
查看>>
非常不错的js 屏蔽类加验证类
查看>>
Innodb间隙锁,细节讲解(转)
查看>>
Apache安装
查看>>
C语言练习题库----数组
查看>>
nginx虚拟主机配置
查看>>
关于对char类型数据赋予负值的汇编表现
查看>>
润乾报表在proxool应用下的数据源配置
查看>>
Python基础23_os,sys,序列化,pickle,json
查看>>