看我异次元传送门
border="0" width="330" height="86" src="http://music.163.com/outchain/player?type=2&id=28377210&auto=1&height=66">**
Drainage Ditches
**
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 64219 Accepted: 24777
**
Description
**
Every 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.
**
Input
**
The 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.
**
Output
**
For 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
**
Source
**
USACO 93
简单题意
单向边的最大流 1 是起点 m是终点
**
Solution
**
起初Wa==》发现多组数据…
然后TLE 发现maxn定义等于200…
估计是被NOIP自己的成绩吓哭了…
裸题不多说…代码觉得还是不是很长的..QAQ
用的邻接表储存的 如果希望看其他版本的代码 点这里这里写链接内容
**
Code
**
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=205;
struct data{int to,next,w;}e[maxn*maxn];
int head[maxn],cnt=1,h[maxn],n,m,ans;
void ins(int u,int v,int w){cnt++;e[cnt].to=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt;}
bool bfs(){
memset(h,-1,sizeof(h));
h[1]=0;
queue<int> q;
q.push(1);
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].next)
if(e[i].w&&h[e[i].to]<0)
{
q.push(e[i].to);
h[e[i].to]=h[x]+1;
}
}
if(h[n]==-1)return 0;
return 1;
}
int dfs(int x,int f){
if(x==n)return f;
int w,used=0;
for(int i=head[x];i;i=e[i].next)
if(e[i].w&&h[e[i].to]==h[x]+1)
{
w=f-used;
w=dfs(e[i].to,min(w,e[i].w));
e[i].w-=w;
e[i^1].w+=w;
used+=w;
if(used==f)return f;
}
if(!used)h[x]=-1;
return used;
}
void Dinic(){
while(bfs()){
ans+=dfs(1,2000000000);
}
}
int main()
{
while(scanf("%d%d",&m,&n)==2)
{
ans=0;cnt=1;
memset(head,0,sizeof(head));
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
ins(u,v,w);
ins(v,u,0);
}
Dinic();
printf("%d\n",ans);
}
return 0;
}
**
HDU 3549
**
这是后来加的…… 一样……就改了改细节……
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=205;
struct data{int to,next,w;}e[maxn*maxn];
int head[maxn],cnt=1,h[maxn],n,m,ans,T;
void ins(int u,int v,int w){cnt++;e[cnt].to=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt;}
bool bfs(){
memset(h,-1,sizeof(h));
h[1]=0;
queue<int> q;
q.push(1);
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].next)
if(e[i].w&&h[e[i].to]<0)
{
q.push(e[i].to);
h[e[i].to]=h[x]+1;
}
}
if(h[n]==-1)return 0;
return 1;
}
int dfs(int x,int f){
if(x==n)return f;
int w,used=0;
for(int i=head[x];i;i=e[i].next)
if(e[i].w&&h[e[i].to]==h[x]+1)
{
w=f-used;
w=dfs(e[i].to,min(w,e[i].w));
e[i].w-=w;
e[i^1].w+=w;
used+=w;
if(used==f)return f;
}
if(!used)h[x]=-1;
return used;
}
void Dinic(){
while(bfs()){
ans+=dfs(1,2000000000);
}
}
int main()
{
int T,Cnt=0;
scanf("%d",&T);
while(T--&&scanf("%d%d",&n,&m)==2)
{
Cnt++;
ans=0;cnt=1;
memset(head,0,sizeof(head));
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
ins(u,v,w);
ins(v,u,0);
}
Dinic();
printf("Case %d: %d\n",Cnt,ans);
}
return 0;
}
——既然选择了远方,便只顾风雨兼程