题目链接: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. 一个flume agent异常的解决过程记录

    今天在使用flume agent的时候,遇到了一个异常,  现把解决的过程记录如下: 问题的背景: 我使用flume agent 来接收从storm topology发送下来的accesslog , ...

  2. java 数据结构与算法---树

    一.树的概念  除根节点外,其余节点有且只有一个父节点. 1.度 节点的度:每个节点的子节点个数. 树的度:树内各个节点的度的最大值. 树的高度(深度):树中节点的最大层次称为树的深度. 节点路径:一 ...

  3. 第217天:深入理解Angular双向数据绑定的原理

    一.理解angular双向数据绑定 双向绑定是新的前端框架中频繁出现的一个新词汇,也是mvvm的核心原理.angularjs五条核心信念中的数据驱动,便是由双向绑定进行完成. 那么什么是双向绑定,下面 ...

  4. HDU2665_Kth number

    给一个数组,求区间[l,r]中第k大的数. 今天被各种数据结构虐爆了,自己还是需要学习一下函数式线段树的,这个东西好像还挺常用. 函数式线段树的思想是这样的,对于每个时间状态,我们都建立一颗线段树,查 ...

  5. 字符串使用replaceAll()方法报异常

    对字符串使用replaceAll()方法替换 * ? + / | 等字符的时候会报以下异常 Dangling meta character '*' near index 0 这主要是因为这些符号在正则 ...

  6. A Chess Game HDU - 1524 (有向图博弈)

    题意:在一个有向无环图上有n个顶点,每一个顶点都只有一个棋子,有两个人,每次根据这个图只能将任意一颗棋子移动一步 ,如果到某一步玩家不能移动时,那么这个人就输. 分析:本题是最典型的有向无环图的博弈, ...

  7. [BZOJ4446]SCoi2015 小凸玩密室 树形DP(烧脑高能预警)

    4446: [Scoi2015]小凸玩密室 Time Limit: 10 Sec  Memory Limit: 128 MB Description 小凸和小方相约玩密室逃脱,这个密室是一棵有n个节点 ...

  8. vyos 基础配置

    vyos 基础配置 http://www.lowefamily.com.au/2015/11/29/using-a-vyos-router-with-hyper-v/1/http://thomasvo ...

  9. Navicat使用教程:获取MySQL中的行数(第1部分)

    下载Navicat Premium最新版本 Navicat Premium是一个可连接多种数据库的管理工具,它可以让你以单一程序同时连接到MySQL.Oracle及PostgreSQL数据库,让管理不 ...

  10. AtCoder Grand Contest 004

    AtCoder Grand Contest 004 A - Divide a Cuboid 翻译 给定一个\(A*B*C\)的立方体,现在要把它分成两个立方体,求出他们的最小体积差. 题解 如果有一条 ...