两个题目都差不多,都是求最小割的最少割边数量,HDU 3987分了边的方向,但是也一样
对于每一个边,将它的容量变为w=w*(maxm+1)+1;因为最小割的容量最后都是一样的,+1是为了算出边数,如果不加一,最后就是跑出一个最小割,因为最小割是相同的,所以不+1的话本质上是没有区别的,但是+1以后,边数少的会变成最小割,最后跑出来的答案对maxm+1取模以后就是最小割的边数了
HDU 3987
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=2e5+7;
const ll maxm=1e5+7;
const ll inf=0x3f3f3f3f3f3f3f3f;
int source,sink;
struct Node
{
int to;
int next;
ll capa;
}edge[maxn<<2];
int cnt;
int dep[maxn];
int head[maxn];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
return;
}
void add(int u,int v,ll capa)
{
edge[cnt].to=v;
edge[cnt].capa=capa;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].to=u;
edge[cnt].capa=0;
edge[cnt].next=head[v];
head[v]=cnt++;
return;
}
bool bfs()
{
queue<int> que;
memset(dep,-1,sizeof(dep));
dep[source]=0;
que.push(source);
while(!que.empty())
{
int node=que.front();
que.pop();
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].capa&&dep[v]==-1)
{
dep[v]=dep[node]+1;
if(v==sink) return true;
que.push(v);
}
}
}
return false;
}
ll dfs(int node,ll minn)
{
if(node==sink||minn==0)
{
return minn;
}
ll r=0;
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].capa>0&&dep[v]==dep[node]+1)
{
ll tmp=dfs(v,min(edge[i].capa,minn));
if(tmp>0)
{
edge[i].capa-=tmp;
edge[i^1].capa+=tmp;
r+=tmp;
minn-=tmp;
if(!minn) break;
}
}
}
if(!r) dep[node]=-1;
return r;
}
ll dinic()
{
ll ans=0;
while(bfs())
{
ans+=dfs(source,inf);
}
return ans;
}
int main()
{
int test;
scanf("%d",&test);
for(int cas=1;cas<=test;cas++)
{
init();
int n,m;
scanf("%d%d",&n,&m);
source=0;
sink=n-1;
for(int i=0;i<m;i++)
{
int u,v,d;
ll c;
scanf("%d%d%lld%d",&u,&v,&c,&d);
add(u,v,c*(maxm+1)+1);
if(d==1) add(v,u,c*(maxm+1)+1);
}
ll ans=dinic();
printf("Case %d: %lld\n",cas,ans%(maxm+1));
}
return 0;
}
HDU 6214
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=2e5+7;
const ll maxm=1e5+7;
const ll inf=0x3f3f3f3f3f3f3f3f;
int source,sink;
struct Node
{
int to;
int next;
ll capa;
}edge[maxn<<2];
int cnt;
int dep[maxn];
int head[maxn];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
return;
}
void add(int u,int v,ll capa)
{
edge[cnt].to=v;
edge[cnt].capa=capa;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].to=u;
edge[cnt].capa=0;
edge[cnt].next=head[v];
head[v]=cnt++;
return;
}
bool bfs()
{
queue<int> que;
memset(dep,-1,sizeof(dep));
dep[source]=0;
que.push(source);
while(!que.empty())
{
int node=que.front();
que.pop();
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].capa&&dep[v]==-1)
{
dep[v]=dep[node]+1;
if(v==sink) return true;
que.push(v);
}
}
}
return false;
}
ll dfs(int node,ll minn)
{
if(node==sink||minn==0)
{
return minn;
}
ll r=0;
for(int i=head[node];~i;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].capa>0&&dep[v]==dep[node]+1)
{
ll tmp=dfs(v,min(edge[i].capa,minn));
if(tmp>0)
{
edge[i].capa-=tmp;
edge[i^1].capa+=tmp;
r+=tmp;
minn-=tmp;
if(!minn) break;
}
}
}
if(!r) dep[node]=-1;
return r;
}
ll dinic()
{
ll ans=0;
while(bfs())
{
ans+=dfs(source,inf);
}
return ans;
}
int main()
{
int test;
scanf("%d",&test);
for(int cas=1;cas<=test;cas++)
{
init();
int n,m;
scanf("%d%d",&n,&m);
scanf("%d%d",&source,&sink);
for(int i=0;i<m;i++)
{
int u,v;
ll c;
scanf("%d%d%lld",&u,&v,&c);
add(u,v,c*(maxm+1)+1);
}
ll ans=dinic();
printf("%lld\n",ans%(maxm+1));
}
return 0;
}