网络流/二分图最小点权覆盖


  果然还是应该先看下胡伯涛的论文……

  orz proverbs

  汗……输出方案我WA了N次T_T,直接从S进行dfs,对于左边的点,如果走不到则表明 s->i 这条边被割掉了,对于右边的点,如果走的到则表明 i+n->t 这条边被割掉了,因为如果没割掉就直接从这个点走到t了……唉我一开始居然没想到

 Source Code
Problem: User: sdfzyhy
Memory: 848K Time: 79MS
Language: G++ Result: Accepted Source Code //BZOJ 2125
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define fore(i,x) for(int i=head[x];i;i=next[i])
#define pb push_back
using namespace std;
inline int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>''){ if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<=''){ v=v*+ch-''; ch=getchar();}
return v*sign;
}
const int N=,M=,INF=~0u>>;
typedef long long LL;
/******************tamplate*********************/ struct edge{
int from,to,v;
};
int n,m;
struct Net{
edge E[M];
int head[N],next[M],cnt;
void add(int x,int y,int z){
E[++cnt]=(edge){x,y,z};
next[cnt]=head[x]; head[x]=cnt;
E[++cnt]=(edge){y,x,};
next[cnt]=head[y]; head[y]=cnt;
}
int s,t,d[N],cur[N],Q[N];
void init(){
n=getint(); m=getint();
s=; t=n*+; cnt=;
int x,y;
F(i,,n){
x=getint();
add(i+n,t,x);
}
F(i,,n){
x=getint();
add(s,i,x);
}
F(i,,m){
x=getint(); y=getint();
add(x,y+n,INF);
}
}
bool mklevel(){
memset(d,-,sizeof d);
d[s]=;
int l=,r=-;
Q[++r]=s;
while(l<=r){
int x=Q[l++];
fore(i,x){
edge&e=E[i];
if (d[e.to]==- && e.v>){
d[e.to]=d[x]+;
Q[++r]=e.to;
}
}
}
return d[t]!=-;
}
int dfs(int x,int a){
if (x==t) return a;
int flow=;
for(int &i=cur[x];i && flow<a;i=next[i]){
edge&e=E[i];
if (!e.v || d[e.to]!=d[x]+) continue;
int f=dfs(e.to,min(a-flow,e.v));
if (f>){
flow+=f;
e.v-=f;
E[i^].v+=f;
}
}
if (!flow) d[x]=-;
return flow;
}
int Dinic(){
int flow=;
while(mklevel()){
F(i,s,t) cur[i]=head[i];
flow+=dfs(s,INF);
}
return flow;
}
bool vis[N];
void dfs1(int x){
if (vis[x]) return;
vis[x]=;
for(int i=head[x];i;i=next[i])
if (E[i].v) dfs1(E[i].to);
}
void solve(){
printf("%d\n",Dinic());
int num=;
memset(vis,,sizeof vis);
dfs1(s);
F(i,,n) num+=(!vis[i])+vis[i+n];
printf("%d\n",num);
F(i,,n){
if (!vis[i]) printf("%d -\n",i);
if (vis[i+n]) printf("%d +\n",i);
}
}
}G1; int main(){
#ifndef ONLINE_JUDGE
freopen("2125.in","r",stdin);
freopen("2125.out","w",stdout);
#endif
G1.init();
G1.solve();
return ;
}
Destroying The Graph
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7511 Accepted: 2399 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 10
. 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

Northeastern Europe 2003, Northern Subregion

[Submit]   []   [Status]   [Discuss]

05-11 09:38
查看更多