UVA - 1161 Objective: Berlin(最大流+时序模型)
题目大意:有n个城市m条航线。给出每条航线的出发地,目的地,座位数,起飞时间和到达时间(所给形式为HHMM。记得转化),再给出城市A和B。和到达城市B的最晚时间。如今问一天内最多有多少人能从A飞到B,能够在其它城市中转
解题思路:将飞机票拆点,拆成i–>i + m,容量为座位数。
接着推断一下。航线之间的连线
假设航线的起点是A的话,那么就和超级源点相连,容量为INF
假设航线的终点是B且到达时间小于等于最晚时间。那么连线,容量为INF
假设航线i的终点和航线j的起点同样。且航线i的到达时间+30<=航线j的起始时间,那么连线。容量为INF
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <iostream>
using namespace std;
#define N 10010
#define INF 0x3f3f3f3f
struct Edge{
int from, to, cap, flow;
Edge() {}
Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
};
struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N], cur[N];
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));
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s] = 1;
d[s] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
d[e.to] = d[u] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a) {
if (x == t || a == 0)
return a;
int flow = 0, f;
for (int i = cur[x]; i < G[x].size(); i++) {
Edge &e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0)
break;
}
}
return flow;
}
int Maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF);
}
return flow;
}
};
Dinic dinic;
#define M 5100
#define S 160
int n, m, source, sink, Time;
int num[S];
map<string, int> Map;
struct Node {
int u, v, c, s, t;
}node[M];
int getTime(string T) {
int a = (T[0] - '0') * 10 + (T[1] - '0');
int b = (T[2] - '0') * 10 + (T[3] - '0');
return a * 60 + b;
}
void solve() {
Map.clear();
int cnt = 3;
string a, b, s, t;
cin >> a >> b >> s >> m;
Map[a] = 1; Map[b] = 2;
Time = getTime(s);
memset(num, 0, sizeof(num));
source = 0; sink = 2 * m + 1;
dinic.init(sink);
for (int i = 1; i <= m; i++) {
cin >> a >> b >> node[i].c >> s >> t;
if (!Map[a]) Map[a] = cnt++;
if (!Map[b]) Map[b] = cnt++;
node[i].u = Map[a];
node[i].v = Map[b];
node[i].s = getTime(s);
node[i].t = getTime(t);
num[node[i].u]++; num[node[i].v]++;
dinic.AddEdge(i, i + m, node[i].c);
}
if (!num[1] || !num[2]) {
printf("0\n");
return ;
}
for (int i = 1; i <= m; i++) {
int u = node[i].u, v = node[i].v;
if (u == 1) dinic.AddEdge(source, i, INF);
if (v == 2 && node[i].t <= Time) dinic.AddEdge(i + m, sink, INF);
for (int j = 1; j <= m; j++) {
if (i == j) continue;
if (v != node[j].u) continue;
if (node[i].t + 30 <= node[j].s) dinic.AddEdge(i + m, j, INF);
}
}
int ans = dinic.Maxflow(source, sink);
printf("%d\n", ans);
}
int main() {
while (scanf("%d\n", &n) != EOF) solve();
return 0;
}
UVA - 1161 Objective: Berlin(最大流+时序模型)的更多相关文章
- UVa 1161 Objective: Berlin (最大流)
题意:给定一些航班,每个航班有人数,和起始终止时间,每次转机要花半小时,问限制时间内最多能有多少人从起始城市到终点城市. 析:差不多是裸板网络流的最大流问题,把每个航班都拆成两个点,这两个点之间连接一 ...
- UVALive 3645 Objective: Berlin(最大流 :时序模型)
题意:已知n(n <= 150)个城市和m(m <= 5000)个航班,每个航班有出发地.到达地.乘坐人数.起飞时间和降落时间(时间用时和分表示),求从一个指定城市出发,去往另一个指定城市 ...
- UVaLive 3645 Objective: Berlin (最大流)
题意:有n个城市,m条航班.已知每条航班的起点和终点,还有每条航班的载客量.出发时间.到达时间.并且要求在任何一个城市(起点.终点除外)都至少要有30分钟的中转时间,求起点到终点的最大客流量. 析:把 ...
- 应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测
应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测 Real-Time Anomaly Detection for Streaming Analytics Subutai Ahmad SAHM ...
- Verilog篇(四)时序模型
时序模型:仿真器的时间推进模型,它反映了推进仿真时间和调度事件的方式. 1)门级时序模型:适用于分析所有的连续赋值语句,过程连续赋值语句,门级原语,用户自定义原语. 特点:任意时刻,任意输入变化都将重 ...
- Keras 时序模型
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Thinking_boy1992/article/details/53207177 本文翻译自 时序模 ...
- EGADS介绍(二)--时序模型和异常检测模型算法的核心思想
EDADS系统包含了众多的时序模型和异常检测模型,这些模型的处理会输入很多参数,若仅使用默认的参数,那么时序模型预测的准确率将无法提高,异常检测模型的误报率也无法降低,甚至针对某些时间序列这些模型将无 ...
- 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 ...
随机推荐
- Elasticsearch之源码分析(shard分片规则)
前期博客是 Elasticsearch之源码编译 (1)elasticsearch在建立索引时,根据id或(id,类型)进行hash,得到hash值之后再与该索引的分片数量取模,取模的值即为存入的分片 ...
- 认识Linux瘦客户机
(本文完整版见http://os.51cto.com/art/201001/181448.htm) 随着Linux的发展,以及网络计算技术的发展和逐步深入的云计算,基于Li ...
- JS实现下拉菜单的功能
<!DOCTYPE html> <html> <head> <meta charset = "utf8"> <title> ...
- JS面向对象系列教程 — 对象的基本操作
面向对象概述  面向对象(Object Oriented)简称OO,它是一种编程思维,用于指导我们如何应对各种复杂的开发场景. 这里说的对象(Object),意思就是事物,在面向对象的思维中,它将一 ...
- 洛谷——P1311 选择客栈
https://www.luogu.org/problem/show?pid=1311 题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰 ...
- 从”茄子快传”看应用程序怎样获取手机已安装程序的apk文件
"茄子快传"是联想开发的一款近距离文件共享软件.它通过wifi-direct(速度飞快,不须要联网)或者普通的网络(速度慢)在不同手机间传递文件. 不知为何.它就火了起来,火的也飞 ...
- Go语言核心之美 1.5-作用域
变量的作用域是指程序代码中能够有效使用这个变量的范围. 不要将作用域和生命期混在一起. 作用域是代码中的一块区域,是一个编译期的属性:生命期是程序执行期间变量存活的时间段.在此时间段内,变量能够被程序 ...
- 任务调度(四)——ScheduledExecutorService替代Timer,实现多线程任务调度
上篇博文<任务调度(三)--Timer的替代品ScheduledExecutorService简介>已经对ScheduledExecutorService做了简介.事实上使用Schedul ...
- 28.Node.js 函数和匿名函数
转自:http://www.runoob.com/nodejs/nodejs-module-system.html 在JavaScript中,一个函数可以作为另一个函数的参数.我们可以先定义一个函数, ...
- 17.Node.js 回调函数--异步编程
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程 ...