题意:给定一些插座和一些插头,还有一些单向接头,比如A->B

接头可以串联A->B->C->D

使得插座和插头匹配数目最大

题解:

首先接头可以用Floyd处理

这题可以转化为二分图的模型,就是直接连边,不做处理

 #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#define MAXN 505
#define INF 0x7f7f7f7f
#define ll long long
#define P 203
using namespace std;
int n,m,e,N;
int V1[MAXN],V2[MAXN];
int G[MAXN<<][MAXN<<];
map<ll,int> mp;
ll Hash(string s){
ll ret=;
for(string::iterator it=s.begin();it!=s.end();it++){
ret=ret*P+(*it);
}
return ret;
}
struct Edge{
int from,to,cap,flow;
Edge(int u=,int v=,int c=,int f=){
from=u,to=v,cap=c,flow=f;
}
};
struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[MAXN];
int b[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n,int s,int t){
this->n=n;
this->s=s,this->t=t;
edges.clear();
for(int i=;i<=n;i++){
G[i].clear();
}
}
void AddEdge(int x,int y,int cap){
edges.push_back(Edge(x,y,cap,));
edges.push_back(Edge(y,x,,));
m=edges.size();
G[x].push_back(m-);
G[y].push_back(m-);
}
bool BFS(){
memset(b,,sizeof(b));
queue<int> q;
d[s]=;
q.push(s);
b[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(e.cap>e.flow&&!b[e.to]){
d[e.to]=d[x]+;
q.push(e.to);
b[e.to]=;
}
}
}
return b[t];
}
int dfs(int x,int a){
if(x==t||!a)return a;
int flow=,f;
for(int& i=cur[x];i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[e.to]==d[x]+&&(f=dfs(e.to,min(a,e.cap-e.flow)))>){
edges[G[x][i]].flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(!a){
break;
}
}
}
return flow;
}
int Maxflow(){
int flow=;
while(BFS()){
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}D;
void init(){
memset(G,,sizeof(G));
mp.clear();
N=;
ll h;
string t1,t2;
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>t1;
h=Hash(t1);
if(mp.count(h)){
V1[i]=mp[h];
}
else{
N++;
mp[h]=N;
V1[i]=N;
}
}
scanf("%d",&m);
for(int i=;i<=m;i++){
cin>>t2>>t1;
h=Hash(t1);
if(mp.count(h)){
V2[i]=mp[h];
}
else{
N++;
mp[h]=N;
V2[i]=N;
}
}
scanf("%d",&e);
for(int i=;i<=e;i++){
cin>>t1>>t2;
int c1,c2;
h=Hash(t1);
if(mp.count(h)){
c1=mp[h];
}
else{
N++;
mp[h]=N;
c1=N;
}
h=Hash(t2);
if(mp.count(h)){
c2=mp[h];
}
else{
N++;
mp[h]=N;
c2=N;
}
G[c1][c2]=;
}
for(int i=;i<=n;i++){
G[i][i]=;
}
for(int k=;k<=N;k++){
for(int i=;i<=N;i++){
for(int j=;j<=N;j++){
G[i][j]|=(G[i][k]&G[k][j]);
}
}
}
}
void solve(){
D.init(n,,n+m+);
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
if(G[V2[i]][V1[j]]){
D.AddEdge(i,j+m,);
}
}
}
for(int i=;i<=m;i++){
D.AddEdge(,i,);
}
for(int j=;j<=n;j++){
D.AddEdge(j+m,n+m+,);
}
printf("%d\n",m-D.Maxflow());
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
int T;
scanf("%d",&T);
while(T--){
init();
solve();
if(T){
printf("\n");
}
}
return ;
}

二分图匹配

也可以把相同类型的压在一起处理,

源点到某类型插头的容量就是该类型的数目,汇点同理

如果相连的话建一条长度为INF的边,

然后跑一遍最大流即可

 #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#define MAXN 505
#define INF 0x7f7f7f7f
#define ll long long
#define P 203
using namespace std;
int n,m,e,N;
int V1[MAXN],V2[MAXN];
int b1[MAXN],b2[MAXN];
int cnt1,cnt2;
int G[MAXN<<][MAXN<<];
map<string,int> mp;
struct Edge{
int from,to,cap,flow;
Edge(int u=,int v=,int c=,int f=){
from=u,to=v,cap=c,flow=f;
}
};
struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[MAXN];
int b[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n,int s,int t){
this->n=n;
this->s=s,this->t=t;
edges.clear();
for(int i=;i<=n;i++){
G[i].clear();
}
}
void AddEdge(int x,int y,int cap){
edges.push_back(Edge(x,y,cap,));
edges.push_back(Edge(y,x,,));
m=edges.size();
G[x].push_back(m-);
G[y].push_back(m-);
}
bool BFS(){
memset(b,,sizeof(b));
queue<int> q;
d[s]=;
q.push(s);
b[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(e.cap>e.flow&&!b[e.to]){
d[e.to]=d[x]+;
q.push(e.to);
b[e.to]=;
}
}
}
return b[t];
}
int dfs(int x,int a){
if(x==t||!a)return a;
int flow=,f;
for(int& i=cur[x];i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[e.to]==d[x]+&&(f=dfs(e.to,min(a,e.cap-e.flow)))>){
edges[G[x][i]].flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(!a){
break;
}
}
}
return flow;
}
int Maxflow(){
int flow=;
while(BFS()){
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}D;
void init(){
memset(G,,sizeof(G));
memset(b1,,sizeof(b1));
memset(b2,,sizeof(b2));
cnt1=cnt2=;
mp.clear();
N=;
string t1,t2;
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>t1;
if(mp.count(t1)){
V1[i]=mp[t1];
}
else{
N++;
mp[t1]=N;
V1[i]=N;
}
}
scanf("%d",&m);
for(int i=;i<=m;i++){
cin>>t2>>t1;
if(mp.count(t1)){
V2[i]=mp[t1];
}
else{
N++;
mp[t1]=N;
V2[i]=N;
}
}
scanf("%d",&e);
for(int i=;i<=e;i++){
cin>>t1>>t2;
int c1,c2;
if(mp.count(t1)){
c1=mp[t1];
}
else{
N++;
mp[t1]=N;
c1=N;
}
if(mp.count(t2)){
c2=mp[t2];
}
else{
N++;
mp[t2]=N;
c2=N;
}
G[c1][c2]=;
}
for(int i=;i<=n;i++){
G[i][i]=;
}
for(int k=;k<=N;k++){
for(int i=;i<=N;i++){
for(int j=;j<=N;j++){
G[i][j]|=(G[i][k]&G[k][j]);
}
}
}
for(int i=;i<=n;i++){
if(!b1[V1[i]]){
cnt1++;
}
b1[V1[i]]++;
}
for(int i=;i<=m;i++){
if(!b2[V2[i]]){
cnt2++;
}
b2[V2[i]]++;
}
}
void solve(){
D.init(n,,cnt1+cnt2+);
int t1=,t2=cnt1;
for(int i=;i<=N;i++){
if(b2[i]) t2++;
else continue;
t1=;
for(int j=;j<=N;j++){
if(b1[j]){
t1++;
if(G[i][j]){
D.AddEdge(t1,t2,INF);
}
}
}
}
t1=;
for(int i=;i<=N;i++){
if(b1[i]){
t1++;
D.AddEdge(,t1,b1[i]);
}
}
t2=;
for(int j=;j<=N;j++){
if(b2[j]){
t2++;
D.AddEdge(t2+cnt1,cnt1+cnt2+,b2[j]);
}
}
printf("%d\n",m-D.Maxflow());
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
int T;
scanf("%d",&T);
while(T--){
init();
solve();
if(T){
printf("\n");
}
}
return ;
}

压点最大流

UVA753:A Plug for UNIX的更多相关文章

  1. 【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 ...

  2. 【uva753/poj1087/hdu1526-A Plug for UNIX】最大流

    题意:给定n个插座,m个插头,k个转换器(x,y),转换器可以让插头x转成插头y.问最少有多少个插头被剩下. 题解: 最大流或者二分图匹配.然而我不知道怎么打二分图匹配..打了最大流.这题字符串比较坑 ...

  3. uva753 A Plug for UNIX 网络流最大流

    C - A Plug for UNIX    You are in charge of setting up the press room for the inaugural meeting of t ...

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

  5. UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)

    解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...

  6. poj 1087 C - A Plug for UNIX 网络流最大流

    C - A Plug for UNIXTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  7. UVA 753 - A Plug for UNIX(网络流)

      A Plug for UNIX  You are in charge of setting up the press room for the inaugural meeting of the U ...

  8. POJ1087 A Plug for UNIX 【最大流】

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13855   Accepted: 4635 ...

  9. POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)

    POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...

随机推荐

  1. SaaS的那些事儿

    前两年...   大一大二期间,不知道软件架构.云服务器.数据库为何物,偶尔听过却从未用过.天天学的写的东西都是一些命令行代码,所幸在学完<数据结构>和<算法导论>后能够独立实 ...

  2. C++中文件的读写

    C++中文件的读写 在C++中如何实现文件的读写? 一.ASCII 输出 为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstrea ...

  3. 深入浅出 SSL 管理配置实战

    我们生活在一个信息大爆炸的时代,几乎每天都在和互联网打交道,购物.网银转账.支付宝付款.搜索信息.查看邮件.观看视频.微信聊天.上网冲浪.阅读新闻等,无不时时刻刻在和网络打交道.那如何保护网络安全就相 ...

  4. 利用java反射读写csv中的数据

      前一段有个需求需要将从数据库读取到的信息保存到csv文件中,在实现该需求的时候发现资料比较少,经过收集反射和csv相关资料,最终得到了如下程序.  1.在使用java反射读取csv文件数据时,先通 ...

  5. Ajax 的onreadystatechange事件注意事项.

    <script type="text/javascript"> function createXHR() { var request = false; try { re ...

  6. JAVA_SE基础——32.this关键字调用本类的构造方法

    黑马程序员入学blog... 也算是学习笔记. 下面我们来看段代码: package day07; class Student{ int id; //身份证 String name; //名字 pub ...

  7. LeetCode & Q13-Roman to Integer-Easy

    Math String Description: Given a roman numeral, convert it to an integer. Input is guaranteed to be ...

  8. New UWP Community Toolkit - RotatorTile

    概述 UWP Community Toolkit  中有一个为图片或磁贴提供轮播效果的控件 - RotatorTile,本篇我们结合代码详细讲解  RotatorTile 的实现. RotatorTi ...

  9. C# HttpClient设置cookies的两种办法 (转发)

    一般有两种办法 第一种handler.UseCookies=true(默认为true),默认的会自己带上cookies,例如 var handler = new HttpClientHandler() ...

  10. iot:下一步要做的工作

    1.DeviceMessage抽象(定义&支持扩展)2.createDeviceMessage.analyseDeviceMessage(支持扩展)3.日志打印4.错误处理5.断线重连6.交互 ...