标签:
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1470 Accepted Submission(s): 485
#include <bits/stdc++.h> using namespace std; int N, M; #define maxn 5010 const int inf = 0x3f3f3f3f; struct Edge { int from, to, cap, flow; Edge(int f, int t, int c, int fl) { from = f; to = t; cap = c; flow = fl; } }; struct Road { int st, ed, oer, cost; }road[3010]; vector <Edge> edges; vector <int> G[maxn]; int n, m, s, t; int d[maxn], cur[maxn], vis[maxn]; 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)); vis[s] = 1; d[s] = 0; queue <int> q; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = 0; i < G[u].size(); i++) { Edge &e = edges[G[u][i]]; if(!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[u]+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 flow = 0; while(bfs()) { memset(cur, 0, sizeof(cur)); flow += dfs(s, inf); } return flow; } struct Node { int pos, oer; Node(int p, int o) { pos = p; oer = o; } }; vector <Node> mp[1010]; int cost[5050]; int main() { while(~scanf("%d%d", &N, &M) && (N+M)) { edges.clear(); for(int i = 0; i <= M+1; i++) G[i].clear(); for(int i = 1; i <= N; i++) mp[i].clear(); s = 0; t = M+1; int sum = 0; for(int i = 1; i <= M; i++) { int pro; scanf("%d", &pro); sum += pro; AddEdge(s, i, pro); } int k; scanf("%d", &k); memset(cost, 0, sizeof(cost)); for(int i = 1; i <= k; i++) { scanf("%d%d%d%d", &road[i].st, &road[i].ed, &road[i].oer, &road[i].cost); cost[road[i].oer] += road[i].cost; mp[road[i].st].push_back(Node(road[i].ed, road[i].oer)); } for(int i = 1; i <= k; i++) { int noer = road[i].oer; int u = road[i].ed; for(int j = 0; j < mp[u].size(); j++) { if(mp[u][j].oer == noer) continue; AddEdge(noer, mp[u][j].oer, inf); } } for(int i = 1; i <= M; i++) AddEdge(i, t, cost[i]); int flow = Maxflow(); printf("%d\n",(sum - flow >0?sum-flow:0)); } return 0; }
hdu 3917 Road constructions 最大权闭合图
标签:
原文地址:http://www.cnblogs.com/titicia/p/4699805.html