UVA 753 UNIX 插头(EK网络流+Floyd传递闭包)
UNIX 插头
紫书P374
【题目链接】UNIX 插头
【题目类型】EK网络流+Floyd传递闭包
&题解:
看了书之后有那么一点懂了,但当看了刘汝佳代码后就完全明白了,感觉他代码写的好牛逼啊,Orz
所以就完全照着码了一份。
【时间复杂度】O(\(n^3\))
&代码:
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
const int INF = 1000000000;
const int maxn= 400+9;
vector<string> names;
int ID(const string& s){
for(int i=0;i<names.size();i++)
if (names[i]==s) return i;
names.push_back(s);
return names.size()-1;
}
struct Edge{
int from,to,cap,flow;
};
struct EK{
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
int a[maxn],p[maxn];
void Init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
int MaxFlow(int s,int t){
int flow=0;
for(;;){
memset(a,0,sizeof(a));
queue<int> Q;
Q.push(s);
a[s]=INF;
while(!Q.empty()){
int x=Q.front(); Q.pop();
for(int i=0;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if (!a[e.to]&&e.cap>e.flow){
p[e.to]=G[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
Q.push(e.to);
}
}
if (a[t]) break;
}
if (!a[t]) break;
for(int u=t;u!=s;u=edges[p[u]].from){
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
flow+=a[t];
}
return flow;
}
};
EK g;
int n,m,k;
int d[maxn][maxn],target[maxn],device[maxn];
int ka=0;
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out","w",stdout);
#endif
string s1,s2;
int o; cin>>o;
while(o--){
if (ka)cout<<endl; ka=1;
names.clear();
cin>>n;
for(int i=0;i<n;i++){
cin>>s1;
target[i]=ID(s1);
}
cin>>m;
for(int i=0;i<m;i++){
cin>>s1>>s2;
device[i]=ID(s2);
}
memset(d,0,sizeof(d));
cin>>k;
for(int i=0;i<k;i++){
cin>>s1>>s2;
d[ID(s1)][ID(s2)]=1;
}
int V=names.size();
for(int k=0;k<V;k++)
for (int i=0;i<V;i++)
for (int j=0;j<V;j++)
d[i][j]|=d[i][k]&&d[k][j];
g.Init(V+2);
for(int i=0;i<m;i++)
g.AddEdge(V,device[i],1);
for(int i=0;i<n;i++)
g.AddEdge(target[i],V+1,1);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if (d[device[i]][target[j]]) g.AddEdge(device[i],target[j],INF);
int r=g.MaxFlow(V,V+1);
cout<<m-r<<endl;
}
return 0;
}
UVA 753 UNIX 插头(EK网络流+Floyd传递闭包)的更多相关文章
- 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 ...
- UVA 247 电话圈 (floyd传递闭包 + dfs输出连通分量的点)
题意:输出所有的环: 思路:数据比较小,用三层循环的floyd传递闭包(即两条路通为1,不通为0,如果在一个环中,环中的所有点能互相连通),输出路径用dfs,递归还没有出现过的点(vis),输出并递归 ...
- UVA 247 电话圈(Floyd传递闭包+输出连通分量)
电话圈 紫书P365 [题目链接]电话圈 [题目类型]Floyd传递闭包+输出连通分量 &题解: 原来floyd还可以这么用,再配合连通分量,简直牛逼. 我发现其实求联通分量也不难,就是for ...
- POJ 3660 Cow ContestCow(Floyd传递闭包)题解
题意:给出m个关系,问你能确定机头牛的排名 思路:要确定排名那必须要把他和其他n-1头牛比过才行,所以Floyd传递闭包,如果赢的+输的有n-1就能确定排名. 代码: #include<cstd ...
- nyoj 211——Cow Contest——————【floyd传递闭包】
Cow Contest 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...
- POJ3660:Cow Contest(Floyd传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16941 Accepted: 9447 题目链接 ...
- POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
Treasure Exploration Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- POJ 3660—— Cow Contest——————【Floyd传递闭包】
Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比赛:floyd传递闭包
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1612 题意: 有n头牛比赛. 告诉你m组(a,b),表示牛a成绩比牛b高. 保证排名没有并 ...
随机推荐
- C#窗体 WinForm 文件操作
文件及文件夹操作 C/S:WinForm可以操作客户端文件 Client ServerB/S:浏览器服务 Brower Server 命名空间:using system .IO; 1. File类:文 ...
- oracle数据库的乱码问题解决方案
我的电脑-----高级系统设置----高级-----环境变量 LANG=zh_CN.GBK NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK
- codeforces 451E Devu and Flowers
题意:有n个瓶子每个瓶子有 f[i] 支相同的颜色的花(不同瓶子颜色不同,相同瓶子花视为相同) 问要取出s支花有多少种不同方案. 思路: 如果每个瓶子的花有无穷多.那么这个问题可以转化为 s支花分到 ...
- android:LayoutInflater
LayoutInflater:一般用于查找res/layout下的布局文件,findViewById()一般是用于查找布局下的各种控件 一般:我们使用LayoutInflater.from(conte ...
- priority_queue 优先队列用法
//采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...
- 福州月赛2057 DFS
题意:告诉你族谱,然后Q条查询s和t的关系,妈妈输出M,爸爸输出F: 题目地址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=78233# ...
- iOS真机UI调试利器——Reveal
做iOS的开发,UI是非常非常重要的一环.调试时我们一般用模拟器,提交前用真机做测试.用模拟器来调试UI效果虽然快捷方便,但有时仍然希望有更强大 的工具来帮助分析UI,尤其是专注在UI的效果调试时.最 ...
- REDIS key notification
Commands Clients Documentation Community Download Support License Join us in London October 19th for ...
- WAMPSERVER多站点配置
1.配置wamp网站地址: 找到wamp的安装目录,如~\wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf 打开httpd-vhosts ...
- 关于kindeditor中点击图片后,滚动条往上顶的bug
比如现在我插入两张图片, 无论我点击哪张图片,里边的滚动条都会往上顶. 本来以为往上会有解决方法,一查结果没有:然后想着去官网查查,然而什么都没有,想到官网提交这个bug,结果没地方提交. 怎么解决, ...