图中虚线是反向弧。在检查飞船某天是否从A->B时要去检查原来的m条边对应的边是否流量不为0
1、MAXN要开到2500
2、f+=MaxFlow(s,limit),写成MaxFlow(s,INF)会wa
3、要考虑到电脑被移到u[i]的情况(每台电脑一天只能走一条边)
#include<iostream>
#include<queue>
#include<cstring>
#include<stdio.h>
using namespace std;
const int MAXN = 2500; //开小了RE 事实上是增点了的(在增边的时候增的点) 最多k天,每天增加n个点,所以最多k*n
const int INF=0x3f3f3f3f;
struct Edge
{
int from, to, cap, flow;
Edge(){}
Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
bool operator < (const Edge& e) const
{
return from < e.from || (from == e.from && to < e.to);
}
};
struct dinic
{
int n,m,s,t;
vector<Edge>edges;
vector<int>G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n)
{
this->n=n;
edges.clear();
for(int i=0;i<MAXN;++i)
G[i].clear();
}
void AddEdge(int u,int v,int cap)
{
edges.push_back(Edge(u,v,cap,0));
edges.push_back(Edge(v,u,0,0));
m=edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
bool bfs()
{
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = 0; i < G[x].size(); i++){
Edge& e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||!a) return a;
int flow=0,f=0;
for(int& i=cur[x];i<G[x].size();++i)
{
Edge &e= edges[G[x][i]];
if(d[e.to]==d[x]+1&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(!a) break;
}
}
return flow;
}
int MaxFlow(int s,int t,int limit)
{
this->s=s;
this->t=t;
int flow=0;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,limit); //我写成Inf了wa到死 不知道为啥
if(flow>=limit)
{
return limit;
}
}
return flow;
}
}dc;
int n,m,k,s,t;
Edge edges[250];
int main()
{
while(scanf("%d%d%d%d%d",&n,&m,&k,&s,&t)!=EOF)
{
dc.init(n);
for(int i=0;i<m;++i)
{
scanf("%d%d",&edges[i].from,&edges[i].to);
}
int day=1;
int flow=0;
while(1)
{
for(int i=0;i<n;++i)
{
dc.AddEdge((day-1)*n+i,day*n+i,INF);//增边1 在day和day+1原地不动
}
for(int i=0;i<m;++i) //增边2 在day和day+1天之间建边
{
int u=edges[i].from;
int v=edges[i].to;
dc.AddEdge((day-1)*n+u,day*n+v,1);
dc.AddEdge((day-1)*n+v,day*n+u,1);
}
flow+=dc.MaxFlow(s,day*n+t,k-flow);
if(flow>=k) break;
day++;
}
printf("%d\n",day);
vector<int>location(k,s);
int idx=0;
while(day--)
{
idx+=2*n; //把都在源点的情况视为第0天 ,(这里是跳过所有原地不动的情况 增边1)接着遍历增边2 检查是否有流量
vector<int>u,v,vis(k,0);
for(int i=0;i<m;++i)//m次循环后
{
//2*n是第一个边第0天到第1天的连线
int f1=dc.edges[idx].flow; //跳过反向弧 不允许从第1天走回第0天 第一天A - > B
idx+=2;
int f2=dc.edges[idx].flow; //第一天B - > A
idx+=2;
if(f1&&!f2) {u.push_back(edges[i].from); v.push_back(edges[i].to);}
if(!f1&&f2) {u.push_back(edges[i].to); v.push_back(edges[i].from);}
}
printf("%d",u.size());
for(int i=0;i<u.size();++i)
{
for(int j=0;j<k;++j)
{
if(!vis[j]&&location[j]==u[i]) // 所有在该天一开始就在起点u[i]的船会走到v[i] 而被搬到u[i]的点不能再被搬运了,用vis标记
{
printf(" %d %d",j+1,v[i]);
location[j]=v[i];
vis[j]=1;
break; //每天只能一个飞机
}
}
}
printf("\n");
}
}
}