题意:有一个n点m边的单向无环图,每个士兵可以从某一点沿着某方向出发,并不能返回,问最少要多少个士兵可以遍历全图。
这道题可以当做最小路径覆盖来做。当然也可以当上下界最小流来做。
最小路径覆盖的做法就不说了,百度搜一大把。
上下界最小流建图方式:
把每一个点 i 拆开为两个点 i 和 i′
对于每个点建边 i -> i′ 流量下界为 1 ,上界为 oo
若原图存在边 e(u,v) ,那么建边 u′ -> v ,流量下界为 0 ,上界为 oo
然后建立超级源点 sst 和超级汇点 eet
以超级源点和超级汇点跑一遍Dinic
然后再加边 e(et,st,0,oo)
再以超级源点和超级汇点跑一遍Dinic,此时的流量为最小流。
代码1(有源汇上下界网络流 最小流):
//author: CHC
//First Edit Time: 2015-09-10 14:35
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1e+4;
const int MAXM=1e+6;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
struct Edge {
int to,ci,next;
Edge(){}
Edge(int _to,int _ci,int _next):to(_to),ci(_ci),next(_next){}
}e[MAXM];
int head[MAXN],tot;
void init(){
memset(head,-1,sizeof(head));
tot=0;
}
void AddEdge(int u,int v,int ci0,int ci1=0){
e[tot]=Edge(v,ci0,head[u]);
head[u]=tot++;
e[tot]=Edge(u,ci1,head[v]);
head[v]=tot++;
}
int dis[MAXN],sta[MAXN],top,cur[MAXN];
bool bfs(int st,int et){
memset(dis,0,sizeof(dis));
dis[st]=1;
queue <int> q;q.push(st);
while(!q.empty()){
int now=q.front();q.pop();
for(int i=head[now];~i;i=e[i].next)
if(e[i].ci&&!dis[e[i].to]){
dis[e[i].to]=dis[now]+1;
q.push(e[i].to);
}
}
return dis[et]!=0;
}
LL Dinic(int st,int et){
LL ans=0;
while(bfs(st,et)){
top=0;
memcpy(cur,head,sizeof(head));
int u=st;
while(1){
//printf("u:%d\n",u);
if(u==et){
int pos,minn=INF;
for(int i=0;i<top;i++){
if(minn>e[sta[i]].ci){
minn=e[sta[i]].ci;
pos=i;
}
//printf("%d%c-->%d%c\n",e[sta[i]^1].to>>1,e[sta[i]^1].to&1?'\'':' ',e[sta[i]].to>>1,e[sta[i]].to&1?'\'':' ');
//printf("%d-->%d\n",e[sta[i]^1].to,e[sta[i]].to);
}
//puts("");
for(int i=0;i<top;i++){
e[sta[i]].ci-=minn;
e[sta[i]^1].ci+=minn;
}
top=pos;
u=e[sta[top]^1].to;
ans+=minn;
//printf("minn:%d\n",minn);
}
for(int i=cur[u];~i;cur[u]=i=e[i].next)
if(e[i].ci&&dis[u]+1==dis[e[i].to])break;
if(cur[u]!=-1){
sta[top++]=cur[u];
u=e[cur[u]].to;
}
else {
if(top==0)break;
dis[u]=0;
u=e[sta[--top]^1].to;
}
}
}
return ans;
}
int du[MAXN],ru[MAXN],chu[MAXN],n,m;
int main()
{
while(~scanf("%d%d",&n,&m)){
if(n+m==0)break;
init();
memset(du,0,sizeof(du));
memset(ru,0,sizeof(ru));
memset(chu,0,sizeof(chu));
int st=0,et=1;
for(int i=1;i<=n;i++){
AddEdge(i<<1,i<<1|1,INF-1);
chu[i<<1]++;
ru[i<<1|1]++;
du[i<<1]--;
du[i<<1|1]++;
}
for(int i=0,x,y;i<m;i++){
scanf("%d%d",&x,&y);
AddEdge(x<<1|1,y<<1,INF);
//printf("%d-->%d\n",x,y);
chu[x<<1|1]++;
ru[y<<1]++;
}
int sst=n*2+2,eet=n*2+3;
LL ctot=0;
for(int i=1;i<=n;i++){
if(ru[i<<1]==0){
AddEdge(st,i<<1,INF);
//printf("%d-->%d\n",st,i);
}
if(chu[i<<1|1]==0){
AddEdge(i<<1|1,et,INF);
//printf("%d-->%d\n",i,et);
}
if(du[i<<1] > 0)AddEdge(sst,i<<1,du[i<<1]),ctot+=du[i<<1];//,printf("vs-->%d\n",i<<1);
else AddEdge(i<<1,eet,-du[i<<1]);//,printf("%d-->vt\n",i<<1);
if(du[i<<1|1]>0)AddEdge(sst,i<<1|1,du[i<<1|1]),ctot+=du[i<<1|1];//,printf("vs-->%d\n",i<<1|1);
else AddEdge(i<<1|1,eet,-du[i<<1|1]);
}
//puts("");
//printf("sst:%d eet:%d\n",sst,eet);
LL ans=Dinic(sst,eet);
AddEdge(et,st,INF);
LL t=Dinic(sst,eet);
//printf("%d\n",e[tot-1].ci);
printf("%I64d\n",t);
}
return 0;
}
代码2(匹配):
//author: CHC
//First Edit Time: 2015-05-10 12:56
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1e+4;
const int MAXM=1e+6;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int dp[510][510],n,m;
struct Edge
{
int from,to,ci,next;
Edge(){}
Edge(int _from,int _to,int _ci,int _next):from(_from),to(_to),ci(_ci),next(_next){}
}e[MAXM];
int head[MAXN],tot;
int dis[MAXN];
int top,sta[MAXN],cur[MAXN];
inline void init(){
memset(head,-1,sizeof(head));
tot=0;
}
inline void AddEdge(int u,int v,int ci0,int ci1=0){
e[tot]=Edge(u,v,ci0,head[u]);
head[u]=tot++;
e[tot]=Edge(v,u,ci1,head[v]);
head[v]=tot++;
}
inline bool bfs(int st,int et){
memset(dis,0,sizeof(dis));
dis[st]=1;
queue <int> q;
q.push(st);
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=head[now];i!=-1;i=e[i].next){
int next=e[i].to;
if(e[i].ci&&!dis[next]){
dis[next]=dis[now]+1;
if(next==et)return true;
q.push(next);
}
}
}
return false;
}
LL Dinic(int st,int et){
LL ans=0;
while(bfs(st,et)){
//printf("here\n");
top=0;
memcpy(cur,head,sizeof(head));
int u=st,i;
while(1){
if(u==et){
int pos,minn=INF;
//printf("top:%d\n",top);
for(i=0;i<top;i++)
{
if(minn>e[sta[i]].ci){
minn=e[sta[i]].ci;
pos=i;
}
//printf("%d --> %d\n",e[sta[i]].from,e[sta[i]].to);
}
for(i=0;i<top;i++){
e[sta[i]].ci-=minn;
e[sta[i]^1].ci+=minn;
}
top=pos;
u=e[sta[top]].from;
ans+=minn;
//printf("minn:%d\n\n",minn);
}
for(i=cur[u];i!=-1;cur[u]=i=e[i].next)
if(e[i].ci&&dis[u]+1==dis[e[i].to])break;
if(cur[u]!=-1){
sta[top++]=cur[u];
u=e[cur[u]].to;
}
else {
if(top==0)break;
dis[u]=0;
u=e[sta[--top]].from;
}
}
}
return ans;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
if(n+m==0)break;
memset(dp,0,sizeof(dp));
init();
for(int i=0,x,y;i<m;i++){
scanf("%d%d",&x,&y);
dp[x][y]=1;
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
if(dp[i][k])
for(int j=1;j<=n;j++)
dp[i][j]|=dp[i][k]*dp[k][j];
int st=0,et=n*3;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(i!=j&&dp[i][j]){
AddEdge(i<<1,j<<1|1,1);
}
}
for(int i=1;i<=n;i++)
AddEdge(st,i<<1,1),AddEdge(i<<1|1,et,1);
printf("%I64d\n",n-Dinic(st,et));
}
return 0;
}