正文
增加 66 行删除 66 行
121 行未改动全部展开12212312412512612712812913013113213315 行未改动14915015115215315415515615715815916016116216316416516616716816933 行未改动2032042052062072082092102113 行未改动2152162172182192202212222232242252263 行未改动2302312322332342352362372382392402412422432442458 行未改动25425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231311 行未改动32532632732832933033133233311 行未改动3453463473483493503513523533 行未改动3573583593603613623633643653663673683694 行未改动3743753763773783793803813823833843853863873883898 行未改动39839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946011 行未改动47247347447547647747847948031 行未改动51251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754819 行未改动56856957057157257357457557626 行未改动121 行未改动全部展开}void EK(){while(bfs()){ll minweight=INF;for(ll i=t;pre[i].v>0;i=pre[i].v){ //找到最小边权minweight=min(minweight,edge[pre[i].edgenum].weight);}for(ll i=t;pre[i].v>0;i=pre[i].v){ //依次加上或者减去edge[pre[i].edgenum].weight-=minweight;edge[pre[i].edgenum^1].weight+=minweight;}ans+=minweight;15 行未改动while(!q.empty()){now=q.front();q.pop();for(ll i=head[now];i>=0;i=edge[i].nxt){if(!inqueue[edge[i].to] && edge[i].weight>0){ //之前没加入到队列中,且是增广路pre[edge[i].to].edgenum=i; //记录这条边的编号pre[edge[i].to].v=now; //记录上一个点inqueue[edge[i].to]=true; //入队q.push(edge[i].to);}}if(now==t){//如果当前点是终点,那么说明找到了增广路return 1;}}return 0;}inline void add(ll x,ll y,ll w){edge[cnt].from=x;edge[cnt].to=y;edge[cnt].nxt=head[x];33 行未改动下面就是喜闻乐见的代码实现了(==**不加优化和优化一只差一行,在$dfs$函数里面,仅多了一行剪枝**==):```cpp#include <bits/stdc++.h>#define ll long long#define INF 999999999999999using namespace std;const int N=205;const int M=1e4+9;3 行未改动typedef struct{ll head[N],cnt,n,m,s,t,depth[N],maxflow; //定义图相关数据Edge edge[M];bool inqueue[N];//定义操作图的方法inline void init(){ //图的初始化n=read();m=read();s=read();t=read();3 行未改动}cnt=0;maxflow=0;}inline void fresh(){ //每次bfs前都要调用一次,消除掉上一次bfs的结果for(int i=0;i<=n;i++){inqueue[i]=false;depth[i]=-1;}}inline void add(){ //加边,同时加0反向边ll x,y,w;x=read();y=read();8 行未改动edge[cnt].weight=0;edge[cnt].nxt=head[y];head[y]=cnt++;}inline bool bfs(){ //对残量网络进行bfs,找到最短的增广路,并且对图进行分层queue<ll> q;ll now;fresh(); //把上一轮bfs的pre和depth消除了q.push(s);depth[s]=0;inqueue[s]=true;while(!q.empty()){now=q.front();q.pop();for(ll i=head[now];i>=0;i=edge[i].nxt){if(!inqueue[edge[i].to] && edge[i].weight>0){ //找没访问过的且有剩余流量的边inqueue[edge[i].to]=true; //入队标记防止重复访问depth[edge[i].to]=depth[now]+1; //分层q.push(edge[i].to);}}}if(depth[t]==-1) { //对整个图bfs完了之后再看能否到treturn false;}return true;}ll dfs(ll now,ll sumflow){//sumflow可以认为是流入这个点的流量ll useflow=0; //统计流出的流量if(now==t){return sumflow;}for(ll i=head[now];i>=0 && sumflow>0;i=edge[i].nxt){ //注意流量不能变成负的if(depth[edge[i].to]==depth[now]+1 && edge[i].weight>0){ //既不能超过边的容量,也不能超过剩下的没流走的流量ll k=dfs(edge[i].to,min(edge[i].weight,sumflow));if(k==0) depth[edge[i].to]=-1; //重要的剪枝edge[i].weight-=k;edge[i^1].weight+=k;useflow+=k;sumflow-=k;}}return useflow;}void dinic(){ //图的最大流算法while(bfs()){maxflow+=dfs(s,INF);}}inline ll read(){ //快读,可以不要ll ret=0,f=1;char ch=getchar();while(ch<'0' || ch>'9'){11 行未改动}Graph;Graph graph;int main(){// #define DEBUG#ifdef DEBUGfreopen("in.txt","r",stdin);#endifgraph.init();11 行未改动代码如下,==**请注意比较和上一份代码的不同,您可以把两份代码拷贝到**[这里](http://www.jq22.com/textDifference)**迅速看出他们的不同**==:```cpp#include <bits/stdc++.h>#define ll long long#define INF 999999999999999using namespace std;const int N=205;const int M=1e4+9;3 行未改动typedef struct{ll head[N],cnt,n,m,s,t,depth[N],maxflow; //定义图相关数据Edge edge[M];bool inqueue[N];ll cur[N];//定义操作图的方法inline void init(){ //图的初始化n=read();m=read();s=read();t=read();4 行未改动}cnt=0;maxflow=0;}inline void fresh(){ //每次bfs前都要调用一次,消除掉上一次bfs的结果for(int i=0;i<=n;i++){inqueue[i]=false;depth[i]=-1;}}inline void add(){ //加边,同时加0反向边ll x,y,w;x=read();y=read();8 行未改动edge[cnt].weight=0;edge[cnt].nxt=head[y];head[y]=cnt++;}inline bool bfs(){ //对残量网络进行bfs,找到最短的增广路,并且对图进行分层queue<ll> q;ll now;fresh(); //把上一轮bfs的pre和depth消除了q.push(s);depth[s]=0;inqueue[s]=true;cur[s]=head[s];while(!q.empty()){now=q.front();q.pop();for(ll i=head[now];i>=0;i=edge[i].nxt){if(!inqueue[edge[i].to] && edge[i].weight>0){ //找没访问过的且有剩余流量的边inqueue[edge[i].to]=true; //入队标记防止重复访问depth[edge[i].to]=depth[now]+1; //分层cur[edge[i].to]=head[edge[i].to];q.push(edge[i].to);}}}if(depth[t]==-1) { //对整个图bfs完了之后再看能否到treturn false;}return true;}ll dfs(ll now,ll sumflow){//sumflow可以认为是流入这个点的流量ll useflow=0; //统计流出的流量if(now==t){return sumflow;}for(ll i=cur[now];i>=0 && sumflow>0;i=edge[i].nxt){cur[now]=i;if(depth[edge[i].to]==depth[now]+1 && edge[i].weight>0){ //既不能超过边的容量,也不能超过剩下的没流走的流量ll k=dfs(edge[i].to,min(edge[i].weight,sumflow));if(k==0) depth[edge[i].to]=-1; //重要的剪枝edge[i].weight-=k;edge[i^1].weight+=k;useflow+=k;sumflow-=k;}}return useflow;}void dinic(){ //图的最大流算法while(bfs()){maxflow+=dfs(s,INF);}}inline ll read(){ //快读,可以不要ll ret=0,f=1;char ch=getchar();while(ch<'0' || ch>'9'){11 行未改动}Graph;Graph graph;int main(){// #define DEBUG#ifdef DEBUGfreopen("in.txt","r",stdin);#endifgraph.init();31 行未改动Edge edge[M];ll head[N],n,m,s,t,cnt,dist[N],maxflow,mincost;Pre pre[N];bool inqueue[N];inline void init(){scanf("%lld %lld %lld %lld",&n,&m,&s,&t);cnt=0;for(int i=0;i<=n;i++){head[i]=-1;}maxflow=0;mincost=0;}inline void add(ll x,ll y,ll w,ll f){edge[cnt].to=y;edge[cnt].nxt=head[x];edge[cnt].capacity=w;edge[cnt].cost=f;head[x]=cnt++;}inline void fresh(){for(ll i=0;i<=n;i++){inqueue[i]=false;pre[i].edgenum=-1;pre[i].nodenum=0;dist[i]=INF;}}inline bool spfa(){ //找消费最低的增广路进行增广fresh();queue<ll> q;ll now;q.push(s);19 行未改动return true;}return false;}inline void EK(){ll minweight=INF;while(spfa()){minweight=INF;26 行未改动121 行未改动全部展开12212312412512612712812913013113213315 行未改动14915015115215315415515615715815916016116216316416516616716816933 行未改动2032042052062072082092102113 行未改动2152162172182192202212222232242252263 行未改动2302312322332342352362372382392402412422432442458 行未改动25425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231311 行未改动32532632732832933033133233311 行未改动3453463473483493503513523533 行未改动3573583593603613623633643653663673683694 行未改动3743753763773783793803813823833843853863873883898 行未改动39839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946011 行未改动47247347447547647747847948031 行未改动51251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754819 行未改动56856957057157257357457557626 行未改动121 行未改动全部展开}void EK(){while(bfs()){ll minweight=INF;for(ll i=t;pre[i].v>0;i=pre[i].v){ //找到最小边权minweight=min(minweight,edge[pre[i].edgenum].weight);}for(ll i=t;pre[i].v>0;i=pre[i].v){ //依次加上或者减去edge[pre[i].edgenum].weight-=minweight;edge[pre[i].edgenum^1].weight+=minweight;}ans+=minweight;15 行未改动while(!q.empty()){now=q.front();q.pop();for(ll i=head[now];i>=0;i=edge[i].nxt){if(!inqueue[edge[i].to] && edge[i].weight>0){ //之前没加入到队列中,且是增广路pre[edge[i].to].edgenum=i; //记录这条边的编号pre[edge[i].to].v=now; //记录上一个点inqueue[edge[i].to]=true; //入队q.push(edge[i].to);}}if(now==t){//如果当前点是终点,那么说明找到了增广路return 1;}}return 0;}inline void add(ll x,ll y,ll w){edge[cnt].from=x;edge[cnt].to=y;edge[cnt].nxt=head[x];33 行未改动下面就是喜闻乐见的代码实现了(==**不加优化和优化一只差一行,在$dfs$函数里面,仅多了一行剪枝**==):```cpp#include <bits/stdc++.h>#define ll long long#define INF 999999999999999using namespace std;const int N=205;const int M=1e4+9;3 行未改动typedef struct{ll head[N],cnt,n,m,s,t,depth[N],maxflow; //定义图相关数据Edge edge[M];bool inqueue[N];//定义操作图的方法inline void init(){ //图的初始化n=read();m=read();s=read();t=read();3 行未改动}cnt=0;maxflow=0;}inline void fresh(){ //每次bfs前都要调用一次,消除掉上一次bfs的结果for(int i=0;i<=n;i++){inqueue[i]=false;depth[i]=-1;}}inline void add(){ //加边,同时加0反向边ll x,y,w;x=read();y=read();8 行未改动edge[cnt].weight=0;edge[cnt].nxt=head[y];head[y]=cnt++;}inline bool bfs(){ //对残量网络进行bfs,找到最短的增广路,并且对图进行分层queue<ll> q;ll now;fresh(); //把上一轮bfs的pre和depth消除了q.push(s);depth[s]=0;inqueue[s]=true;while(!q.empty()){now=q.front();q.pop();for(ll i=head[now];i>=0;i=edge[i].nxt){if(!inqueue[edge[i].to] && edge[i].weight>0){ //找没访问过的且有剩余流量的边inqueue[edge[i].to]=true; //入队标记防止重复访问depth[edge[i].to]=depth[now]+1; //分层q.push(edge[i].to);}}}if(depth[t]==-1) { //对整个图bfs完了之后再看能否到treturn false;}return true;}ll dfs(ll now,ll sumflow){//sumflow可以认为是流入这个点的流量ll useflow=0; //统计流出的流量if(now==t){return sumflow;}for(ll i=head[now];i>=0 && sumflow>0;i=edge[i].nxt){ //注意流量不能变成负的if(depth[edge[i].to]==depth[now]+1 && edge[i].weight>0){ //既不能超过边的容量,也不能超过剩下的没流走的流量ll k=dfs(edge[i].to,min(edge[i].weight,sumflow));if(k==0) depth[edge[i].to]=-1; //重要的剪枝edge[i].weight-=k;edge[i^1].weight+=k;useflow+=k;sumflow-=k;}}return useflow;}void dinic(){ //图的最大流算法while(bfs()){maxflow+=dfs(s,INF);}}inline ll read(){ //快读,可以不要ll ret=0,f=1;char ch=getchar();while(ch<'0' || ch>'9'){11 行未改动}Graph;Graph graph;int main(){// #define DEBUG#ifdef DEBUGfreopen("in.txt","r",stdin);#endifgraph.init();11 行未改动代码如下,==**请注意比较和上一份代码的不同,您可以把两份代码拷贝到**[这里](http://www.jq22.com/textDifference)**迅速看出他们的不同**==:```cpp#include <bits/stdc++.h>#define ll long long#define INF 999999999999999using namespace std;const int N=205;const int M=1e4+9;3 行未改动typedef struct{ll head[N],cnt,n,m,s,t,depth[N],maxflow; //定义图相关数据Edge edge[M];bool inqueue[N];ll cur[N];//定义操作图的方法inline void init(){ //图的初始化n=read();m=read();s=read();t=read();4 行未改动}cnt=0;maxflow=0;}inline void fresh(){ //每次bfs前都要调用一次,消除掉上一次bfs的结果for(int i=0;i<=n;i++){inqueue[i]=false;depth[i]=-1;}}inline void add(){ //加边,同时加0反向边ll x,y,w;x=read();y=read();8 行未改动edge[cnt].weight=0;edge[cnt].nxt=head[y];head[y]=cnt++;}inline bool bfs(){ //对残量网络进行bfs,找到最短的增广路,并且对图进行分层queue<ll> q;ll now;fresh(); //把上一轮bfs的pre和depth消除了q.push(s);depth[s]=0;inqueue[s]=true;cur[s]=head[s];while(!q.empty()){now=q.front();q.pop();for(ll i=head[now];i>=0;i=edge[i].nxt){if(!inqueue[edge[i].to] && edge[i].weight>0){ //找没访问过的且有剩余流量的边inqueue[edge[i].to]=true; //入队标记防止重复访问depth[edge[i].to]=depth[now]+1; //分层cur[edge[i].to]=head[edge[i].to];q.push(edge[i].to);}}}if(depth[t]==-1) { //对整个图bfs完了之后再看能否到treturn false;}return true;}ll dfs(ll now,ll sumflow){//sumflow可以认为是流入这个点的流量ll useflow=0; //统计流出的流量if(now==t){return sumflow;}for(ll i=cur[now];i>=0 && sumflow>0;i=edge[i].nxt){cur[now]=i;if(depth[edge[i].to]==depth[now]+1 && edge[i].weight>0){ //既不能超过边的容量,也不能超过剩下的没流走的流量ll k=dfs(edge[i].to,min(edge[i].weight,sumflow));if(k==0) depth[edge[i].to]=-1; //重要的剪枝edge[i].weight-=k;edge[i^1].weight+=k;useflow+=k;sumflow-=k;}}return useflow;}void dinic(){ //图的最大流算法while(bfs()){maxflow+=dfs(s,INF);}}inline ll read(){ //快读,可以不要ll ret=0,f=1;char ch=getchar();while(ch<'0' || ch>'9'){11 行未改动}Graph;Graph graph;int main(){// #define DEBUG#ifdef DEBUGfreopen("in.txt","r",stdin);#endifgraph.init();31 行未改动Edge edge[M];ll head[N],n,m,s,t,cnt,dist[N],maxflow,mincost;Pre pre[N];bool inqueue[N];inline void init(){scanf("%lld %lld %lld %lld",&n,&m,&s,&t);cnt=0;for(int i=0;i<=n;i++){head[i]=-1;}maxflow=0;mincost=0;}inline void add(ll x,ll y,ll w,ll f){edge[cnt].to=y;edge[cnt].nxt=head[x];edge[cnt].capacity=w;edge[cnt].cost=f;head[x]=cnt++;}inline void fresh(){for(ll i=0;i<=n;i++){inqueue[i]=false;pre[i].edgenum=-1;pre[i].nodenum=0;dist[i]=INF;}}inline bool spfa(){ //找消费最低的增广路进行增广fresh();queue<ll> q;ll now;q.push(s);19 行未改动return true;}return false;}inline void EK(){ll minweight=INF;while(spfa()){minweight=INF;26 行未改动