题目链接:https://cn.vjudge.net/contest/68128#problem/C

没理解好题意真的麻烦,一上午就这么过去了。。。。。

具体思路:按照 源点 ->插座->转换器->插头->汇点的方式建图,源点到插座的流量为1,插头到汇点的流量为1,转换器之间如果能相连则赋值为inf.(转换器相连的条件,对于某一个转换器 他可以连下一个的头或者尾,但是这个转换器的方向不能变!!!坑点)

AC代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<queue>
#include<stdio.h>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
# define inf 0x3f3f3f3f
# define maxn 500+10
# define ll long long
int n,m,k;
int dis[maxn][maxn];
char chazuo[maxn][maxn];
char change[maxn][maxn];
char chahead[maxn][maxn];
int vis[maxn],pre[maxn];
struct node
{
char fr[maxn];
char to[maxn];
} a[maxn]; bool bfs(int st,int ed)
{
queue<int>q;
memset(vis,0,sizeof(vis));
vis[st]=1;
pre[st]=st;
q.push(st);
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=1; i<=n+m+k+2; i++)
{
if(vis[i]==0&&dis[top][i]>0)
{
pre[i]=top;
vis[i]=1;
if(i==ed)return true;
q.push(i);
}
}
}
return false;
}
int EK(int st,int ed)
{
int ans=0;
while(bfs(st,ed))
{
int minn=inf;
for(int i=ed; i!=st; i=pre[i])
{
minn=min(minn,dis[pre[i]][i]);
}
for(int i=ed; i!=st; i=pre[i])
{
dis[pre[i]][i]-=minn;
dis[i][pre[i]]+=minn;
}
ans+=minn;
}
return ans;
}
int main()
{
while(~scanf("%d",&n))
{
memset(dis,0,sizeof(dis));
for(int i=1; i<=n; i++)
{
scanf("%s",chazuo[i]);
}
scanf("%d",&m);
for(int i=1; i<=m; i++)
{
scanf("%*s %s",chahead[i]);
}
scanf("%d",&k);
for(int i=1; i<=k; i++)
{
scanf("%s %s",a[i].fr,a[i].to);
}
for(int i=1; i<=n; i++) //zuo -> chahead
{
for(int j=1; j<=m; j++)
{
if(strcmp(chazuo[i],chahead[j])==0)
{
dis[i][n+k+j]=1;
}
}
}
for(int i=1; i<=n; i++) //zuo -> change
{
for(int j=1; j<=k; j++)
{
if(strcmp(chazuo[i],a[j].fr)==0||strcmp(chazuo[i],a[j].to)==0)
{
dis[i][n+j]=1;
}
}
}
for(int i=1; i<=k; i++) //change-> head
{
for(int j=1; j<=m; j++)
{
if(strcmp(a[i].to,chahead[j])==0||strcmp(a[i].fr,chahead[j])==0)
{
dis[n+i][n+k+j]=1;
}
}
}
for(int i=1; i<=k; i++) // change -> change
{
for(int j=1; j<=k; j++)
{
if(strcmp(a[i].fr,a[j].to)==0)
{
dis[i+n][j+n]=inf;
}
}
}
int st=n+m+k+1;
int ed=st+1;
for(int i=1; i<=n; i++)
{
dis[st][i]=1;
}
for(int i=1; i<=m; i++)
{
dis[n+k+i][ed]=1;
}
int ans=EK(st,ed);
printf("%d\n",m-ans);
}
return 0;
}

C - A Plug for UNIX (又是建图坑)的更多相关文章

  1. poj 1087 A Plug for UNIX(字符串编号建图)

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14862   Accepted: 5026 ...

  2. POJ A Plug for UNIX (最大流 建图)

    Description You are in charge of setting up the press room for the inaugural meeting of the United N ...

  3. A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏

    A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...

  4. POJ1087 A Plug for UNIX(网络流)

                                       A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. 【poj1087/uva753】A Plug for UNIX(最大流)

    A Plug for UNIX   Description You are in charge of setting up the press room for the inaugural meeti ...

  6. TZOJ 1911 A Plug for UNIX(最大流)

    描述 You are in charge of setting up the press room for the inaugural meeting of the United Nations In ...

  7. POJ1087 A Plug for UNIX 2017-02-12 13:38 40人阅读 评论(0) 收藏

    A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeting ...

  8. POJ1087:A Plug for UNIX(最大流)

    A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...

  9. POJ1087 A Plug for UNIX —— 最大流

    题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K T ...

随机推荐

  1. 聚合函数count里面加条件

    聚合函数中如果想汇总某一类数据,可以在括号中增加条件: sum(case when 字段>0 then 1 else 0 end) as 字段 *注意:count(case when 字段> ...

  2. (待修莫队 没过! 抽空在检查)Dynamic len(set(a[L:R])) UVA - 12345

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  3. attention、self-attention、transformer和bert模型基本原理简述笔记

    attention 以google神经机器翻译(NMT)为例 无attention: encoder-decoder在无attention机制时,由encoder将输入序列转化为最后一层输出state ...

  4. 【树论 1】 prim算法的学习和使用

    进阶版神犇可以看看本题解的姊妹篇 Kruskal算法的学习和使用 下面的内容是prim算法 但是最小生成树是什么呢? 标准定义如下:在边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值 ...

  5. 网络对抗课题4.3.1 SQL注入原理与实践

    网络对抗课题4.3.1 SQL注入原理与实践 原理 SQL注入漏洞是指在Web应用对后台数据库查询语句处理存在的安全漏洞.也就是,在输入字符串中嵌入SQL指令,在设计程序中忽略对可能构成攻击的特殊字符 ...

  6. Java之List和Set

    List.Set.数据结构.Collections 初次学习,涉及到List集合,Set集合和数据结构方面的一些知识,有错误还请批评指正 数据结构 数据存储的常用结构有:栈.队列.数组.链表和红黑树. ...

  7. cocoaPods安装、更新第三方库

    pod install 换成 pod install --verbose --no-repo-update pod update 换成 pod update --verbose --no-repo-u ...

  8. poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串

    Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16223   Accepted: 4763 Descr ...

  9. Java入门:基础算法之计算三角形面积

    本部分介绍如何计算三角形面积. /** * @author: 理工云课堂 * @description: 程序计算三角形的面积.三角形的底和高由用户输入 */ import java.util.Sca ...

  10. Dockerfile 部署 nodejs

    1.编写.dockerignore 构建镜像时,并不需要node_modules目录等内容,可以使用.dockerignore忽略一些文件 # .dockerignore Dockerfile nod ...