题目链接:点击打开链接
思路:
最大权闭合子图的裸题, 给个学习资料:点击打开链接
当结点即有正权值又有负数权值时, 怎么求任意闭合子图的最大和呢? 只要求出最小割E, 用总的正数权值TOT 减去E就是答案。
细节参见代码:
#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; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 1000000000; const int mod = 1000000000; const int maxn = 644; int n, m, time[maxn], pay[maxn], pro[maxn]; 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 L; vector<int> G[maxn]; int ok(int mid) { int s = 0, t = n + m + 1; g.init(n + m + 10); for(int i = 1; i <= n; i++) { if(time[i] > mid) g.AddEdge(i, t, INF); else g.AddEdge(i, t, pay[i]); } int tot = 0; for(int i = 1; i <= m; i++) { int len = G[i].size(); for(int j = 0; j < len; j++) { int cur = G[i][j]; g.AddEdge(i+n, cur, INF); } g.AddEdge(s, i+n, pro[i]); tot += pro[i]; } int ans = g.Maxflow(s, t); return tot - ans; } int T, k ,v, kase = 0; int main() { scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&L); int l = 0, r = 0; for(int i = 1; i <= n; i++) { scanf("%d%d", &pay[i], &time[i]); r = max(r, time[i]); } for(int i = 1; i <= m; i++) { scanf("%d%d", &pro[i], &k); G[i].clear(); for(int j = 0; j < k; j++) { scanf("%d", &v); G[i].push_back(v); } sort(G[i].begin(), G[i].end()); } printf("Case #%d: ", ++kase); while(r > l) { int mid = (l + r) >> 1; if(ok(mid) >= L) r = mid; else l = mid + 1; } int cur = ok(l); if(cur >= L) printf("%d %d\n", l, cur); else printf("impossible\n"); } return 0; }
时间: 2024-10-25 11:50:08