A Plug for UNIX 最大流简要变形

本文探讨了一个关于如何使用有限类型的适配器来解决多种电子设备与不同插座类型匹配的问题,目标是最小化无法连接的设备数量。通过构建一个特殊的网络流模型,并运用深度优先搜索算法来解决这个问题。

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 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

要求:求满足不能插上插座的用电器最少个数
要注意:每种适配器都有无限个,所以建图的时候要改为inf


Select Code
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <map>
//#include <pa
using namespace std;
const int N = 500;
const int INF = 0x3f3f3f3f;
int n,m,k;
string s1[105],s2[105];
pair < string , string > p[N];
map<string, int> eee;
map<int, string> aaa;
int cnt = 1;
int used[N];
struct edge
{
    int to,cap,rev;
};
vector <edge> G[N];
void add_edge(int a,int b ,int c)
{
    G[a].push_back((edge){b,c,G[b].size()});
    G[b].push_back((edge){a,0,G[a].size()-1});
};
bool dfs(int s,int t)
{
    if(s==t)
        return true;
    used[s]=1;
    for(int i=0;i<G[s].size();i++)
    {
        edge &e=G[s][i];
        if(G[s][i].cap>0&&!used[e.to]){
            if(dfs(G[s][i].to,0)){
                e.cap--;
                G[e.to][e.rev].cap=1;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    cin>>n;string s0;
    for(int i = 0; i < n; i ++)
    {
        cin>>s1[i];
        eee[s1[i]]=cnt;
        aaa[cnt++]=s1[i];
    }
    cin>>m;
    for(int i = 0; i < m; i ++)
    {
        cin>>s0>>s2[i];
    }
    cin >> k;
    for(int i =0; i < k; i ++)
    {
        cin>>p[i].first>>p[i].second;
    }//电器和插板
    for(int i=1;i<=m;i++)
    {
        if(eee[s2[i-1]]){
        int c=m+eee[s2[i-1]];
        add_edge(i,c,1);
        }
    }//插板和终点;
    for(int i=1;i<=n;i++)
    {
        add_edge(m+i,0,1);
    }
    for(int i=1;i<=k;i++)
    {
        add_edge(m+n+i,m+n+k+i,INF);
        for(int j=1;j<=m;j++)
        {
            if(s2[j-1]==p[i-1].first)
            add_edge(j,m+n+i,INF);
        }
    }
    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=k;j++)
        {
            if(p[j-1].first==p[i-1].second)
            add_edge(m+n+k+i,n+m+j,INF);
        }
        if(eee[p[i-1].second]){
            add_edge(m+n+k+i,m+eee[p[i-1].second],INF);
        }
    }
    int ans=0;
    for(int i=1;i<=m;i++){
        memset(used,0,sizeof(used));
        if(!dfs(i,0))
            ans++;
    }
    printf("%d\n",ans);
    return 0;
}

Comment in Markdown
Preview:
PostClear
### UNIX 系统中的插件与扩展 在 UNIX 系统中,插件或扩展通常以共享对象(shared object)的形式存在,其文件扩展名为 `.so` [^1]。这些共享对象库可以通过动态加载的方式集成到应用程序中,从而提供额外的功能支持。例如,在 MySQL 中,InnoDB 插件通过 `ha_innodb_plugin.so` 文件实现,并通过 `plugin-load` 选项进行加载 [^1]。 对于 GStreamer 这样的多媒体框架,UNIX 平台上的插件也通常以 `.so` 文件形式存在 [^2]。GStreamer 的插件初始化函数(如 `plugin_init`)会注册多个元素(elements),包括播放器(`playbin`、`playbin3`)、解码器(`decodebin`、`decodebin3`)以及 URI 处理器(`uridecodebin`、`uridecodebin3`)等 [^2]。这些插件在 UNIX 环境下能够动态加载并扩展框架功能。 此外,某些分布式系统工具(如 Redis)虽然不直接提供 UNIX 插件,但可以通过命令或脚本扩展功能 [^3]。例如,Redis 的分布式锁机制可以通过 `SETNX` 或 `SET` 命令实现,并结合过期时间(`PX` 参数)确保锁的安全性 [^3]。 以下是一些常见的 UNIX 插件或扩展示例: 1. **数据库插件**:如 InnoDB 插件的 `ha_innodb_plugin.so`。 2. **多媒体框架插件**:如 GStreamer 的 `gstplaybackplugin.c`,其中定义了多个媒体处理元素。 3. **分布式系统工具**:如 Redis 的分布式锁实现,基于 `SET` 命令的扩展功能。 ### 示例代码:GStreamer 插件注册 以下是 GStreamer 插件初始化的一个简化示例,展示如何在 UNIX 系统中注册多个元素: ```c static gboolean plugin_init (GstPlugin * plugin) { gboolean res = FALSE; res |= GST_ELEMENT_REGISTER (playbin, plugin); res |= GST_ELEMENT_REGISTER (decodebin, plugin); res |= GST_ELEMENT_REGISTER (uridecodebin, plugin); return res; } ``` 此代码片段展示了如何在 UNIX 环境下通过共享对象库扩展 GStreamer 的功能 [^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值