UVA 1161 - Objective: Berlin(网络流)
来源:程序员人生 发布时间:2014-10-08 14:39:46 阅读次数:1753次
UVA 1161 - Objective: Berlin
题目链接
题意:给定一些航班,每个航班有人数,和起始终止时间,每次转机要花半小时,问限制时间内最多能有多少人从起始城市到终点城市
思路:以航班为结点建图,航班有容量限制所以进行拆点,然后两个航班如果终点和起点对上,并且时间满足就可以建边,然后源点连向起点为起始的航班,终点为终点的航班连向汇点(要在时间不超过时限的情况下),建好图跑一下最大流就可以了
代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int MAXNODE = 10005;
const int MAXEDGE = 1000005;
typedef int Type;
const Type INF = 0x3f3f3f3f;
struct Edge {
int u, v;
Type cap, flow;
Edge() {}
Edge(int u, int v, Type cap, Type flow) {
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
}
};
struct Dinic {
int n, m, s, t;
Edge edges[MAXEDGE];
int first[MAXNODE];
int next[MAXEDGE];
bool vis[MAXNODE];
Type d[MAXNODE];
int cur[MAXNODE];
vector<int> cut;
void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
}
void add_Edge(int u, int v, Type cap) {
edges[m] = Edge(u, v, cap, 0);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0);
next[m] = first[v];
first[v] = m++;
}
bool bfs() {
memset(vis, false, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = true;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = first[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (!vis[e.v] && e.cap > e.flow) {
vis[e.v] = true;
d[e.v] = d[u] + 1;
Q.push(e.v);
}
}
}
return vis[t];
}
Type dfs(int u, Type a) {
if (u == t || a == 0) return a;
Type flow = 0, f;
for (int &i = cur[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[i^1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
Type Maxflow(int s, int t) {
this->s = s; this->t = t;
Type flow = 0;
while (bfs()) {
for (int i = 0; i < n; i++)
cur[i] = first[i];
flow += dfs(s, INF);
}
return flow;
}
void MinCut() {
cut.clear();
for (int i = 0; i < m; i += 2) {
if (vis[edges[i].u] && !vis[edges[i].v])
cut.push_back(i);
}
}
} gao;
const int N = 5005;
int n, hn, limit;
map<string, int> hash;
int get(string str) {
if (!hash.count(str)) hash[str] = hn++;
return hash[str];
}
int gett(string str) {
int h = (str[0] - '0') * 10 + str[1] - '0';
int m = (str[2] - '0') * 10 + str[3] - '0';
return h * 60 + m;
}
struct HB {
int u, v, c, s, t;
void read() {
string a, b;
cin >> a >> b;
u = get(a); v = get(b);
cin >> c;
cin >> a >> b;
s = gett(a); t = gett(b);
}
} h[N];
bool judge(HB a, HB b) {
if (a.v != b.u) return false;
if (a.t + 30 > b.s) return false;
return true;
}
int main() {
while (~scanf("%d", &n)) {
string a, b;
int s, t;
hash.clear();
hn = 0;
cin >> a >> b;
s = get(a); t = get(b);
cin >> a;
limit = gett(a);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
h[i].read();
gao.init(n * 2 + 2);
for (int i = 1; i <= n; i++) {
if (h[i].u == s) gao.add_Edge(0, i, INF);
if (h[i].v == t && h[i].t <= limit) gao.add_Edge(i + n, n * 2 + 1, INF);
gao.add_Edge(i, i + n, h[i].c);
for (int j = 1; j <= n; j++) {
if (i == j) continue;
if (judge(h[i], h[j])) gao.add_Edge(i + n, j, INF);
}
}
printf("%d
", gao.Maxflow(0, n * 2 + 1));
}
return 0;
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠