您的位置:首页 > 其它

[POJ1087]A Plug for UNIX 做题笔记

2016-03-05 17:07 337 查看
题目链接:http://poj.org/problem?id=1087

这种主要考基本代码实现能力的题。。。没什么好说的

代码写的比较乱,一开始想写个字符串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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: