POJ2125:Destroying The Graph
来源:互联网 发布:电脑编程与维护 编辑:程序博客网 时间:2024/04/30 03:59
Destroying The Graph
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 4718 Accepted: 1436 Special Judge
Description
Alice and Bob play the following game. First, Alice draws some directed graph with N vertices and M arcs. After that Bob tries to destroy it. In a move he may take any vertex of the graph and remove either all arcs incoming into this vertex, or all arcs outgoing from this vertex.
Alice assigns two costs to each vertex: Wi+ and Wi-. If Bob removes all arcs incoming into the i-th vertex he pays Wi+ dollars to Alice, and if he removes outgoing arcs he pays Wi- dollars.
Find out what minimal sum Bob needs to remove all arcs from the graph.
Alice assigns two costs to each vertex: Wi+ and Wi-. If Bob removes all arcs incoming into the i-th vertex he pays Wi+ dollars to Alice, and if he removes outgoing arcs he pays Wi- dollars.
Find out what minimal sum Bob needs to remove all arcs from the graph.
Input
Input file describes the graph Alice has drawn. The first line of the input file contains N and M (1 <= N <= 100, 1 <= M <= 5000). The second line contains N integer numbers specifying Wi+. The third line defines Wi- in a similar way. All costs are positive and do not exceed 106 . Each of the following M lines contains two integers describing the corresponding arc of the graph. Graph may contain loops and parallel arcs.
Output
On the first line of the output file print W --- the minimal sum Bob must have to remove all arcs from the graph. On the second line print K --- the number of moves Bob needs to do it. After that print K lines that describe Bob's moves. Each line must first contain the number of the vertex and then '+' or '-' character, separated by one space. Character '+' means that Bob removes all arcs incoming into the specified vertex and '-' that Bob removes all arcs outgoing from the specified vertex.
Sample Input
3 61 2 34 2 11 21 13 21 23 12 3
Sample Output
531 +2 -2 +
Source
Northeastern Europe 2003, Northern Subregion
一道典型的最小点权覆盖问题,SAP速度很好看,之后需要搜索一下用过的点,输出即可。
代码:
一道典型的最小点权覆盖问题,SAP速度很好看,之后需要搜索一下用过的点,输出即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define N 10010
#define M 20010
#define inf 1 << 30
#define eps 1 << 29
using namespace std;
int mark[N];
int cnt, n, m, s, t;
int head[N];
int NN;
struct edge
{
int v, next, w;
} edge[M];
void addedge(int u, int v, int w)
{
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
edge[cnt].v = u;
edge[cnt].w = 0;
edge[cnt].next = head[v];
head[v] = cnt++;
}
int sap()
{
int pre[N], cur[N], dis[N], gap[N];
int flow = 0, aug = inf, u;
bool flag;
for (int i = 1; i <= NN; ++i)
{
cur[i] = head[i];
gap[i] = dis[i] = 0;
}
gap[s] = NN;
u = pre[s] = s;
while (dis[s] < NN)
{
flag = 0;
for (int &j = cur[u]; j != -1; j = edge[j].next)
{
int v = edge[j].v;
if (edge[j].w > 0 && dis[u] == dis[v] + 1)
{
flag = 1;
if (edge[j].w < aug) aug = edge[j].w;
pre[v] = u;
u = v;
if (u == t)
{
flow += aug;
while (u != s)
{
u = pre[u];
edge[cur[u]].w -= aug;
edge[cur[u] ^ 1].w += aug;
}
aug = inf;
}
break;
}
}
if (flag)
continue;
int mindis = NN;
for (int j = head[u]; j != -1; j = edge[j].next)
{
int v = edge[j].v;
if (edge[j].w > 0 && dis[v] < mindis)
{
mindis = dis[v];
cur[u] = j;
}
}
if ((--gap[dis[u]]) == 0)
break;
gap[dis[u] = mindis + 1]++;
u = pre[u];
}
return flow;
}
void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
memset(mark, 0, sizeof(mark));
}
void dfs(int x) //不同于单纯的SAP,加入了一个搜素点集元素的函数,通过head数组的记录信息搜索。
{
mark[x] = 1;
for (int i = head[x]; i; i = edge[i].next)
{
if (edge[i].w > 0 && !mark[edge[i].v]) dfs(edge[i].v);
}
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
init();
int wp[101], wm[101], w, len = 1, ans[105];
s = 0;
t = 2 * n + 1;
NN = 2 * n + 2;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &wp[i]);
addedge(i + n, t, wp[i]);
}
for (int i = 1; i <= n; ++i)
{
scanf("%d", &wm[i]);
addedge(s, i, wm[i]);
}
for (int i = 1; i <= m; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y + n, inf);
};
w = sap();
dfs(s);
for (int i = 1; i <= n; ++i)
{
if (!mark[i]) ans[len++] = i;
if (mark[i + n]) ans[len++] = i + n;
}
len--;
printf("%d\n%d\n", w, len);
for (int i = 1; i <= len; ++i)
{
if (ans[i] <=n) printf("%d -\n", ans[i]);
else printf("%d +\n", ans[i] - n);
}
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <cmath>
#define N 10010
#define M 20010
#define inf 1 << 30
#define eps 1 << 29
using namespace std;
int mark[N];
int cnt, n, m, s, t;
int head[N];
int NN;
struct edge
{
int v, next, w;
} edge[M];
void addedge(int u, int v, int w)
{
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
edge[cnt].v = u;
edge[cnt].w = 0;
edge[cnt].next = head[v];
head[v] = cnt++;
}
int sap()
{
int pre[N], cur[N], dis[N], gap[N];
int flow = 0, aug = inf, u;
bool flag;
for (int i = 1; i <= NN; ++i)
{
cur[i] = head[i];
gap[i] = dis[i] = 0;
}
gap[s] = NN;
u = pre[s] = s;
while (dis[s] < NN)
{
flag = 0;
for (int &j = cur[u]; j != -1; j = edge[j].next)
{
int v = edge[j].v;
if (edge[j].w > 0 && dis[u] == dis[v] + 1)
{
flag = 1;
if (edge[j].w < aug) aug = edge[j].w;
pre[v] = u;
u = v;
if (u == t)
{
flow += aug;
while (u != s)
{
u = pre[u];
edge[cur[u]].w -= aug;
edge[cur[u] ^ 1].w += aug;
}
aug = inf;
}
break;
}
}
if (flag)
continue;
int mindis = NN;
for (int j = head[u]; j != -1; j = edge[j].next)
{
int v = edge[j].v;
if (edge[j].w > 0 && dis[v] < mindis)
{
mindis = dis[v];
cur[u] = j;
}
}
if ((--gap[dis[u]]) == 0)
break;
gap[dis[u] = mindis + 1]++;
u = pre[u];
}
return flow;
}
void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
memset(mark, 0, sizeof(mark));
}
void dfs(int x) //不同于单纯的SAP,加入了一个搜素点集元素的函数,通过head数组的记录信息搜索。
{
mark[x] = 1;
for (int i = head[x]; i; i = edge[i].next)
{
if (edge[i].w > 0 && !mark[edge[i].v]) dfs(edge[i].v);
}
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
init();
int wp[101], wm[101], w, len = 1, ans[105];
s = 0;
t = 2 * n + 1;
NN = 2 * n + 2;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &wp[i]);
addedge(i + n, t, wp[i]);
}
for (int i = 1; i <= n; ++i)
{
scanf("%d", &wm[i]);
addedge(s, i, wm[i]);
}
for (int i = 1; i <= m; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y + n, inf);
};
w = sap();
dfs(s);
for (int i = 1; i <= n; ++i)
{
if (!mark[i]) ans[len++] = i;
if (mark[i + n]) ans[len++] = i + n;
}
len--;
printf("%d\n%d\n", w, len);
for (int i = 1; i <= len; ++i)
{
if (ans[i] <=n) printf("%d -\n", ans[i]);
else printf("%d +\n", ans[i] - n);
}
}
return 0;
}
- POJ2125:Destroying The Graph
- POJ2125 Destroying The Graph
- POJ2125 Destroying The Graph
- POJ2125:Destroying The Graph
- poj2125 Destroying The Graph
- [POJ2125]Destroying The Graph 做题笔记
- poj2125:Destroying The Graph(网络流)
- POJ2125 Destroying The Graph(最小割求割点)
- POJ2125 Destroying The Graph 最小点权覆盖
- 【POJ2125】【网络流】【最小割】Destroying The Graph 题解
- 【POJ2125】Destroying The Graph(最小权覆盖点集)
- 【poj2125】Destroying The Graph 二分图最小点权覆盖集
- POJ 2125 Destroying The Graph
- POJ 2125 Destroying The Graph
- poj 2125 Destroying The Graph
- poj 2125 Destroying The Graph
- POJ 2125 Destroying The Graph
- POJ 2125 Destroying The Graph
- ACM恺撒密码C++实现
- 给工厂分配公司代码
- out隐式对象
- 验证码与flash图片交换效果的实现
- 野指针
- POJ2125:Destroying The Graph
- ASP.NET系统用户权限在.NET中的设计与实现
- C语言编译全过程剖析
- 20111031总结:
- 定义采购组织(Purchasing Organization)
- pdfbox读取pdf到控制台
- c++异常处理机制示例及讲解
- 堆和栈的区别
- HDU3879:Base Station