读题比做题难系列……

poj1087

输入n,代表插座个数,接下来分别输入n个插座,字母表示。把插座看做最大流源点,连接到一个点做最大源点,流量为1。

输入m,代表电器个数,接下来分别输入m个电器,字符串表示。把电器看做最大流终点,连接到一个点做最大汇点,流量为1。

输入k,代表转换器个数,接下来分别输入k个转换器,每个插座输入两个字母a,b表示a可以连在b上。把转换器看做流,b->a,因为转换器无限提供,流量为无限大

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <algorithm>
#include <string> using namespace std; const int N = 505;
const int INF = 0x7fffffff; map<string, int>mymap; int cap[N][N];
string str[N];
int flow[N];
int pre[N]; queue<int> q; int BFS(int src, int des)
{
int i;
while (!q.empty()) q.pop();
for (i = 1; i <= des; ++i) pre[i] = -1;
pre[src] = 0;
q.push(src);
flow[src] = INF;
while (!q.empty()) {
int index = q.front();
q.pop();
if (index == des) break;
for (i = 1; i <= des; ++i) {
if (pre[i] == -1 && cap[index][i] > 0) {
pre[i] = index;
flow[i] = min(flow[index], cap[index][i]);
q.push(i);
}
}
}
if (pre[des] == -1) return -1;
return flow[des];
} int maxFlow(int src, int des)
{
int ans = 0;
int in = 0;
while ((in = BFS(src, des)) != -1) {
int k = des;
while (k != src) {
int last = pre[k];
cap[last][k] -= in;
cap[k][last] += in;
k = last;
}
ans += in;
}
return ans;
} int main()
{
int n, m, k, i;
int cnt = 1;
string str1,str2;
cin >> n;
for (i = 1; i <= n; ++i) {
cin >> str1;
mymap[str1] = ++cnt;
cap[1][cnt] = 1;
}
cin >> m;
for (i = 1; i <= m; ++i) {
cin >> str[i] >> str2;
if (!mymap[str[i]]) mymap[str[i]] = ++cnt;
if (!mymap[str2]) mymap[str2] = ++cnt;
cap[mymap[str2]][mymap[str[i]]] = 1; }
cin >> k;
for (i = 1; i <= k; ++i) {
cin >> str1 >> str2;
if (!mymap[str1]) mymap[str1] = ++cnt;
if (!mymap[str2]) mymap[str2] = ++cnt;
cap[mymap[str2]][mymap[str1]] = INF;
}
++cnt;
for (i = 1; i <= m; ++i) {
cap[mymap[str[i]]][cnt] = 1;
}
cout << m - maxFlow(1, cnt);
return 0;
}

  

poj 1459

配个良心的地址,读起题来容易些:http://www.2cto.com/kf/201502/377406.html

方法和上题相同,最大源点连到发电站,流量就是发电站的最大电量,用户同理。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; const int N = 105;
const int INF = 0x7fffffff; int n, np, nc, m; int flow[N];
int cap[N][N];
int pre[N]; queue<int>q; int BFS(int src, int des)
{
while (!q.empty()) {
q.pop();
}
for (int i = 0; i <= des; ++i) {
pre[i] = -1;
}
flow[src] = INF;
pre[src] = 0;
q.push(src);
while (!q.empty()) {
int index = q.front();
q.pop();
if (index == des) {
break;
}
for (int i = 1; i <= des; ++i) {
if (pre[i] == -1 && cap[index][i] > 0) {
pre[i] = index;
flow[i] = min(cap[index][i], flow[index]);
q.push(i); //<---经常忘记这句!
}
}
}
if (pre[des] == -1) {
return -1;
}
return flow[des];
} int maxFlow(int src, int des)
{
int ans = 0;
int in = 0;
while ((in = BFS(src, des)) != -1) {
int k = des;
while (k != src) {
int last = pre[k];
cap[last][k] -= in;
cap[k][last] += in;
k = last;
}
ans += in;
}
return ans;
} int main()
{
int i;
while (scanf("%d%d%d%d", &n, &np, &nc, &m) != EOF) {
memset(cap, 0, sizeof cap);
memset(flow, 0, sizeof flow);
int u, v, z; for (i = 0; i < m; ++i) {
scanf(" (%d,%d)%d", &u, &v, &z);
cap[u + 1][v + 1] += z;
}
for (i = 0; i < np; ++i) { // power station
scanf(" (%d)%d", &u, &z);
cap[0][u + 1] = z;
}
for (i = 0; i < nc; ++i) { // consumer
scanf(" (%d)%d", &u, &z);
cap[u + 1][n + 1] = z;
}
printf("%d\n", maxFlow(0, n + 1));
}
return 0;
}

不明白为什么scanf的格式匹配会死循环,大概是回车的问题= =,加个空格就好了。

poj1087 A Plug for UNIX & poj1459 Power Network (最大流)的更多相关文章

  1. POJ1459 Power Network —— 最大流

    题目链接:https://vjudge.net/problem/POJ-1459 Power Network Time Limit: 2000MS   Memory Limit: 32768K Tot ...

  2. poj1459 Power Network --- 最大流 EK/dinic

    求从电站->调度站->消费者的最大流,给出一些边上的容量.和电站和消费者能够输入和输出的最大量. 加入一个超级源点和汇点,建边跑模板就能够了. 两个模板逗能够. #include < ...

  3. POJ1087 A Plug for UNIX —— 最大流

    题目链接:https://vjudge.net/problem/POJ-1087 A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K T ...

  4. POJ1087 A Plug for UNIX(网络流)

                                       A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. POJ1087:A Plug for UNIX(最大流)

    A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...

  6. poj1459 Power Network (多源多汇最大流)

    Description A power network consists of nodes (power stations, consumers and dispatchers) connected ...

  7. POJ1459 Power Network(网络最大流)

                                         Power Network Time Limit: 2000MS   Memory Limit: 32768K Total S ...

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

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

  9. POJ1459 - Power Network

    原题链接 题意简述 原题看了好几遍才看懂- 给出一个个点,条边的有向图.个点中有个源点,个汇点,每个源点和汇点都有流出上限和流入上限.求最大流. 题解 建一个真 · 源点和一个真 · 汇点.真 · 源 ...

随机推荐

  1. 正则表达式中的\n

    搜索文件中的字符,希望每次从每行的开始进行匹配. 所以在表达式开头加了\n 结果发现怎么都匹配不了. string regEx = @"\n\d*\s*!\s*TESTNAME” 最后,偶然 ...

  2. firefox浏览器删除插件

    打开注册表编辑器([开始菜单]→[运行]或 Win+R快捷键打开运行,输入regedit). 2 点击菜单栏[编辑]→[搜索],或者按 Ctrl + F 快捷键,搜索 MozillaPlugins . ...

  3. c++中的static关键字

    1.在函数体中,一个被声明为静态的变量在这一函数被调用的过程中维持其值不变. 2.在模块内(但在函数外),比如在某一个C源文件内,一个被声明为静态的变量可以被该模块内的所有函数调用,但不能被模块外的函 ...

  4. 图片上没有line-height垂直居中

    <style> div {     width: 150px;     height: 155px;     line-height: 155px;     border: 1px sol ...

  5. ZOJ 2110 Tempter of the Bone(DFS)

    点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...

  6. javaweb学习总结(四十三)——Filter高级开发

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  7. Java集合类之栈Stack

    package com.test; import java.util.*; public class Demo7_3 { public static void main(String[] args) ...

  8. bzoj3514

    好题+数据结构神题+感人肺腑pascal被卡系列,我下面的代码几乎写到最优可怎耐bzoj上pascal开的是O1,c++开的是O2,这怎么可能跑得过!!!还是说说方法吧,这是一道算贡献的好题,因为我们 ...

  9. MFC框架

    第一点:类别型录网的搭建: 类别型录网搭建的目的是为了实现所谓的"执行期类型识别",也就是在程序运行的时候识别出某个对象是否是某个类的实例(基类也可以).这里还不是很明白为什么需要 ...

  10. CI 在nginx中出现404错误的解决方式

    因为你的nginx配置的是截取.php文件后缀的访问转发到PHP-CGI,而index.php和index.php/是不一样的.. 你在nginx里面写一句: if (!-e $request_fil ...