UVa 1161 Objective: Berlin (最大流)
题意:给定一些航班,每个航班有人数,和起始终止时间,每次转机要花半小时,问限制时间内最多能有多少人从起始城市到终点城市。
析:差不多是裸板网络流的最大流问题,把每个航班都拆成两个点,这两个点之间连接一条流量为这个航班的容量,然后再暴力去查看能不能连接,如果能,
那么就连接一条容量无限的边,然后在源点和汇点加一个无限的容量边。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10005;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};
const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
const int maxm = 1000005; struct Edge{
int v, f, next;
};
int src, sink;
int g[maxn];
int nume;
Edge e[maxm];
map<string, int> mp;
int cnt; int getid(const string &s){
return mp.count(s) ? mp[s] : mp[s] = cnt++;
} void add(int u, int v, int c){
e[++nume].v = v;
e[nume].f = c;
e[nume].next = g[u];
g[u] = nume;
e[++nume].v = u;
e[nume].f = 0;
e[nume].next = g[v];
g[v] = nume;
} queue<int> q;
bool vis[maxn + 10];
int d[maxn + 10]; void bfs(){
memset(d, 0, sizeof d);
while(!q.empty()) q.pop();
vis[src] = true;
q.push(src);
while(!q.empty()){
int u = q.front(); q.pop();
for(int i = g[u]; i; i = e[i].next)
if(e[i].f && !vis[e[i].v]){
q.push(e[i].v);
d[e[i].v] = d[u] + 1;
vis[e[i].v] = true;
}
}
} int dfs(int u, int del){
if(u == sink) return del;
int ans = 0;
for(int i = g[u]; i && del; i = e[i].next)
if(e[i].f && d[e[i].v] == d[u] + 1){
int dd = dfs(e[i].v, Min(e[i].f, del));
e[i].f -= dd;
e[i^1].f += dd;
del -= dd;
ans += dd;
}
return ans;
} int maxflow(){
int ans = 0;
while(true){
memset(vis, 0, sizeof vis);
bfs();
if(!vis[sink]) return ans;
ans += dfs(src, INF);
}
}
char s[105], t[105];
struct node{
int u, v, c, start, last;
};
node a[maxm]; int cal(char *s){
int ans = 0;
ans += ((s[0] - '0') * 10 + s[1] - '0') * 60;
ans += ((s[2] - '0') * 10 + s[3] - '0');
return ans;
} bool judge(const node &lhs, const node &rhs){
return lhs.v == rhs.u && lhs.last + 30 <= rhs.start;
} int main(){
while(scanf("%d", &n) == 1){
scanf("%s", s);
cnt = 0;
mp.clear();
int ss = getid(s);
scanf("%s", t);
int tt = getid(t);
memset(g, 0, sizeof g);
nume = 1;
int time;
scanf("%s", s);
time = cal(s);
scanf("%d", &m);
char start[10], last[10];
src = 0; sink = 2 * m + 1;
for(int i = 1; i <= m; ++i){
scanf("%s %s %d %s %s", s, t, &a[i].c, start, last);
a[i].u = getid(s);
a[i].v = getid(t);
a[i].start = cal(start);
a[i].last = cal(last);
}
for(int i = 1; i <= m; ++i){
if(a[i].u == ss) add(0, i, INF);
if(a[i].v == tt && a[i].last <= time) add(i+m, 2*m+1, INF);
add(i, i+m, a[i].c);
for(int j = 1; j <= m; ++j)
if(i != j && judge(a[i], a[j])) add(i+m, j, INF);
}
printf("%d\n", maxflow());
}
return 0;
}
UVa 1161 Objective: Berlin (最大流)的更多相关文章
- UVA - 1161 Objective: Berlin(最大流+时序模型)
题目大意:有n个城市m条航线.给出每条航线的出发地,目的地,座位数,起飞时间和到达时间(所给形式为HHMM.记得转化),再给出城市A和B.和到达城市B的最晚时间.如今问一天内最多有多少人能从A飞到B, ...
- UVALive 3645 Objective: Berlin(最大流 :时序模型)
题意:已知n(n <= 150)个城市和m(m <= 5000)个航班,每个航班有出发地.到达地.乘坐人数.起飞时间和降落时间(时间用时和分表示),求从一个指定城市出发,去往另一个指定城市 ...
- UVaLive 3645 Objective: Berlin (最大流)
题意:有n个城市,m条航班.已知每条航班的起点和终点,还有每条航班的载客量.出发时间.到达时间.并且要求在任何一个城市(起点.终点除外)都至少要有30分钟的中转时间,求起点到终点的最大客流量. 析:把 ...
- UVa1161 Objective: Berlin(最大流)
题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVALive-3645 Objective: Berlin (最大流:时序模型)
题目大意:有n个城市,m条航班.已知每条航班的起点和终点,还有每条航班的载客量.出发时间.到达时间.并且要求在任何一个城市(起点.终点除外)都至少要有30分钟的中转时间,求起点到终点的最大客流量. 题 ...
- UVA 820 --- POJ 1273 最大流
找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...
- Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。
/** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...
- uva 1658(最小费用最大流)
题意:一个带权有向图,求起点到终点的两条路径权值之和最小,且两条路径没有公共点(除起点,终点): 分析:拆点法,将u拆成u和u',u-u'容量为1,费用为0,这样就能保证每个点只用一次,起点s-s'容 ...
- UVA 10779 Collectors Problem(最大流)
这个题是很难往网络流上面构思的... 从s向每个物品增加容量为Bob拥有数的弧,然后从每个物品向t增加容量为1的弧(代表种类个数).这时候跑最大流的话,得到的肯定是Bob拥有的初始种类数.那么交换后的 ...
随机推荐
- msp430入门编程50
msp430中项目编程套路 msp430入门编程 msp430入门学习
- 从零开始写STL—set/map
这一部分只要把搜索树中暴露的接口封装一下,做一些改动. set源码剖析 template<typename T> class set { public: typedef T key_typ ...
- Eclipse 中 新建maven项目 无法添加src/main/java 问题
eclipse创建maven web项目,在选择maven_archetype_web原型后,默认只有src/main/resources这个Source Floder. 按照maven目录结构,添加 ...
- DNS的工作原理及解析
DNS协议是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 一.什么是DNS? DNS( Domain Name System)是“域名系统”的英文缩写,是一种组织成域层次 ...
- C++:vector中的resize()函数 VS reserve()函数
http://www.cnblogs.com/biyeymyhjob/archive/2013/05/11/3072893.html
- 搜索引擎keyword智能提示的一种实现
问题背景 搜索关键字智能提示是一个搜索应用的标配.主要作用是避免用户输入错误的搜索词,并将用户引导到相应的关键词上,以提升用户搜索体验. 美团CRM系统中存在数以百万计的商家,为了让用户高速查找到目标 ...
- Ajax系列之四:问题总结
1.最经典的就是ie下的缓存问题了. 假设使用的是get.那么在ie下出现缓存问题.导致代码仅仅运行一次. 解决的方法就是加时间戳或者随机数,使url变为唯一,这样就不会出现ie 下的缓存问题了, ...
- “约定优于配置”与Magento改造尝试四之block、helper和model载入
暂定本章为这个系列最后一章,还是继续沿用模块的别名(alias)概念 <modules> <Mage_Wishlist> <version>1.6.0.0</ ...
- Office文档如何转换 PDF 转 DOC XLS
1 使用Adobe Acrobat Pro,打开任意PDF都可以转换为XLSX格式(似乎没找到XLS) 2 如果你转换之后的东西无法打开,则先转换成DOC,然后再把DOC全选复制粘贴到XLS即可 ...
- Opengl ES 1.x NDK实例开发之七:旋转的纹理立方体
开发框架介绍请參见:Opengl ES NDK实例开发之中的一个:搭建开发框架 本章在第六章(Opengl ES 1.x NDK实例开发之六:纹理贴图)的基础上绘制一个旋转的纹理立方体,原理和纹理贴图 ...