Destroying The Graph
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7457 Accepted: 2373 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 +
一道经典的最小点权覆盖
解法:
- 拆点,从source到i建一条容量为i点入度的边,从i^sink建一条容量为i^的出度的边,i到i^的边的容量为无穷
- 求小割,就可得到最小花费,然后一次dfs即可求出所有的割边
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cassert>
using namespace std ;
typedef pair<int , int> PII ;
const int INF = 1<<27 ;
const int N = 2e2 + 11 ;
const int M = 2e6 + 11 ;
int head[N] , to[M] , flow[M] , info[M] ; int nedge ;
int dis[N] , gap[N] ; int gnode ;
bool vis[N] ;
int source , sink ;
int n ,m ;
vector<PII> ans ;
void add_edge(int a , int b , int c) {
if(head[a] ==-1) ++gnode ;
if(head[b] ==-1) ++gnode ;
to[++nedge] = b ;
flow[nedge] = c ;
info[nedge] = head[a] ;
head[a] = nedge ;
swap(a , b) ;
to[++nedge] = b ;
flow[nedge] = 0 ;
info[nedge] = head[a] ;
head[a] = nedge ;
}
void init() {
fill(head , head+N , -1) ; nedge = -1 ; gnode = 0 ;
source = 0 , sink = 201 ;
int a , b ;
for(int i = 1 ; i <= n ; ++i) {
scanf("%d" ,&a) ;
add_edge(i+100 , sink , a) ;
}
for(int i = 1; i <= n ; ++i) {
scanf("%d" ,&a) ;
add_edge(source , i , a) ;
}
while(m--) {
scanf("%d%d" ,&a ,&b) ;
add_edge(a , b+100 , INF) ;
}
}
int dfs(int u , int limit) {
if(u == sink) return limit ;
int f1 = 0 , f ;
int minh = gnode-1 ;
for(int i = head[u] ; i != -1 ; i = info[i]) {
int v = to[i] ;
if(flow[i] > 0) {
if(dis[v]+1 == dis[u]) {
f = dfs(v , min(limit , flow[i])) ;
flow[i] -= f ;
flow[i^1] += f ;
limit -= f ;
f1 += f ;
if(dis[source] >= gnode) return f1 ;
if(limit == 0) break ;
}
minh = min(minh , dis[v]) ;
}
}
if(f1 == 0) {
--gap[dis[u]] ;
if(gap[dis[u]] == 0) dis[source] = gnode ;
dis[u] = minh + 1 ;
++gap[dis[u]] ;
}
return f1 ;
}
void sap() {
fill(dis , dis+N , 0) ;
fill(gap , gap+N , 0) ;
gap[source] = gnode ;
int all = 0 ;
while(dis[source] < gnode) {
all += dfs(source , INF) ;
}
printf("%d\n" , all) ;
}
void dfs(int u) {
vis[u] = true ;
for(int i = head[u] ; i != -1 ; i = info[i]) {
int v = to[i] ;
if(vis[v]) continue ;
if(flow[i] > 0) {
dfs(v) ;
}else {
ans.push_back(make_pair(u , v)) ;
}
}
}
int main() {//freopen("data.in" , "r" , stdin) ;
while(scanf("%d%d" ,&n ,&m)==2) {
init() ;
sap() ;
ans.clear() ;
fill(vis , vis+N , 0);
dfs(source) ;
int cnt = 0 ;
for(int i = 0 ; i < ans.size() ; ++i) {
if(vis[ans[i].second]) continue ;
if(ans[i].first == source || ans[i].second == sink) ++cnt ;
}
printf("%d\n" , cnt) ;
for(int i = 0 ; i < ans.size() ; ++i) {
if(vis[ans[i].second]) continue ;
if(ans[i].first == source) printf("%d -\n" , ans[i].second) ;
if(ans[i].second == sink) printf("%d +\n" , ans[i].first-100) ;
}
}
}