社区讨论
88pts WA on #12 球跳悬2关
P1344[USACO4.4] 追查坏牛奶 Pollutant Control参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mhji32hl
- 此快照首次捕获于
- 2025/11/04 02:55 4 个月前
- 此快照最后确认于
- 2025/11/04 02:55 4 个月前
CPP
#include<bits/stdc++.h>
#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define int long long
using namespace std;
const int maxn=1e6+10,maxm=2e2+10,mod=998244353,inf=1e18;
int n,m,s,t,u,v,w,tot=1,cnt=1,ans,h[maxn],vis[maxn],dis[maxn],pre[maxn],cur[maxn],head[maxn];
struct node
{
int to,nxt,val;
}e[maxn];
struct edge
{
int to,nxt,val;
}g[maxn];
void add(int u,int v,int w)
{
e[++tot]=(node){v,head[u],w};
head[u]=tot;
e[++tot]=(node){u,head[v],0};
head[v]=tot;
}
void addedge(int u,int v,int w)
{
g[++cnt]=(edge){v,h[u],w};
h[u]=cnt;
g[++cnt]=(edge){u,h[v],0};
h[v]=cnt;
}
bool bfs()
{
queue<int>q;
memset(pre,0,sizeof pre);
q.push(s);
pre[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].to;
if(!pre[v] and e[i].val)
{
pre[v]=pre[u]+1;
q.push(v);
}
}
}
if(pre[t])
{
return 1;
}
return 0;
}
bool bfsedge()
{
queue<int>q;
memset(pre,0,sizeof pre);
q.push(s);
pre[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=h[u];i;i=g[i].nxt)
{
int v=g[i].to;
if(!pre[v] and g[i].val)
{
pre[v]=pre[u]+1;
q.push(v);
}
}
}
if(pre[t])
{
return 1;
}
return 0;
}
int dfs(int x,int flow)
{
if(x==t)
{
return flow;
}
int res=0;
for(int &i=cur[x];i;i=e[i].nxt)
{
int v=e[i].to;
if(pre[v]==pre[x]+1 and e[i].val)
{
int w=dfs(v,min(e[i].val,flow-res));
e[i].val-=w;
e[i^1].val+=w;
res+=w;
if(res==flow)
{
return res;
}
}
}
if(!res)
{
pre[x]=0;
}
return res;
}
int dfsedge(int x,int flow)
{
if(x==t)
{
return flow;
}
int res=0;
for(int &i=cur[x];i;i=g[i].nxt)
{
int v=g[i].to;
if(pre[v]==pre[x]+1 and g[i].val)
{
int w=dfsedge(v,min(g[i].val,flow-res));
g[i].val-=w;
g[i^1].val+=w;
res+=w;
if(res==flow)
{
return res;
}
}
}
if(!res)
{
pre[x]=0;
}
return res;
}
int dinic()
{
int res=0;
while(bfs())
{
memcpy(cur,head,sizeof(cur));
res+=dfs(s,1e18);
}
return res;
}
int dinicedge()
{
int res=0;
while(bfsedge())
{
memcpy(cur,h,sizeof(cur));
res+=dfsedge(s,1e18);
}
return res;
}
signed main()
{
cin>>n>>m;s=1,t=n;
for(int i=1;i<=m;i++)
{
cin>>u>>v>>w;
add(u,v,w);
add(v,u,0);
addedge(u,v,1);
addedge(v,u,0);
}
cout<<dinic()<<" "<<dinicedge();
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...