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
原题链接 题意简述 原题看了好几遍才看懂- 给出一个个点,条边的有向图.个点中有个源点,个汇点,每个源点和汇点都有流出上限和流入上限.求最大流. 题解 建一个真 · 源点和一个真 · 汇点.真 · 源 ...
随机推荐
- E8.Net工作流平台之中国特色
特色之一领导排名有先后 领导排名是有潜规则的,不论是在企业通讯录中,还是企业员工目录中,不管在流程执行过程中,还是存档数据中,当前领导的排名一定要按潜规则展示,不能随便罗列.E8.Net工作流解决了 ...
- MVC-Razor引擎布局
ViewBag.Title:标题 layout: @ViewBag.Title view: @{ViewBag.Title="标题"} @RenderBody():视图的内容直接渲 ...
- Quartz Sheduler 入门
Quartz Sheduler 入门 原文地址:http://www.quartz-scheduler.org/generated/2.2.2/html/qtz-all/ 下载地址:http://qu ...
- Centos安装gnome主菜单编辑器无
首选项---主菜单-- 即是alacarte.. centos ===安装 alacarte.noarch 0:0.12.4-1.el6 即可.
- Discuz CDN优化方案
DZ整体来说CDN是有点蛋疼的,因为毕竟琐碎,貌似大部分帖子都没有说全,这里罗列一下,给在用的孩儿们一点参考: 1.在后台设置CSS/JS走CDN路径,具体[全局]-[性能优化]-[服务器优化] 2. ...
- ul 、ol li 继承原有样式的问题
如: 1.为什么我的服务器无法远程了? 2.为什么我的服务器总是自动重启? 3.为什么我的服务器总是自动重启? 以前写这种类型的列表,都是自己用键盘输入这些数字,其实不然,ul .ol li本身就自带 ...
- 如何设置、查看以及调试core文件
http://blog.csdn.net/xiaoxiaoniaoer1/article/details/7740820 1.core文件的生成开关和大小限制--------------------- ...
- Linux下使用clock_gettime给程序计时
http://www.cnblogs.com/daqiwancheng/archive/2010/07/01/1769522.html
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMapping、model
一.RequestMapping 1.可以写在方法上或类上,且值可以是数组 package spittr.web; import static org.springframework.web.bind ...
- USACO3.25Magic Squares(bfs)
/* ID: shangca2 LANG: C++ TASK: msquare */ #include <iostream> #include<cstdio> #include ...