UVA 1161 - Objective: Berlin(网络流)

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\n", gao.Maxflow(0, n * 2 + 1));
	}
	return 0;
}
时间: 2024-08-04 13:11:55

UVA 1161 - Objective: Berlin(网络流)的相关文章

uva 1161 Objective: Berlin (最大流)

uva 1161 Objective: Berlin 题目大意:你要从A地到B地去,并且最晚要在lt之前到达.现在给你m个航班信息,信息包括:起始地点,降落地点,载客上限,起飞时间,降落时间.中途转机要花费半小时的时间.问在lt之前,可以从A地到达B地的最多的游客数量. 解题思路:以航班为节点进行建图.设置超级源点,连向所有起点为A地的航班,容量为INF:设置超级汇点,使所有降落地点为B点且降落时间在lt之前的航班连向超级汇点.每个航班都要拆成两个节点,中间的边容量为该航班的载客上限.如果i航班

UVa 1161 Objective: Berlin (最大流)

题意:给定一些航班,每个航班有人数,和起始终止时间,每次转机要花半小时,问限制时间内最多能有多少人从起始城市到终点城市. 析:差不多是裸板网络流的最大流问题,把每个航班都拆成两个点,这两个点之间连接一条流量为这个航班的容量,然后再暴力去查看能不能连接,如果能, 那么就连接一条容量无限的边,然后在源点和汇点加一个无限的容量边. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #

UVA 11765 Component Placement 网络流 新姿势建图

题目链接:点击打开链接 题意: 给定n个物品, m个约束条件 把n个物品分到2个集合里 下面第一行表示i物品分到第一个集合里的花费 第二行表示分到第二个集合里的花费 第三行表示分物品的限制(1表示只能分到第一个集合,-1表示只能分到第二个集合,0无限制) 下面m行给出约束条件 u v cost 表示u v 两点必须能互相沟通,若两点已经在同一集合则花费为0 ,若不在同一集合则花费增加cost 问满足m个约束条件下的最小花费 思路: 首先感觉是网络流,==建图比较难想 用流量表示费用 1.若i点放

UVa1161 Objective: Berlin(最大流)

题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3602 Description The administration of a well-known football team has made a study about the lack of support ininternational away games. This st

UVA 10779 Collectors Problem 网络流+建图

题目链接:点击打开链接 题意:白书P370 思路: 因为问的是最后贴纸总数,那么就设最后的贴纸总数是网络流的答案. 首先我们模拟贴纸的流动过程: Bob 的 某种贴纸a -> 给一个没有a贴纸的人Peo -> 还给Bob一个Peo的某张重复贴纸 -> 这张贴纸可以算作答案了 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include

UVA 11082 - Matrix Decompressing(网络流+行列模型)

UVA 11082 - Matrix Decompressing 题目链接 题意:给定一个矩阵每行每列的和,要求现在构造一个矩阵满足元素在1-20之间,行列和满足条件 思路:行列建图,源点连到每个行,容量为和,每列连到汇点,容量为和,每行连到每列,容量20,注意这题要求的是1-20,所以可以先把所有位置-1,最后输出的时候+1即可 代码: #include <cstdio> #include <cstring> #include <queue> #include <

UVA 1486 - Transportation(网络流+拆边)

UVA 1486 - Transportation 题目链接 题意:一个有向图上运输k货物,有一些边,每个边一个系数a,经过该边如果有x货物,就要缴纳a x x的钱,问运输从1到n最小的代价 思路:费用流,这题边的容量c最大只有5,所以可以拆边,一条边拆成c条边,每条容量1,对应相应的代价为a * (i^2 - (i - 1)^2),然后跑一下费用流即可 代码: #include <cstdio> #include <cstring> #include <vector>

UVALIVE 3645 Objective: Berlin

最大流 .以航班为节点进行最大流. 容量限制进行拆点. 如果时间地点满足可以建一条边. 具体看代码.变量名被修改过了.一开始的变量名可能比较容易看懂 但CE了.可能与库里的变量重复了. AC代码 #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #

UVA 1306 - The K-League(网络流)

UVA 1306 - The K-League 题目链接 题意:n个球队,已经有一些胜负场,现在还有一些场次,你去分配胜负,问每支球队有没有可能获胜 思路:网络流公平分配模型,把场次当作任务,分配给人,然后先贪心,枚举每个人,让这些人能赢的都赢,剩下的去建图,每个源点连向比赛容量为场次,每个比赛连向2个球队,容量无限大,每个球队连向汇点,容量为每个的人的总和减去当前已经赢的,建完图跑一下最大流,然后判断源点流出的是否都满流即可 代码: #include <cstdio> #include &l