POJ 1087 —— A Plug for UNIX

原题:http://poj.org/problem?id=1087

题意:n个插座,m个电器及其对应的插座,k种转化器,转换器(u,v)表示可以把原本需要u插座的电器转接到v插座上,问最少有多少设备没有插座用,每种转换器数量不限;;

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>
#define inf 1e9
using namespace std;
const int maxn = 1500;
const int maxm = 5500;
int n, m, k;
int num_nodes;
int a[30];
map<string, int>mp;

struct Edge
{
    int from, to, flow, cap;
}edge[maxm*2];  

vector<int>G[maxn];
int edgenum;
void add(int u, int v, int c)
{
    edge[edgenum].from = u;
    edge[edgenum].to = v;
    edge[edgenum].flow = 0;
    edge[edgenum].cap = c;
    edgenum++;  

    edge[edgenum].from = v;
    edge[edgenum].to = u;
    edge[edgenum].flow = 0;
    edge[edgenum].cap = 0;
    edgenum++;  

    G[u].push_back(edgenum-2);
    G[v].push_back(edgenum-1);
}  

int deep[maxn];
bool vis[maxn];
void BFS(int s, int t)
{
    queue<int>Q;
    memset(vis, false, sizeof vis);
    Q.push(t);
    vis[t] = true;
    deep[t] = 0;
    while(!Q.empty())
    {
        int now = Q.front();
        Q.pop();
        for(int i = 0;i<(int)G[now].size();i++)
        {
            int v = edge[G[now][i]].to;
            if(!vis[v])
			{
                deep[v] = deep[now] + 1;
                vis[v] = true;
                Q.push(v);
            }
        }
    }
}  

int gap[maxn];
int cur[maxn];
int front[maxn];
int Augment(int s, int t)
{
    int minflow = inf;
    int begin = t;
    while(begin != s)
    {
        Edge& e = edge[front[begin]];
        minflow = min(minflow, e.cap - e.flow);
        begin = e.from;
    }  

    begin = t;
    while(begin != s)
    {
        edge[front[begin]].flow += minflow;
        edge[front[begin]^1].flow -= minflow;
        begin = edge[front[begin]].from;
    }
    return minflow;
}  

int Maxflow(int s, int t)
{
    int flow = 0;
    BFS(s, t);
    memset(gap, 0, sizeof gap);
    memset(cur, 0, sizeof cur);
    for(int i = 0;i<num_nodes;i++)  gap[deep[i]]++;
    int begin = s;
    while(deep[s] < num_nodes)
    {
        if(begin == t)
		{
            flow += Augment(s, t);
            begin = s;
        }
        bool flag = false;
        for(int i = cur[begin];i<(int)G[begin].size();i++)
        {
            Edge& e = edge[G[begin][i]];
            if(e.cap > e.flow && deep[begin] == deep[e.to] + 1)
			{
                front[e.to] = G[begin][i];
                cur[begin] = i;
                flag = true;
                begin = e.to;
                break;
            }
        }
        if(!flag)
        {
            int k = num_nodes-1;
            for(int i = 0;i<(int)G[begin].size();i++)
			{
                Edge& e = edge[G[begin][i]];
                if(e.cap > e.flow)
                    k = min(k, deep[e.to]);
            }
            if(--gap[deep[begin]] == 0) break;
            gap[deep[begin] = k+1]++;
            cur[begin] = 0;
            if(begin != s)
                begin = edge[front[begin]].from;
        }
    }
    return flow;
}  

void init()
{
    for(int i = 0;i<num_nodes+2;i++) G[i].clear();
    edgenum = 0;
    memset(deep, 0, sizeof deep);
}

int cnt = 0;
int Id(char s[])
{
	if(mp.find(s) == mp.end())
		mp[s] = ++cnt;
	return mp[s];
}

int main()
{
	int s = 0, t = 400;
	num_nodes = t+1;
	init();
	scanf("%d", &n);
	char c[5];
	mp.clear();
	for(int i = 1;i<=n;i++)
	{
		scanf("%s", c);
		add(Id(c), t, 1);
	}
	scanf("%d", &m);
	char thing[30];
	for(int i = 1;i<=m;i++)
	{
		scanf("%s%s", thing, c);
		add(s, i+n, 1);
		add(i+n, Id(c), 1);
	}
	scanf("%d", &k);
	char u[5], v[5];
	for(int i = 1;i<=k;i++)
	{

		scanf("%s%s", u, v);
		add(Id(u), Id(v), inf);
	}
	int flow = Maxflow(s, t);
	printf("%d\n", m-flow);
	return 0;
}
时间: 2024-08-26 14:34:35

POJ 1087 —— A Plug for UNIX的相关文章

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

POJ 1087 A Plug for UNIX 链接:http://poj.org/problem?id=1087 题意:有n(1≤n≤100)个插座,每个插座用一个数字字母式字符串描述(至多有24 个字符).有m(1≤m≤100)个设备,每个设备有名称,以及它使用的插头的名称:插头的名称跟它所使用的插座的名称是一样的:设备名称是一个至多包含24 个字母数字式字符的字符串:任何两个设备的名称都不同:有k(1≤k≤100)个转换器,每个转换器能将插座转换成插头. 样例: 4 A B C D 5

POJ 1087 A Plug for UNIX(网络流之最大流)

题目地址:POJ 1087 不知道是谁把这题化为了二分最大匹配的专题里..于是也没多想就按照二分图的模型来建的(虽然当时觉得有点不大对...).后来发现二分最大匹配显然不行..有权值..直接来个最大流多方便..然后一直WA..后来仔细想了想..这根本就不能建二分图啊....这题跟二分图一点关系都没有.... 这题的建图思路是让源点与每一个设备的插座类型连边,让汇点与每一个插座连边.然后用floyd判断该设备能否通过转换转换成可以插的插座上.只要可以转换成的就连边,权值为INF.然后求一次最大流,

POJ 1087 A Plug for UNIX 会议室插座问题 构图+最大流

题目链接:POJ 1087 A Plug for UNIX A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13809   Accepted: 4623 Description You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXec

POJ 1087 A Plug for UNIX (最大流)

A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K       Description 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

poj 1087 A Plug for UNIX(字符串编号建图)

A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14862   Accepted: 5026 Description You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an int

poj 1087 A Plug for UNIX 【最大流】

题目连接:http://poj.org/problem?id=1087 题意: n种插座 ,m个电器,f组(x,y)表示插座x可以替换插座y,问你最多能给几个电器充电. 解法:起点向插座建边,容量1,电器向汇点建边,容量1,插座向电器建边,容量1,可以替换的插座间建边,容量无穷大.然后套板子...求最大流. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h>

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

Description 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

POJ 1087 A Plug for UNIX

网络最大流水题 #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include<queue> #include<map> #include<algorithm> using namespace std; const int maxn=1000+10; const int INF=0x7FF

poj 1087.A Plug for UNIX 解题报告

网络流,关键在建图 建图思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化). 2.层次出现断层,无法得到新流(重要). 时间复杂度(m*n^2) */ #include <iostream> #include <cstdio> #include <cstring> #include <map> #define ms(a,b) mem