首页 技术 正文
技术 2022年11月14日
0 收藏 549 点赞 3,773 浏览 4306 个字

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8599    Accepted Submission(s): 4005

Problem DescriptionEvery time it rains on Farmer John’s fields, a pond forms over Bessie’s favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie’s
clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 

Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 

Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 
 InputThe input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection
1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to
Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
 OutputFor each case, output a single integer, the maximum rate at which water may emptied from the pond. 
 Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

 Sample Output

50

 SourceUSACO 93 解题报告EK模板,不多说。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 99999999
#define N 220
#define M 220
using namespace std;
int n,m,edge[N][N],flow,a[N],p[N];
queue<int>Q;
void ek()
{
while(1)
{
while(!Q.empty())
Q.pop();
memset(a,0,sizeof(a));
memset(p,0,sizeof(p));
Q.push(1);
a[1]=inf;
p[1]=1;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int v=1;v<=n;v++)
{
if(!a[v]&&edge[u][v]>0)
{
a[v]=min(a[u],edge[u][v]);
p[v]=u;
Q.push(v);
}
}
if(a[n])break;
}
if(!a[n])break;
for(int u=n;u!=1;u=p[u])
{
edge[p[u]][u]-=a[n];
edge[u][p[u]]+=a[n];
}
flow+=a[n];
}
}
int main()
{
int i,j,u,v,w;
while(~scanf("%d%d",&m,&n))
{
flow=0;
memset(edge,0,sizeof(edge));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[u][v]+=w;
}
ek();
printf("%d\n",flow);
}
}

又学了Dinic算法。

。。

(基于邻接矩阵的)。前向星不知道怎么处理反向弧。。。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 99999999
#define N 200
#define M 200
using namespace std;
int edge[N][N],flow,l[N],n,m;
int bfs()
{
memset(l,-1,sizeof(l));
queue<int>Q;
l[1]=0;
Q.push(1);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int v=1;v<=n;v++)
{
if(edge[u][v]&&l[v]==-1)
{
l[v]=l[u]+1;
Q.push(v);
}
}
}
if(l[n]>0)
return 1;
else return 0;
}
int dfs(int x,int f)
{
if(x==n)return f;
int a;
for(int i=1;i<=n;i++)
{
if(edge[x][i]&&(l[i]==l[x]+1)&&(a=dfs(i,min(f,edge[x][i]))))
{
edge[x][i]-=a;
edge[i][x]+=a;
return a;
}
}
return 0;
}
int main()
{
int i,j,u,v,w;
while(~scanf("%d%d",&m,&n))
{
memset(edge,0,sizeof(edge));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[u][v]+=w;
}
int a=0;
flow=0;
while(bfs())
while(a=dfs(1,inf))
flow+=a;
printf("%d\n",flow);
}
return 0;
}

最终觉醒了,之前看了一串非递归的前向星dinic,,。老认为不正确。,,换了一种思路,写成递归调用dfs和邻接表一样,对于处理反向弧加流的话在建边的时候处理,建边一次建两条,正向和反向,这样就知道。反向弧在哪里了,,。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define N 300
#define M 30000
#define inf 99999999
using namespace std;
struct node
{
int u,v,w,r,next;
}edge[M];int n,m,head[N],l[N],cnt;
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
edge[cnt].r=cnt+1;
head[u]=cnt++;
edge[cnt].u=v;
edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].next=head[v];
edge[cnt].r=cnt-1;
head[v]=cnt++;
}
int bfs()
{
queue<int >Q;
while(!Q.empty())
Q.pop();
Q.push(1);
memset(l,-1,sizeof(l));
l[1]=0;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
if(edge[i].w&&l[edge[i].v]==-1)
{
l[edge[i].v]=l[u]+1;
Q.push(edge[i].v);
}
}
}
if(l[n]>0)return 1;
else return 0;
}
int dfs(int x,int f)
{
int i,a;
if(x==n)return f;
for(i=head[x];i!=-1;i=edge[i].next)
{
if(edge[i].w&&l[edge[i].v]==l[x]+1&&(a=dfs(edge[i].v,min(f,edge[i].w))))
{
edge[i].w-=a;
edge[edge[i].r].w+=a;
return a;
}
}
return 0;
}
int main()
{
int i,j,u,v,w;
while(cin>>m>>n)
{
memset(head,-1,sizeof(head));
memset(edge,0,sizeof(edge));
cnt=0;
for(i=0;i<m;i++)
{
cin>>u>>v>>w;
add(u,v,w);
}
int ans=0,a;
while(bfs())
while(a=dfs(1,inf))
ans+=a;
cout<<ans<<endl;
}
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,112
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,585
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922