UVA 1161 - Objective: Berlin(最大流)
来源:互联网 发布:杭州市软件行业协会 编辑:程序博客网 时间:2024/06/08 08:53
题目链接:点击打开链接
思路:
一看这些约束条件和数据量, 我们就想到了网络流。
由于有时间, 我们可以构造二元组(u, t)表示在城市u,时间t这个状态, 以这样的二元组作为结点跑最大流, 可惜这样结点高达150*24*60, 会TLE。
我们可以考虑枚举任意两个航线, 如果满足关系, 就建边, 跑最大流。
考虑到每个航线只能经过一次, 我们把航线当作结点, 拆点跑最大流即可。
细节参见代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>#include <vector>#include <stack>#include <bitset>#include <cstdlib>#include <cmath>#include <set>#include <list>#include <deque>#include <map>#include <queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const double eps = 1e-6;const double PI = acos(-1);const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;const int seed = 131;const ll INF64 = ll(1e18);const int maxn = 10000 + 10;int T,n,m;struct Edge { int from, to, cap, flow;};bool operator < (const Edge& a, const Edge& b) { return a.from < b.from || (a.from == b.from && a.to < b.to);}struct Dinic { int n, m, s, t; // 结点数, 边数(包括反向弧), 源点编号, 汇点编号 vector<Edge> edges; // 边表, edges[e]和edges[e^1]互为反向弧 vector<int> G[maxn]; // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号 bool vis[maxn]; // BFS使用 int d[maxn]; // 从起点到i的距离 int cur[maxn]; // 当前弧指针void init(int 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}); 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 x = Q.front(); Q.pop(); for(int i = 0; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if(!vis[e.to] && e.cap > e.flow) { //只考虑残量网络中的弧 vis[e.to] = 1; d[e.to] = d[x] + 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; }}g;int u, v, c, kase = 0, t, num[maxn], t1[maxn], t2[maxn], a[maxn], b[maxn];char s1[11], s2[11], s3[maxn][10], s4[maxn][10];map<string, int> p;int main() { while(~scanf("%d", &n)) { scanf("%s", s1); p.clear(); scanf("%s", s2); kase = 0; p[s1] = ++kase; if(!p.count(s2)) p[s2] = ++kase; int start = p[s1], endd = p[s2]; scanf("%s%d", s1, &m); t = ((s1[0]-'0')*10+s1[1]-'0')*60+(s1[2]-'0')*10+s1[3]-'0'; for(int i = 1; i <= m; i++) { scanf("%s%s%d%s%s", s3[i], s4[i], &num[i], s1, s2); t1[i] = ((s1[0]-'0')*10+s1[1]-'0')*60+(s1[2]-'0')*10+s1[3]-'0'; t2[i] = ((s2[0]-'0')*10+s2[1]-'0')*60+(s2[2]-'0')*10+s2[3]-'0'; if(!p.count(s3[i])) p[s3[i]] = ++kase; if(!p.count(s4[i])) p[s4[i]] = ++kase; a[i] = p[s3[i]]; b[i] = p[s4[i]]; } g.init(2*m + 5); int res = m; int src = 2*m + 1, stc = 2*m + 2; for(int i = 1; i <= m; i++) { int id1 = b[i]; int cur = a[i]; g.AddEdge(i, i+m, num[i]); if(start == cur) g.AddEdge(src, i, INF); if(id1 == endd && t2[i] <= t) g.AddEdge(m+i, stc, INF); for(int j = 1; j <= m; j++) { if(i == j) continue; int id2 = a[j]; if(id1 != id2) continue; if(t2[i] + 30 <= t1[j]) g.AddEdge(i+m, j, INF); } } printf("%d\n", g.Maxflow(src, stc)); } return 0;}
0 0
- uva 1161 Objective: Berlin (最大流)
- UVA 1161 - Objective: Berlin(最大流)
- UVA - 1161 Objective: Berlin(最大流+时序模型)
- UVA 1161&& UVALive 3645 Objective: Berlin (最大流 -- 时序模型)
- Uva-1161-Objective: Berlin
- UVA 1161 - Objective: Berlin(网络流)
- UVALive 3645 Objective: Berlin(最大流)
- Objective: Berlin - UVa 1161 Dinic
- UVALive 3645 Objective: Berlin (最大流,拆点法)
- (beginer) 网络流(拆点+时序模型) UVA 1161 - Objective: Berlin
- LA 3645 Objective: Berlin 最大流
- Objective: Berlin UVALive
- uva 11045 uva 10330(EK最大流)
- UVa 259 Software Allocation ( 最大流 )
- uva 11972 - Round Trip(最大流)
- uva 11380(最大流+拆点)
- uva 563 Crimewave(最大流)
- uva 11082 Matrix Decompressing (最大流)
- R中常用的命令
- Android环境下移植busybox-1.21.1
- Canvas实战
- android Design Support Library
- 单点登陆_搭建linux开发环境
- UVA 1161 - Objective: Berlin(最大流)
- LeetCode #216: Combination Sum III
- EDID 详解
- [LeetCode]ZigZag Conversion
- android6.0权限等级
- Android L APP 如何获取sys file system 中节点的写权限
- 线程同步的几种方法
- HDU 4556 Stern-Brocot Tree
- 【NOIP提高组】数格子