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


题意:

有n个不同型号的插座(每种型号只有1个),m个不同的用电器且有着对应的插座型号(不同用电器对应的插座型号可能相同),同时有k种插座替代关系,例如A型号插座可以替代B型号插座(但B不一定能替代A)(插座型号和用电器均用字符串表示)。
问最少有多少用电器没有插座用?

输入一个整数n,接下来n行为现有的插座型号;
再输入一个整数m,接下来m行为用电器和插座的对应关系(用电器 对应插座);
在输入一个整数k,接下来k行为插座间的替代关系(A B 表示 B可以代替A)。


分析:

网络流首先就是要建图了,以样例输入为例,可以得到以下的图。(仅标出了正向边,反向边均为0)
在这里插入图片描述
这个要注意的点(坑)挺多的;

  1. 首先要明确现有的插座只有一开始输入的那n种插座,并且每种只有1个;
  2. 在后续输入的对应和替代关系中,出现的插座都不一定是现有的;
  3. 替换关系是单向的,但是是可以传递的,例如输入了 X A 和 B X,同时说明A可以替代B,而且可能会存在多个个可以替代B的,所以替代关系的正向边的容量应当为INF
  4. 因为不知道一共会有多少不同型号的插座,所以不便于按顺序编号,于是可以使用map直接将字符串映射为编号,然后建图。
  5. 注意数组大小,稍微开大些。

建完图以后就是套最大流的板子了。


以下代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
#define LL long long
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e3+50;
const int maxm=1e5+50;
struct edge
{
    int w;
    int to;
    int next;
}e[maxm];
int head[maxn],cnt;
int s=0,t=maxn-1;
int n,m,k;
void addedge(int u,int v,int w)
{
    e[cnt].w=w;
    e[cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
    cnt++;
}
int dis[maxn];     //dis数组记录层次
bool bfs()         //利用BFS建立分成图,从而可以多次DFS增广
{
	memset(dis,-1,sizeof(dis));     //初始化dis数组
	queue<int> q;
	q.push(s);
	dis[s]=0;      //源点层次为0
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			int v=e[i].to;
			if(e[i].w>0&&dis[v]==-1)     //可达&&未分层
			{
				dis[v]=dis[u]+1;         //分层
				if(v==t)                 //若到达汇点,则分层结束,返回true
					return true;
				q.push(v);
			}
		}
	}
	return false;  //运行到此处,说明汇点已不可达,返回false
}
int cur[maxn];     //弧优化:cur数组用于记录上一次DFS增广时u已经增广到第几条边,从而优化时间
int dfs(int u,int flow)       //flow代表流入u点的最大流量
{
	if(u==t)
		return flow;          //到达汇点,直接返回flow
	for(int &i=cur[u];i!=-1;i=e[i].next)
	{                         //注意i前面用&引用,这样就可以直接改变cur[u]
		int v=e[i].to;
		if(dis[v]==dis[u]+1&&e[i].w>0)   //v为u的下一层&&可达
		{
			int k=dfs(v,min(flow,e[i].w));
			if(k>0)
			{
				e[i].w-=k;         //正向边-=k
				e[i^1].w+=k;       //反向边+=k
				return k;
			}
		}
	}
	return 0;       //无法继续增广,返回0
}
int dinic()
{
	int ans=0;      //记录总流量
	while(bfs())    //分层
	{
		for(int i=0;i<maxn;i++)    //初始化cur数组,即将head数组赋给cur数组
			 cur[i]=head[i];
		while(int k=dfs(s,INF))    //增广
			ans+=k;
	}
	return ans;
}
int main()
{
    memset(head,-1,sizeof(head));
    cnt=0;
    scanf("%d",&n);
    map<string,int> mp;        //映射编号
    string a,b;
    int index=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        mp[a]=++index;
        addedge(s,index,1);     //连接插座和源点
        addedge(index,s,0);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        cin>>a>>b;
        mp[a]=++index;
        if(!mp.count(b))
            mp[b]=++index;
        addedge(mp[b],mp[a],1);    //连接插座和用电器
        addedge(mp[a],mp[b],0);
        addedge(mp[a],t,1);        //连接用电器和汇点
        addedge(t,mp[a],0);
    }
    scanf("%d",&k);
    for(int i=1;i<=k;i++)
    {
        cin>>a>>b;
        if(!mp.count(a))
            mp[a]=++index;
        if(!mp.count(b))
            mp[b]=++index;
        addedge(mp[b],mp[a],INF);   //建立替代关系
        addedge(mp[a],mp[b],0);
    }
    printf("%d\n",m-dinic());
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值