A Plug for UNIX (最大流)

1087:A Plug for UNIX(用于 UNIX 的插头)(已翻译) 1087:A Plug for UNIX(用于 UNIX 的插头)(已翻译) 有AC代码 阅读详情
You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as possible. 
Since the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling 
irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can. 
Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn't exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug. 
In order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have.
Input
The input will consist of one case. The first line contains a single positive integer n (1 <= n <= 100) indicating the number of receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle type consists of a string of at most 24 alphanumeric characters. The next line contains a single positive integer m (1 <= m <= 100) indicating the number of devices you would like to plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle it requires). A device name is a string of at most 24 alphanumeric 
characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k (1 <= k <= 100) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug.
Output
A line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in.
Sample Input
4 
A 
B 
C 
D 
5 
laptop B 
phone C 
pager B 
clock B 
comb X 
3 
B X 
X A 
X D 

Sample Output

1


代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,np,nc,m;
char x;
const int maxE = 200000;
const int maxN = 800000;
const int maxQ = 200000;
const int MAXN=505;
const int MAXM=5105;
const int oo = 1e+9;
const int inf=0x3f3f3f3f;

struct Edge {
   int v;//弧尾
   int c;//容量
   int n;//指向下一条从同一个弧头出发的弧
} edge[maxE];//边组


int adj[maxN], cntE;//前向星的表头
int Q[maxQ], head, tail;//队列
int d[maxN], cur[maxN], pre[maxN], num[maxN];
int source, sink, nv;//sourse:源点,sink:汇点,nv:编号修改的上限


void add(int u, int v, int c) {//添加边
    //正向边
    edge[cntE].v = v;
    edge[cntE].c = c;//正向弧的容量为c
    edge[cntE].n = adj[u];
    adj[u] = cntE++;

    //反向边
    edge[cntE].v = u;
    edge[cntE].c = 0;//反向弧的容量为0
    edge[cntE].n = adj[v];
    adj[v] = cntE++;
}

void rev_bfs () {//反向BFS标号
    memset(num,0,sizeof(num));
    memset(d,-1,sizeof(d));//没标过号则为-1

    d[sink] = 0;//汇点默认为标过号
    num[0] = 1;
    head = tail = 0;
    Q[tail++] = sink;

    while (head != tail) {
        int u = Q[head++];
        for (int i = adj[u]; ~i; i = edge[i].n) {
            int v = edge[i].v;
            if (~d[v]) continue;//已经标过号
            d[v] = d[u] + 1;//标号
            Q[tail++] = v;
            num[d[v]]++;
        }
    }
}

int ISAP() {
    //copy (cur, adj);//复制,当前弧优化
    memcpy(cur,adj,sizeof(cur));
    rev_bfs ();//只用标号一次就够了,重标号在ISAP主函数中进行就行了
    int flow = 0, u = pre[source] = source, i;

    while (d[sink] < nv) {//最长也就是一条链,其中最大的标号只会是nv - 1,如果大于等于nv了说明中间已经断层了。
        if (u == sink) {//如果已经找到了一条增广路,则沿着增广路修改流量
            int f = oo, neck;
            for (i = source; i != sink; i = edge[cur[i]].v) {
                if (f > edge[cur[i]].c){
                    f = edge[cur[i]].c;//不断更新需要减少的流量
                    neck = i;//记录回退点,目的是为了不用再回到起点重新找
                }
            }
            for (i = source; i != sink; i = edge[cur[i]].v) {//修改流量
                edge[cur[i]].c -= f;
                edge[cur[i] ^ 1].c += f;
            }
            flow += f;//更新
            u = neck;//回退
        }
        for (i = cur[u]; ~i; i = edge[i].n) if (d[edge[i].v] + 1 == d[u] && edge[i].c) break;
        if (~i) {//如果存在可行增广路,更新
            cur[u] = i;//修改当前弧
            pre[edge[i].v] = u;
            u = edge[i].v;
        }
        else {//否则回退,重新找增广路
            if (0 == (--num[d[u]])) break;//GAP间隙优化,如果出现断层,可以知道一定不会再有增广路了
            int mind = nv;
            for (i = adj[u]; ~i; i = edge[i].n) {
                if (edge[i].c && mind > d[edge[i].v]) {//寻找可以增广的最小标号
                    cur[u] = i;//修改当前弧
                    mind = d[edge[i].v];
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre[u];//回退
        }
    }

    return flow;
}
struct  device  //电器
{
    char s1[30],s2[30];
}dd[MAXN];
struct adapter //转换器
{
    char s1[30], s2[30];
}a[MAXN];
struct receptacle  //插座
{
    char s[30];
}r[MAXN];

void init () {//初始化
    memset(adj,-1,sizeof(adj));
    cntE = 0;
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%s",r[i].s);

    scanf("%d",&m);
    for(int i=1; i<=m; i++)
        scanf("%s%s",dd[i].s1,dd[i].s2);
    int K;
    scanf("%d",&K);
    for(int i=1; i<=K; i++)
        scanf("%s%s",a[i].s1,a[i].s2);
    init();
    source=0;  sink=n+m+K+1; //源点,汇点
    nv=sink+1;
    for(int i=1; i<=m; i++)  //所有电器
    {
        add(source,i,1);  //源点和电器建边容量为1

        for(int j=1; j<=K ; j++) //所有电器和转换器的连接
            if(!strcmp(dd[i].s2 , a[j].s1)) //电器可以插入转换器中
                add(i,j+m,oo);

        for(int j=1; j<=n; j++)  //电器和插座直接相连
            if(!strcmp(dd[i].s2 , r[j].s))
                add(i,m+K+j,1);
    }

    for(int i=1; i<=K; i++)
    {
        for(int j=1; j<=K; j++) //所有转换器和转换器之间的连接
            if(i!=j && !strcmp(a[i].s2 , a[j].s1))
                //不用考虑相同种类的转换器的连接,因为没有意义
                add(i+m,j+m,oo);

        for(int j=1; j<=n; j++) //所有转换器和所有插座连接
            if(!strcmp(a[i].s2 , r[j].s)) //转换器可插入插座中
                add(i+m , m+K+j , oo);  //转化器和插座相连
    }

    for(int i=1; i<=n; i++)  //插座和汇点相连
        add(m+K+i,sink,1);
        nv=sink+1;

    cout<<m-ISAP()<<endl;
}


多核异构处理器核间通信:从共享内存到RPMsg协议的实战解析 在多核异构处理器架构中,核间通信是实现高效协同计算的核心技术。其基本原理是通过共享内存作为数据交换的物理媒介,配合硬件信号机制实现同步与通知。从技术价值看,它解决了不同架构核心间的数据共享与指令协同问题,是释放异构计算潜力的关键。在应用场景上,该技术广泛应用于智能物联网设备、汽车电子和工业控制等领域,用于连接高性能应用处理器与实时微控制器。本文以典型的ARM Cortex-A与Cortex-M组合为例,深入剖析了基于共享内存和RPMsg协议的通信机制,涵盖了从硬件基础到软件协议栈的完整实现链路,并提供了针对 阅读详情

相关推荐

详解电路设计中的RC电路

​ 一、什么是RC电路 RC电路是指由电阻R和电容C组成的电路,它是脉冲产生和整形电路中常用到 的电路。 常见的RC充、放电电路、RC积分电路合RC微分电路接下来详细讲解。 二、RC充、放电电路 RC充、放电电路如下图所示,下面通过充电和放电两个过程来分析此电路。 ​RC充、放电电路图 (1)RC充电电路 RC充电电路图如下图(a)所示 RC充电电路图及充电曲线图 ​由上图分析: 将上图a中开关S置于“1”处,电源E开始通过电阻R对电容C充电,由于刚开始充电时电容两端没有..

IT xiaobai 4万+

C - A Plug for UNIX(网络流)

C - A Plug for UNIXPOJ - 1087 题意: ​ 给你一些插头,和插座。插头和插座分别有不同的型号,只有相同的型号才能配成一对。现在有k个转换器,每种转换器有无限个,但不一定有所有的转换器型号,转换器的作用是:将一个插头转换成另一个插头。现在求你最少有多少个插头不能插到插座上。 分析: ​ 只要求最多有多少个插头可以插到插座上即可,那么我们可以采用网络流算法。 让sour...

Dch19990825的博客 440

Ubuntu AX200 iwlwifi-cc-46.3cfab8da.0.tgz无法下载的解决办法

AX200 iwlwifi-cc-46.3cfab8da.0.tgz 无法下载网页出问题使用 其他方法来进行但是前提还是有网络。

chen20170325的博客 1039

A Plug for UNIX 【POJ - 1087】【网络流Dinic】

题目链接 题意:这道题的题面确实是太长了,但是看了一下样例,再推就是这么个意思(看样例猜题意猜中系列),题意是这样的,有N个插座(可能有些用电器是没有已经提供的插座的,需要转接),但是每个列写出来的插座的个数只有一个,以及M个用电器它们都有对应的唯一的指定插座,还有K个转接方式,转接线可以无限次使用,要问的是最少有多少的用电器是没法供上电的。 思路:我们可以这么考虑,从源点给予每个插座...

既然弱小,就只顾变强就是了 292

消费级显卡跑大模型:RTX 3060 12G 部署 70 亿参数 AI 编程助手的优化实战

本文详细介绍了如何在消费级显卡RTX 3060 12G上,通过量化、投机解码和分页KV-Cache等核心优化技术,成功部署并流畅运行70亿参数的CodeLlama-7B编程助手。文章提供了从环境配置、模型准备到参数调优的完整实战笔记,实测显存占用降至7.3GB,生成速度达21.7 token/秒,为开发者和技术爱好者提供了可行的本地AI部署方案。

weixin_29053067的博客 1261

RAP 语义键:让 UUID 世界变得可读、可导航、可校验

RAP 语义键的核心价值与实现要点 语义键(Semantic Key)是 ABAP RAP 模型中连接技术实现与业务体验的关键设计:通过为实体指定一组业务可读的标识字段(如订单号、物料编码等),在保持技术主键(如UUID)不变的同时,显著提升用户界面的可读性和导航体验。 主要优势: 提升UI可读性:Fiori元素会突出显示语义键字段,帮助用户快速识别关键业务数据 改善导航体验:OData URL会使用可读的语义键而非技术ID,便于分享和排查问题 降低沟通成本:业务人员能准确引用有意义的业务标识而非技术ID

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 156

POJ - 1087 A Plug for UNIX (最大流+建图)

链接:https://cn.vjudge.net/problem/POJ-1087 题意:m个设备,n个插座,k种适配器(每种有无穷个)。m个设备需插入相应插座,适配器描述为(a,b),既可以将b类插座变为a类插座。问最少有多少设备不能插入插座。就是最大流,主要需要自己编号,我编的时候烦的一批,看了别人代码,实现就是简洁,菜死了。。。说一下建图。 1、插座与汇点相连,容量为1。 2、设备与源...

birdmanqin的博客 304

基于FPGA的多级CIC滤波器实现四倍插值

基于FPGA的多级CIC滤波器实现四倍插值 原文链接:基于FPGA的多级CIC滤波器实现四倍插值 在《基于FPGA的多级CIC滤波器实现四倍抽取一》和《基于FPGA的多级CIC滤波器实现四倍抽取二》中我们先来了解滑动平均滤波器、微分器、积分器以及梳状滤波器原理以及它们的幅频响应。此篇我们将用verilog实现基于FPGA的多级CIC滤波器实现四倍插值。 1 CIC滤波器的基本概述 CIC(积分梳状...

baidu_34971492的博客 3142

UVa 753 A Plug for UNIX

最大流

依然一笑作春温。 493

Uniapp微信小程序:详解根据角色权限动态展示底部tabbar,支持不同身份显示不同底部导航+灵活权限组合完整源码,菜单切换不闪烁、秒加载不卡(提供动态原生Tabbar配置+自定义组件形式2种方案)

uni-app微信小程序实现根据用户不同角色显示不同的tabBar,uniapp微信小程序原生tabbar实现动态菜单导航,微信小程序动态tabBar实现:基于自定义组件,灵活支持不同用户角色,动态pages,uni-app微信小程序动态tabBar,根据不同用户展示不同的tabBar,uni-app微信小程序动态切换tabBar,根据不同用户角色展示不同的tabBar,微信小程序自定义底部tabbar动态修改,Uniapp小程序根据权限(角色)不同动态展示底部tabbar,解决tabbar闪烁卡顿问题,小

✅ 精选推荐文章 4284

COMSOL有限元仿真深度指南:为结构件添加预应变

一、什么是预应力和预应变? 我们知道,结构力学问题中,结构件在外载荷作用下产生变形响应以吸收外载荷做功带来的能量;而在结构内部,整体的受力和变形则是通过固体微元的应力与应变传递的。 对于线弹性材料来说,应力与应变的关系可以线性表达,也即 σ=Dεσ = Dεσ=Dε ;在该式中,无论固体微元的应变程度多大,弹性矩阵DDD都是一个不变的常值。而对于超弹性材料来说,应力与应变的关系则需要通过应变能密度函数WWW来表征(详细参加博客《COMSOL有限元仿真深度指南:超弹性模型有限元分析入门》)。 从上面我们也不难

忘了面孔的Batou 7171

POJ1087 A Plug for UNIX —— 最大流

题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit:1000MS Memory Limit:65536K Total Submissions:17861 Accepted:6172 Description You are in...

weixin_30908649的博客 72

物联网系列③——基于ESP8266与点灯科技平台的温湿度计设计(接入小爱同学)

物联网系列③——基于ESP8266与点灯科技平台的温湿度计设计(接入小爱同学)一、设计目标二、电路展示三、存在的问题四、代码五、需要注意的问题 一、设计目标       前一阵子偶然接触到点灯科技平台,产生了制作一块ESP8266电路板,拥有以下功能: 使用CH340对ESP8266进行程序下载 使用DHT11进行温湿度数据读取 使用点灯科技平台接入物联网,可通过手机APP获得室内温湿度 接入小爱同学,通过语音获取室内温湿度信息 Mico US

weixin_43278295的博客 8853

A Plug for UNIX 最大流 建图

You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and id...

突破吧!次元壁 218

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Poj 1087 A Plug for UNIX 最大流

Poj 1087 A Plug for UNIX 最大流 题目描述: You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make ...

weixin_30547797的博客 167

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

A Plug for UNIX 最大流

/*题意就是给了你m个电器,n个插头,tt个转换器,以及自己增加一个虚拟的源点和汇点。将转换器和插头相连的边置为无穷大。 其余的边长度都置为1.该模板中n表示图中节点的总数。最后要记得修改。还有一点需要注意的就是转换不是只有样例中给出的'X,可能有无数个,无数种类型。 在这里借鉴了一种网上map的写法。很简介。下面这个网址给了一张图很详细。一看便知http://www.cnblogs.com/lo

ihe 1110310415 linyiming QQ:545931195 875

POJ - 1087 A Plug for UNIX(最大流)

题目连接 题意: 现有 N 个不同类型的插座, 每个 插座只有一个, M个用电器,每个用电器只能插到一个插座上, K个转换器,每个转化器有无数个(因为你可以在附近的店买) 思路: 1)读题画图,目标求出最少有几个用电器用不成(我们需要求最多有几个用电器能用,好像可以最大匹配,最大流)画完图后,然后最大匹配尝试失败,最大流,画个源点和汇点。 2)这种题 ,就是网络流的板子 ,这个题建...

Harington的博客 305

POJ 1087 A Plug for UNIX 网络流最大流

题意比较啰嗦。简单的说就是有n个插座,m个设备和k种插座转换器,问最多能同时插上几个设备。我们可以用网络流中的最大流来解这个问题。 首先,我们要将设备名映射成一个唯一的数字,本来想用hash,但是范围太大,因此可以使用动态递增标记,每添加一个设备,为其分配一个编号,并且编号值增1,这个可以用map来解决。 其次,不管是设备还是插座还是转换器,我们都将其视为图中的一个节点,并且让源点指向

colorfulshark 481

A Plug for UNIX 最大流dinic

题目: You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information a...

zhangjinlei321的博客 193

A Plug for UNIX 最大流 EK算法

You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and id...

weixin_44184902的博客 90

POJ 1087 A Plug for UNIX 最大流

题意:有n个插座,m台设备,每个设备对应一个插头,k个转接器,每个转接器支持某种插座中插某种插头,求不能使用的最小设备数 思路:源点与插座相连,设备与汇点相连,容量都为1,每个设备对应的插座与其相连,容量为1,转接器中对应的插座与转换的插头相连,需要注意的是这个容量为无限大。之后用最大流求出可以使用的最多设备数,转换一下即可得出答案 #include<iostream> #includ...

qq_42397248的博客 171

POJ 1087 A Plug for UNIX (最大流)

题意:n个插座(字符串表示),m个插头(字符串表示),k种转换器,转换器有无数个,可以将插头s2转换为s1,求最少多少个插头不能插上插座。 题解:最大流 我们将插座和插头都视作同一字符串,用map表示,即能插上就直接插上,不再建边,差别体现在与源点还是汇点相连。 ①将源点0与插座建边,权值为1。 ②将汇点num+1与插头建边,权值++,因为插头可能相等。num表示总的点,所以要等所有边建好后,再将插头与汇点建边。 ③将转换器建边,因为可以将插头s2转换为s1,相当于s1->s2。 这里用的sap。用字

gongyuandaye的博客 228

POJ- A Plug for UNIX (最大流)

题意: 给你n种插座和m种电器,但是发现电器和插座有些不匹配,现给你每个电器需要的插座种类。然后和插座转换器的类型 求最少有多少个电器没有插座 最大流问题 关键问题在于建图 题目中给的都是字母,需要转化为相应的数字,然后再建图 每中电器都相应的转化为数字,这时候用的就是map的一一对应了 然后建图: 源点----->插座(转换器用来插座对插座)---->电器---->汇点

wjhshuai的博客 358

A Plug for UNIX(最大流)

题目链接点击这里                                                                                 A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16517   Accepte

x_y_q_的博客 754

POJ 1087--A Plug for UNIX【最大流dinic】

A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14711   Accepted: 4959 Description You are in charge of setting up the press room for the i

_阿阿阿阿欢 1053

ZOJ-1157 A Plug for UNIX 【最大流】

Problem You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of in...

173
上一篇: 寒假训练—2018.2.6
下一篇: Control (对最小割容量的理解)(拆点+最大流)
sdau_blue
博客等级 码龄10年 291粉丝 610原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值