Sha 1161-Objective: Berlin (Network Stream)

Source: Internet
Author: User
Sha 1161-Objective: Berlin

Question Link

Question: given some flights, each flight has a certain number of people, and the start and end time, it takes half an hour to start each flight and ask how many people can start from the start city to the end city within the time limit.

Train of Thought: Build a map based on the flight node, because there is a capacity limit on the flight, so we need to split the points. Then, if the two flights are opposite to the starting point, and the time is enough, we can build the edge, then, the source point is the starting flight, and the end point is the destination flight's redirect point (when the time limit is not exceeded). Set up a graph and run the maximum flow.

Code:

#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;}


Sha 1161-Objective: Berlin (Network Stream)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.