Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions:11472 | Accepted: 3468 |
Description
You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do some firings. You’re now simply too mad to give response to questions like “Don’t you think it is an even more stupid decision to have signed them?”, yet calm enough to consider the potential profit and loss from firing a good portion of them. While getting rid of an employee will save your wage and bonus expenditure on him, termination of a contract before expiration costs you funds for compensation. If you fire an employee, you also fire all his underlings and the underlings of his underlings and those underlings’ underlings’ underlings… An employee may serve in several departments and his (direct or indirect) underlings in one department may be his boss in another department. Is your firing plan ready now?
Input
The input starts with two integers n (0 < n ≤ 5000) and m (0 ≤ m ≤ 60000) on the same line. Next follows n + m lines. The first n lines of these give the net profit/loss from firing the i-th employee individually bi (|bi| ≤ 107, 1 ≤ i ≤ n). The remaining m lines each contain two integers i and j (1 ≤ i, j ≤ n) meaning the i-th employee has the j-th employee as his direct underling.
Output
Output two integers separated by a single space: the minimum number of employees to fire to achieve the maximum profit, and the maximum profit.
Sample Input
5 5 8 -9 -20 12 -10 1 2 2 5 1 4 3 4 4 5
Sample Output
2 2
Hint
题解:
比较裸的最大权闭合图,建立一个源点,与所有正点相连,边权为其正值
建立一个汇点,与所有负点相连,边权为其绝对值,之前的边改为无穷,
即求源点到汇点的最大流,从起点出发遍历e.cap>0的点便是路径
代码:
#include<cstdio>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
const int maxn = 5005;
const ll inf = 1e18;
struct node
{
int to, rev; ll cap;
};
vector<node>G[maxn];
int n, m, level[maxn], iter[maxn];
bool vis[maxn];
void add(int from, int to, ll cap)
{
node e; e.to = to; e.cap = cap; e.rev = G[to].size();
G[from].push_back(e);
e.to = from; e.cap = 0; e.rev = G[from].size() - 1;
G[to].push_back(e);
}
void bfs()
{
queue<int>P;
memset(level, -1, sizeof(level));
level[0] = 0; P.push(0);
while (!P.empty())
{
int v = P.front(); P.pop();
for (int i = 0; i<G[v].size(); i++)
{
node e = G[v][i];
if (e.cap>0 && level[e.to]<0)
{
level[e.to] = level[v] + 1;
P.push(e.to);
}
}
}
}
ll dfs(int v, int t, ll f)
{
if (v == t)return f;
for (int &i = iter[v]; i<G[v].size(); i++)
{
node &e = G[v][i];
if (e.cap>0 && level[e.to]>level[v])
{
int d;
if (e.cap>f)d = dfs(e.to, t, f);
else d = dfs(e.to, t, e.cap);
if (d>0)
{
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
ll max_flow()
{
ll flow = 0;
while (1)
{
bfs();
if (level[n + 1]<0)return flow;
memset(iter, 0, sizeof(iter));
int f;
while ((f = dfs(0, n + 1, inf))>0)flow += f;
}
}
int upstream(int s)
{
int cnt=0;
memset(vis,0,sizeof(vis));
queue<int>P;vis[s]=1;P.push(s);
while(!P.empty())
{
int u=P.front();P.pop();
for(int i=0;i<G[u].size();i++)
{
node e=G[u][i];
if(e.cap>0&&!vis[e.to])
{
vis[e.to]=1;
P.push(e.to);
cnt++;
}
}
}
return cnt;
}
int main()
{
scanf("%d%d", &n, &m);
ll sum=0;
for (int i = 1; i <= n; i++)
{
int x; scanf("%d", &x);
if (x >= 0)add(0, i, x);
else add(i, n + 1, -x);
if(x>0)sum+=x;
}
for (int i = 1; i <= m; i++)
{
int x, y; scanf("%d%d", &x, &y);
add(x, y, inf);
}
printf("%d %lld\n",upstream(0),sum-max_flow());
for(int i=1;i<=n;i++)if(vis[i])printf("%d\n",i);
return 0;
}