国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > UVA 1161 - Objective: Berlin(最大流)

UVA 1161 - Objective: Berlin(最大流)

来源:程序员人生   发布时间:2016-11-05 08:16:22 阅读次数:2201次

题目链接:点击打开链接

思路:

1看这些束缚条件和数据量, 我们就想到了网络流。

由于有时间, 我们可以构造2元组(u, t)表示在城市u,时间t这个状态,  以这样的2元组作为结点跑最大流, 惋惜这样结点高达150*24*60, 会TLE。

我们可以斟酌枚举任意两个航线, 如果满足关系, 就建边, 跑最大流。

斟酌到每一个航线只能经过1次, 我们把航线当作结点, 拆点跑最大流便可。

细节参见代码:

#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⑹; const double PI = acos(⑴); 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⑵); G[to].push_back(m⑴); } 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; }


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生