社区讨论
求助 网络流有什么好的debug方法
P2402奶牛隐藏参与者 4已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mi6nudz3
- 此快照首次捕获于
- 2025/11/20 07:55 4 个月前
- 此快照最后确认于
- 2025/11/20 07:55 4 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
#define s 2*n+1
#define e 2*n+2
#define LL long long
#define MN 100010
#define MM 1550
#define so(i,j,k,l) for(int i=j;i<=k;i+=l)
#define INF 2100000000
//queue<int>q;
int dep[2*MN],n,h[MN*2],tot,aa[MN],b[MN],sum,m,x,y,z; LL ans;
LL dis[233][233];
struct lll{
int to,ne,w,op;
lll(){to=ne=w=op=0;}
}a[MN];
void link(int x,int y,int w)
{
a[++tot].to=y,a[tot].ne=h[x],h[x]=tot,a[tot].w=w;
}
/*bool bfs(int u)
{
memset(dep,-1,sizeof(dep));
while(!q.empty()) q.pop();
dep[s]=1;q.push(s);
while (!q.empty())
{
for(int i=h[q.front()];i;i=a[i].ne)
{
if(dep[a[i].to]==-1&&a[i].w)
{
dep[a[i].to]=dep[q.front()]+1;
q.push(a[i].to);
if(a[i].to==e) return 1;
}
}
q.pop();
}
return 0;
}
int dfs(int u,int nfl)
{
if(u==e) return nfl;
int rest = nfl;
for(int i=h[u];i;i=a[i].ne)
{
if(a[i].w&&dep[a[i].to]==dep[u]+1&&rest)
{
int nwf=dfs(a[i].to,min(rest,a[i].w));
rest-=nwf;
a[a[i].op].w+=nwf;
a[i].w-=nwf;
}
}
return nfl-rest;
}*/
bool bfs(int u)
{
memset(dep,-1,sizeof(dep));
int tt=0,hh=0,q[MN];dep[s]=1;q[++tt]=s;
while(hh<tt)
{
int v=q[++hh];
for(int i=h[v];i;i=a[i].ne)
{
if(dep[a[i].to]==-1&& a[i].w)
{
dep[a[i].to]=dep[v]+1;
q[++tt]=a[i].to;
if(a[i].to==e) return 1;
}
}
}
return 0;
}
int dfs(int u,int nfl)
{
if(u==e) return nfl;
int rest = nfl;
for(int i=h[u];i;i=a[i].ne)
{
if(a[i].w&&dep[a[i].to]==dep[u]+1&&rest)
{
int nwf=dfs(a[i].to,min(rest,a[i].w));
a[i].w-=nwf;
a[a[i].op].w+=nwf;
rest-=nwf;
}
}
return nfl-rest;
}
LL can(LL mid)
{
so(i,1,tot,1) a[i].ne=a[i].to=a[i].w=a[i].op=0,h[i]=0;
tot=0,ans=0;
so(i,1,n,1) so(j,i+1,n,1)
{
if(dis[i][j]<=mid)
link(i,j+n,INF),link(j+n,i,0),a[tot-1].op=tot,
link(j,i+n,INF),link(i+n,j,0),a[tot-1].op=tot;
}
so(i,1,n,1)
link(s,i,aa[i]),link(i,s,0),a[tot-1].op=tot,
link(i+n,e,b[i]),link(e,i+n,0),a[tot-1].op=tot,
link(i,n+i,INF),link(n+i,i,0),a[tot-1].op=tot;
//for(int i=h[s];i;i=a[i].ne) printf("%d#%d ",a[i].to,a[i].w);
while(bfs(s))
ans+=dfs(s,INF);
return ans;
}
int main()
{
//memset(dis,,sizeof(dis));cout<<dis[0];
scanf("%d%d",&n,&m);so(i,1,n,1) so(j,1,n,1) dis[i][j]=1e19;
so(i,1,n,1) scanf("%d%d",&aa[i],&b[i]),sum+=aa[i];
so(i,1,m,1) scanf("%d%d%d",&x,&y,&z),dis[x][y]=dis[y][x]=min((LL)z,dis[y][x]);
so(i,1,n,1) dis[i][i]=0;
so(k,1,n,1) so(i,1,n,1) so(j,1,n,1) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
LL l=0,r=1e9*m;
//so(i,1,n,1) {so(j,1,n,1) printf("%d ",dis[i][j]);cout<<endl;}
while(l<=r)
{
LL mid=(l+r)>>1;
if(can(mid)>=sum) r=mid-1;
else l=mid+1;
}
if(r==1e9*m) printf("-1");
else printf("%lld",l);
return 0;
}
RT现在这个代码调不出来了,网络流感觉不好debug啊
回复
共 5 条回复,欢迎继续交流。
正在加载回复...