POJ1087 A Plug of UNIX
你作为某高管去住宿了,然后宾馆里有几种插座,分别有其对应型号,你携带了几种用电器(手机,电脑一类的),也有其对应型号;可是不一定用电器就能和插座匹配上,于是宾馆的商店里提供了一些转换器,这些转换器可以将某一型号电源转换成另一型号的。问,你的用电器最少会有多少种无法充电
源点向电器连边,容量为1,电器向对应的插座连边,容量为1,对于转换器,在插座与插座之间连边,容量为inf,所有插座向汇点连边,容量为插座的个数~
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#include<iostream>
#include<string>
using namespace std;
const int maxn=;
const int inf=1e9;
queue<int> q;
int n;
int g[maxn][maxn];
int pre[maxn];
int flow[maxn];
int maxflow;
int bfs (int s,int t) {
while (!q.empty()) q.pop();
for (int i=;i<=n;i++) pre[i]=-;
pre[s]=;
q.push(s);
flow[s]=inf;
while (!q.empty()) {
int x=q.front();
q.pop();
if (x==t) break;
for (int i=;i<=n;i++)
if (g[x][i]>&&pre[i]==-) {
pre[i]=x;
flow[i]=min(flow[x],g[x][i]);
q.push(i);
}
}
if (pre[t]==-) return -;
else return flow[t];
}
void Edmonds_Karp (int s,int t) {
int increase=;
while ((increase=bfs(s,t))!=-) {
int k=t;
while (k!=s) {
int last=pre[k];
g[last][k]-=increase;
g[k][last]+=increase;
k=last;
}
maxflow+=increase;
}
}
map<string,int> pos;
int main () {
string s1,s2;
int N,M,cnt,st,ed;
while (~scanf("%d",&N)) {
pos.clear();
memset(g,,sizeof(g));
maxflow=;
st=;
ed=;
cnt=;
while (N--) {
cin>>s1;
pos[s1]=cnt;
g[][cnt++]=;
}
scanf ("%d",&M);
for (int i=;i<M;i++) {
cin>>s1>>s2;
if (pos[s1]==) pos[s1]=cnt++;
if (pos[s2]==) pos[s2]=cnt++;
g[pos[s1]][ed]=;
g[pos[s2]][pos[s1]]=;
}
scanf ("%d",&N);
while (N--) {
cin>>s1>>s2;
if (pos[s1]==) pos[s1]=cnt++;
if (pos[s2]==) pos[s2]=cnt++;
g[pos[s2]][pos[s1]]=inf;
}
n=cnt-;
Edmonds_Karp (st,ed);
printf ("%d\n",M-maxflow);
}
return ;
}
POJ1087 A Plug of UNIX的更多相关文章
- POJ1087 A Plug for UNIX —— 最大流
题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K T ...
- POJ1087:A Plug for UNIX(最大流)
A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...
- POJ1087 A Plug for UNIX(网络流)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ1087 A Plug for UNIX 【最大流】
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13855 Accepted: 4635 ...
- 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 ...
- [POJ1087]A Plug for UNIX
题目描述 Description You are in charge of setting up the press room for the inaugural meeting of the Uni ...
- poj1087 A Plug for UNIX(网络流最大流)
http://poj.org/problem?id=1087 好久没遇见过这么坑的题了这个题真是挫的够可以的.题目大意:你作为某高管去住宿了,然后宾馆里有几种插座,分别有其对应型号,你携带了几种用电器 ...
- poj1087 A Plug for UNIX & poj1459 Power Network (最大流)
读题比做题难系列…… poj1087 输入n,代表插座个数,接下来分别输入n个插座,字母表示.把插座看做最大流源点,连接到一个点做最大源点,流量为1. 输入m,代表电器个数,接下来分别输入m个电器,字 ...
- 【uva753/poj1087/hdu1526-A Plug for UNIX】最大流
题意:给定n个插座,m个插头,k个转换器(x,y),转换器可以让插头x转成插头y.问最少有多少个插头被剩下. 题解: 最大流或者二分图匹配.然而我不知道怎么打二分图匹配..打了最大流.这题字符串比较坑 ...
- POJ-1087 A Plug for UNIX (网络流)
思路 电器数1 ~ 100,附带100种接口,注意题目:You notice that some of the devices use plugs for which there is no rece ...
随机推荐
- Linux - Shell - #!/bin/bash
概述 简单解释一下 shell 脚本卡头的 #!/bin/bash 水一篇, 少一篇 背景 shell 脚本中的注释 通常是 以# 卡头的行 但是有时候执行 shell 的时候, 会有这种内容 #!/ ...
- 【C语言】指针函数例子
#include<stdio.h> char* getword(char); char* getword(char c) { switch (c) { case'A':return&quo ...
- jsp页面直接读取mysql数据库数据显示
jsp页面直接读取mysql数据库数据显示: <%@page import="java.sql.ResultSet"%> <%@page import=" ...
- 【转载】Java多线程
转自:http://www.jianshu.com/p/40d4c7aebd66 引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目 ...
- c#中的栈(stack)与队列(queue)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 计算机网络 - TCP_NODELAY 和 TCP_CORK, TCP_NOPUSH
参考 https://www.cnblogs.com/biyeymyhjob/p/4670502.html https://stackoverflow.com/questions/3761276/wh ...
- 【SIKI学院】愤怒的小鸟创建过程-1
第一讲:资源导入,场景的简单搭建 1.创建一个2D工程,用到了3个场景,因此ctrl +S创建场景00-level,ctrl+N保存在你创建的文件夹中,这样一个文件就创建好了,之后继续重复此步骤创建另 ...
- Bugku-CTF分析篇-抓到一只苍蝇(在哪?here!卧槽?!好大一坨苍蝇。)
抓到一只苍蝇 抓到一只苍蝇 本题要点:pcapng包导出文件.合并连续的pcapng包.rar文件头.binwalk基本使用.foremost安装及使用 下载完成后,发现有这样 ...
- PHP基础学习笔记2
一.数组 1.1 数值数组 1)自动分配 ID 键 ) ); ?> 即二维数组: 100 100 96 20 60 59 40 110 100 上面创建的二维数组自动分配ID键:下面以指定键的方 ...
- BZOJ 3262: 陌上花开 (cdq分治,三维偏序)
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; c ...