poj1087 A Plug for UNIX & poj1459 Power Network (最大流)
读题比做题难系列……
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 (最大流)的更多相关文章
- POJ1459 Power Network —— 最大流
题目链接:https://vjudge.net/problem/POJ-1459 Power Network Time Limit: 2000MS Memory Limit: 32768K Tot ...
- poj1459 Power Network --- 最大流 EK/dinic
求从电站->调度站->消费者的最大流,给出一些边上的容量.和电站和消费者能够输入和输出的最大量. 加入一个超级源点和汇点,建边跑模板就能够了. 两个模板逗能够. #include < ...
- 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 Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ1087:A Plug for UNIX(最大流)
A Plug for UNIX 题目链接:https://vjudge.net/problem/POJ-1087 Description: You are in charge of setting u ...
- poj1459 Power Network (多源多汇最大流)
Description A power network consists of nodes (power stations, consumers and dispatchers) connected ...
- POJ1459 Power Network(网络最大流)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total S ...
- POJ1087 A Plug for UNIX 【最大流】
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13855 Accepted: 4635 ...
- POJ1459 - Power Network
原题链接 题意简述 原题看了好几遍才看懂- 给出一个个点,条边的有向图.个点中有个源点,个汇点,每个源点和汇点都有流出上限和流入上限.求最大流. 题解 建一个真 · 源点和一个真 · 汇点.真 · 源 ...
随机推荐
- 数组有N+M个数字, 数字的范围为1 ... N, 打印重复的元素, 要求O(M + N), 不可以用额外的空间
数组有N+M个数字, 数字的范围为1 ... N, 打印重复的元素, 要求O(M + N), 不可以用额外的空间 1.题目中要求我们不能使用额外的空间,那么我们能采用在原数组上做文章,这里的重点是如何 ...
- cocos2dx Tab选项卡控件的实现
选项卡控件在游戏和应用中很是常见,但是cocostudio里并没有实现好的选项卡控件,于是自己封装了 一个,效果如下: 代码: TabUiControl.h #pragma once //std #i ...
- js和Jquery获取选中select值和文本
<body> <select name="PaymentType" style="width:110px" > <option v ...
- Firefly框架参考
在游戏服务器端,往往需要处理大量的各种各样的任务,每一项任务所需的系统资源也可能不同.而这些复杂的任务只用一个单独的服务器进程是很难支撑和管理起来的.所以,游戏服务器端的开发者往往需要花费大量的时间精 ...
- [Gauss]POJ1222 EXTENDED LIGHTS OUT
题意:给一个5*6的矩阵 1代表该位置的灯亮着, 0代表该位置的灯没亮 按某个位置的开关,可以同时改变 该位置 以及 该位置上方.下方.左方.右方, 共五个位置的灯的开.关(1->0, 0-&g ...
- Motion on Ubuntu
Motion is a program that monitors the video signal from one or more cameras and is able to detect if ...
- Mpeg-2的同步及时间恢复--STC,PCR,DTS,PTS
http://blog.csdn.net/hice1226/article/details/6717354 Mpeg-2的同步及时间恢复--STC,PCR,DTS,PTS 摘要:Mpeg-2同步及时间 ...
- 133. Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- [Quick-x]制作新手引导高亮区域方法之一:混合模式
demo下载:https://github.com/chenquanjun/Quick-x-HighlightArea 1.混合模式 (1)首先创建一个全屏的CCRenderTexture实例 这里使 ...
- VisualC#数据库高级教程文档分享
这一节我们演示下怎样使用VS2010创建与发布MVC3建立的网站.使用VS2010创建MVC3.0网站,需要下载MVC3.0的安装包,这个大家可以去网络上下载. 1.项目创建 ...