poj3204
暴搞。
注意如果直接增加割边容量,从空图开始网络流可能会超时。记录一下原图的流量情况,每次枚举恢复一下原图就ok了。
暴搞。
显然,增加的必定是割边的容量。如果不是割边,就有剩余容量,原来的容量都流不满,再加也没用。
注意如果直接增加割边容量,从空图开始网络流可能会超时。记录一下原图的流量情况,每次枚举恢复一下原图就ok了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define INF 5200000
#define NN 520
#define MM 10100
struct edge{int u,v,c;}ed[MM];//MM要为连接关系数量的两倍
int fi[NN],ne[MM],te,lev[NN];
int pe[NN],cap[NN],tc[NN],sta[NN];//模拟栈用数组
void init(){
memset(fi,-1,sizeof(fi));
te=0;
}
void insert(int u,int v,int c){
ed[te].u=u;ed[te].v=v;ed[te].c=c;
ne[te]=fi[u];fi[u]=te++;
ed[te].u=v;ed[te].v=u;ed[te].c=0;
ne[te]=fi[v];fi[v]=te++;
}
int dinic_bfs(int s,int t){
memset(lev,0,sizeof(lev));
lev[s]=1;
queue<int> q;
q.push(s);
int u,v,e,c;
while(!q.empty()){
u=q.front();q.pop();
for(e=fi[u];e!=-1;e=ne[e]){
v=ed[e].v;c=ed[e].c;
if (!lev[v]&&c){
lev[v]=lev[u]+1;
q.push(v);
}
}
if (u==t) break;
}
return lev[t];
}
int dinic_dfs1(int u,int t,int cap){//直接深搜
int tc=cap;
if (u==t) return cap;
int v,e,c,tf;
for(e=fi[u];e!=-1&&tc;e=ne[e]){
v=ed[e].v;
c=ed[e].c;
if (lev[v]==lev[u]+1&&c>0){
tf=dinic_dfs1(v,t,min(tc,c));
tc-=tf;
ed[e].c-=tf;
ed[e^1].c+=tf;
}
}
if (e==-1) lev[u]=-10;//重要优化
return cap-tc;
}
int dinic_dfs(int s,int t,int tn){//模拟栈深搜
int top=0,i,flow=0;
sta[++top]=s;
cap[s]=tc[s]=INF;
pe[s]=fi[s];
int u,v,c,tf;
while(top){
u=sta[top];
if (u!=t)
for(int &e=pe[u];e!=-1&&tc[u];e=ne[e]){
v=ed[e].v;c=ed[e].c;
if (lev[v]==lev[u]+1&&c){
sta[++top]=v;pe[v]=fi[v];tc[v]=cap[v]=min(c,tc[u]);
break;
}
}
else tc[u]=0;
if (tc[u]==0||pe[u]==-1) {
tf=cap[u]-tc[u];
if (pe[u]==-1) lev[u]=-10;//重要优化
top--;
if (top){
tc[sta[top]]-=tf;
ed[pe[sta[top]]].c-=tf;
ed[pe[sta[top]]^1].c+=tf;
pe[sta[top]]=ne[pe[sta[top]]];
}
else flow+=tf;
}
}
return flow;
}
int dinic(int s,int t,int tn){//起点终点相等需要特判
int flow=0;
int tf=0;
while(dinic_bfs(s,t)){
flow+=dinic_dfs(s,t,t+1);
}
return flow;
}
int aa[MM],s,t,n,m,ta,sc[MM];
int main(){
int i,a,b,c,j,ans,ans1,ans2;
scanf("%d%d",&n,&m);
init();
for(i=1;i<=m;++i){
scanf("%d%d%d",&a,&b,&c);
insert(a,b,c);
}
s=0;t=n-1;
ans1=dinic(s,t,t+1);
ta=0;
for(i=0;i<te;i+=2)if (ed[i].c==0){//记录割边
aa[++ta]=i;
}
for(i=0;i<te;++i) sc[i]=ed[i].c;
ans=0;
for(i=1;i<=ta;++i){
for(j=0;j<te;++j){
ed[j].c=sc[j];
}
ed[aa[i]].c=sc[aa[i]]+1;
ans2=dinic(s,t,t+1);
if (ans2>0) ans++;
}
printf("%d\n",ans);
return 0;
}