[POJ1087]A Plug for UNIX 做题笔记
2016-03-05 17:07
337 查看
题目链接:http://poj.org/problem?id=1087
这种主要考基本代码实现能力的题。。。没什么好说的
代码写的比较乱,一开始想写个字符串hash,但题目的字符串范围好奇怪,后来干脆暴力,数据很水也过了。。但hash函数的名字没改。
这种主要考基本代码实现能力的题。。。没什么好说的
代码写的比较乱,一开始想写个字符串hash,但题目的字符串范围好奇怪,后来干脆暴力,数据很水也过了。。但hash函数的名字没改。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int N=1000,M=500000,inf=0x3fffffff; char plug[200][50]; char devi[200][50]; char from[200][50],to[200][50]; char hash[1000][50]; int head ,d ,ver[M<<1],e[M<<1],next[M<<1]; int ne=0,tot=1,n,m,k,s,t,maxflow=0,T; bool cmp (char *a,char *b) { if ( strlen(a)!=strlen(b) ) return 0; for (int i=0;i<strlen(a);i++) if (a[i]!=b[i]) return 0; return 1; } void add (int u,int v,int w) { ver[++tot]=v;e[tot]=w;next[tot]=head[u];head[u]=tot; ver[++tot]=u;e[tot]=0;next[tot]=head[v];head[v]=tot; } int Hash (char *s) { for (int i=1;i<=ne;i++) if (cmp(hash[i],s)) return i; strcpy( hash[++ne],s );// return ne; } bool bfs () { queue<int> q; memset(d,0,sizeof(d)); q.push(s); d[s]=1; while (!q.empty()) { int x=q.front(); q.pop(); for (int i=head[x];i;i=next[i]) if (e[i]&&!d[ver[i]]) { q.push(ver[i]); d[ver[i]]=d[x]+1; if (ver[i]==t) return 1; } } return 0; } int dinic (int x,int f) { int rest=f; if (x==t) return f; for (int i=head[x];i&&rest;i=next[i]) if (e[i]&&d[ver[i]]==d[x]+1) { int now=dinic(ver[i],min(e[i],rest)); if (!now) d[ver[i]]=0;// e[i]-=now; e[i^1]+=now; rest-=now; } return f-rest; } int main () { int tmp; //scanf("%d%*c",&T); T=1; while (T--) { scanf("%d",&n); memset(head,0,sizeof(head)); tot=1;ne=maxflow=0; for (int i=1;i<=n;i++) { scanf("%s",plug[i]);// Hash(plug[i]); } scanf("%d",&m); for (int i=1;i<=m;i++) { scanf("%*s");scanf("%s",devi[i]); Hash(devi[i]); } scanf("%d",&k); for (int i=1;i<=k;i++) { scanf("%s",to[i]);scanf("%s",from[i]); Hash(from[i]);Hash(to[i]); } s=0,t=ne+1; for (int i=1;i<=n;i++) add(Hash(plug[i]),t,1); for (int i=1;i<=k;i++) add(Hash(to[i]),Hash(from[i]),inf);//注意边权是inf for (int i=1;i<=m;i++) add(s,Hash(devi[i]),1); while (bfs()) while (tmp=dinic(s,inf)) maxflow+=tmp; printf("%d",m-maxflow); } }
相关文章推荐
- Linux的运行级别和chkconfig用法
- zzuli蓝桥 text1 I 平方和
- 0031TCP/IP协议之三次握手图文详解
- 客户端python热更新
- codevs1009
- nginx配置文件
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-003- 上传文件multipart,配置StandardServletMultipartResolver、CommonsMultipartResolver
- IOS开发中的几种设计模式
- String s = "123"; 到底创建了几个对象
- 由先序序列和中序序列构建二叉树
- process令牌
- VB.net版机房收费-“未能添加对‘Factory’的引用”
- python基础0305
- BZOJ 1007: [HNOI2008]水平可见直线 平面直线
- 《HBase上的交互式SQL查询》Intel 孙元浩
- zzuli蓝桥 text1 G 搬砖
- SQL读书笔记(八) 联结表
- 第一次作业
- 2016年3月5日 进制转换(二进制、八进制、十进制、十六进制)
- Asp.Net页面传值的几种方式