您的位置:首页 > 其它

【二分图|最小点权覆盖】POJ-2125 Destroying The Graph

2014-11-03 22:08 483 查看
Destroying The Graph

Time Limit: 2000MS Memory Limit: 65536K
    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.
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 6
1 2 3
4 2 1
1 2
1 1
3 2
1 2
3 1
2 3

Sample Output
5
3
1 +
2 -
2 +

————————————————————無語の分割線————————————————————
前言:POJ上过了之后,ZOJ上一直过不了,折腾了一个晚上,发现是因为vis数组忘了清空了!!!
思路:首先建图,
拆掉所有弧——点覆盖集
点有两个权值——拆点为出点和入点
所有出点为X部,所有入点为Y部,二分图
建立源点S连接X部,权值为W-,建立汇点T连接Y部,权值为W+
接着转化为最小割,
一个点,要么匹配给S,要么匹配给T
每次匹配都要花费一定代价,每个匹配的边就是割边!最小花费就是最小割
最后剩下的是求割边集并输出——
从S能够通过残留网络走得到的点都属于X部,从T走的到的点都属于Y部
dfs染个色就行了
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 222, M = 11111;
int S, T, n, m;
int tot, head
, cur
, lev
, q
, s
;
bool vis[222];
struct Edge {
int u, v, w, next;
Edge(){}
Edge(int _u, int _v, int _w, int _next):
u(_u), v(_v), w(_w), next(_next){}
}edge[M];

void init()
{
tot = 0;
memset(head, -1, sizeof(head));
}

void add(int u, int v, int w)
{
edge[tot] = Edge(u, v, w, head[u]);
head[u] = tot++;
edge[tot] = Edge(v, u, 0, head[v]);
head[v] = tot++;
}

bool bfs()
{
memset(lev, -1, sizeof(lev));
lev[S] = 0;
int fron = 0, rear = 0;
q[rear++] = S;
while(fron < rear) {
int u = q[fron%N]; fron++;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v;
if(edge[i].w && lev[v] == -1) {
lev[v] = lev[u] + 1;
q[rear%N] = v; rear++;
if(v == T) return true;
}
}
}
return false;
}

int Dinic()
{
int ret = 0;
while(bfs()) {
memcpy(cur, head, sizeof(head));
int u = S, top = 0;
while(1) {
if(u == T) {
int mini = INF, loc;
for(int i = 0; i < top; i++) {
if(mini > edge[s[i]].w) {
mini = edge[s[i]].w;
loc = i;
}
}
for(int i = 0; i < top; i++) {
edge[s[i]].w -= mini;
edge[s[i]^1].w += mini;
}
ret += mini;
top = loc;
u = edge[s[top]].u;
}
int &i = cur[u];
for(; ~i; i = edge[i].next) {
int v = edge[i].v;
if(edge[i].w && lev[v] == lev[u] + 1) break;
}
if(~i) {
s[top++] = i;
u = edge[i].v;
}
else {
if(!top) break;
lev[u] = -1;
u = edge[s[--top]].u;
}
}
}
return ret;
}

vector <int> step;

void dfs(int u)
{
vis[u] = true;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v;
if(!vis[v] && edge[i].w) {
dfs(v);
}
}
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
int Cas, kase = 0;
scanf("%d", &Cas);
while(Cas--) {
scanf("%d%d", &n, &m);
int x, y;
S = 0; T = n<<1|1;
init();
for(int i = n+1; i <= (n<<1); i++) {
scanf("%d", &x);
add(i, T, x);
}
for(int i = 1; i <= n; i++) {
scanf("%d", &x);
add(S, i, x);
}
for(int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
add(x, y+n, INF);
}
int ans = Dinic();
if(kase++) printf("\n");
printf("%d\n", ans);
step.clear();
memset(vis, 0, sizeof(vis));
dfs(S);
for(int i = 1; i <= n; i++) {
if(!vis[i]) step.push_back(i);
if(vis[i+n]) step.push_back(i+n);
}
int len = step.size();
printf("%d\n", len);
for(int i = 0; i < len; i++) {
if(step[i] <= n) printf("%d-\n", step[i]);
else printf("%d+\n", step[i] - n);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: