解题思路:
涨姿势,什么叫闭合图: http://www.cnblogs.com/wuyiqi/archive/2012/03/12/2391960.html
第一种解法:
第一问:s和i点连一个权为ai的边,y和i点连一个权为bi的边,若j依赖i,则i向j连一条权为inf的边。对图求最小割。
图可以这样建的原因是:假设j依赖i,若i到y的边断了,即i为延迟完成。这时候从s到i的的残留会转移到i->j->y,这时候也只能把j到y的边断了。
第二问:s集合是延迟集合,t是提前集合。把s的点都跑一遍标记一下,剩余的都可以分给t集。
第二种解法:
第一问:我也不知道他们是怎么想到的,先对所有点取sum(min(del[i], adv[i]))并把它当作答案。如果没有依赖的话,显然这个就是答案。
对于所有点,如果adv[i] - del[i] > 0(表示提前完成比延迟完成多的代价),则s到i连一条adv[i] - del[i]边(表示延迟完成比提前完成多的代价),否则i到t连一条边del[i] - adv[i],若j依赖i,则i向j连一条INF的边。对上图求最小割。
假设存在j依赖i,并且del[i] - adv[i] < 0,之前我们默认i是延迟的,若把s->i断掉,则i变成是提前的;若不把s->i断掉,则残留压向i->j,若存在j->t的边,则j->t也要断掉。符合题目要求。
太机智了。
第二问:同上。
第一种:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <queue>
#include <stack>
#include <cstdlib>
#include <ctime>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ULL unsigned long long
#define Hei cout << "Czy!!!" << endl;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define MOD 1000000007
/*
clock_t t1, t2;
t1 = clock();
t2 = clock();
cout << (double)(t2 - t1) / CLOCKS_PER_SEC << endl;
*/
#define maxn 210
#define INF 100000000
struct Edge
{
int from, to, cap, flow;
Edge(int from = 0, int to = 0, int cap = 0, int flow = 0): from(from), to(to), cap(cap), flow(flow) {}
};
vector<int> G[maxn];
vector<Edge> edges;
bool vis[210];
int tot;
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
struct Dinic
{
int s, t;
bool vis[maxn];
int d[maxn];
int cur[maxn];
bool BFS()
{
memset(vis, 0, sizeof(vis));
queue<int> Q;
while (!Q.empty()) Q.pop();
Q.push(s);
d[s] = 0;
vis[s] = 1;
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())
{
//Hei;
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF);
}
return flow;
}
}solver;
void dfs(int u)
{
vis[u] = true;
for (int i = 0; i < G[u].size(); i++)
{
Edge e = edges[G[u][i]];
if (e.flow != e.cap && !vis[e.to]) dfs(e.to);
}
}
int main()
{
freopen("gen.in", "r", stdin);
freopen("my.out", "w", stdout);
int n;
while (cin >> n)
{
edges.clear();
for (int i = 0; i <= n + 1; i++) G[i].clear();
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
AddEdge(0, i, x);
}
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
AddEdge(i, n + 1, x);
}
int m;
cin >> m;
while (m--)
{
int x, y;
scanf("%d%d", &x, &y);
AddEdge(x, y, INF);
}
cout << solver.MaxFlow(0, n + 1) << " ";
memset(vis, false, sizeof(vis));
dfs(0);
int tot = 0;
for (int i = 1; i <= n; i++) if (!vis[i]) tot++;
cout << tot << endl;
}
fclose(stdin);
fclose(stdout);
}
第二种:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <queue>
#include <stack>
#include <cstdlib>
#include <ctime>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ULL unsigned long long
#define Hei cout << "Czy!!!" << endl;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define MOD 1000000007
/*
clock_t t1, t2;
t1 = clock();
t2 = clock();
cout << (double)(t2 - t1) / CLOCKS_PER_SEC << endl;
*/
#define maxn 210
#define INF 100000000
struct Edge
{
int from, to, cap, flow;
Edge(int from = 0, int to = 0, int cap = 0, int flow = 0): from(from), to(to), cap(cap), flow(flow) {}
};
vector<int> G[maxn];
vector<Edge> edges;
bool vis[210];
int adv[210], del[210];
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
struct Dinic
{
int s, t;
bool vis[maxn];
int d[maxn];
int cur[maxn];
bool BFS()
{
memset(vis, 0, sizeof(vis));
queue<int> Q;
while (!Q.empty()) Q.pop();
Q.push(s);
d[s] = 0;
vis[s] = 1;
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())
{
//Hei;
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF);
}
return flow;
}
}solver;
void dfs(int u)
{
vis[u] = true;
for (int i = 0; i < G[u].size(); i++)
{
Edge e = edges[G[u][i]];
if (e.flow != e.cap && !vis[e.to]) dfs(e.to);
}
}
int main()
{
freopen("gen.in", "r", stdin);
freopen("my.out", "w", stdout);
int n;
while (cin >> n)
{
edges.clear();
for (int i = 0; i <= n + 1; i++) G[i].clear();
for (int i = 1; i <= n; i++) scanf("%d", &adv[i]);
int ans = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &del[i]);
if (adv[i] - del[i] > 0) AddEdge(0, i, adv[i] - del[i]);
else AddEdge(i, n + 1, del[i] - adv[i]);
ans += min(adv[i], del[i]);
}
int m;
cin >> m;
while (m--)
{
int x, y;
scanf("%d%d", &x, &y);
AddEdge(x, y, INF);
}
cout << solver.MaxFlow(0, n + 1) + ans << " ";
memset(vis, false, sizeof(vis));
dfs(0);
int tot = 0;
for (int i = 1; i <= n; i++) if (!vis[i]) tot++;
cout << tot << endl;
}
fclose(stdin);
fclose(stdout);
}