Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 82153 | Accepted: 31914 |
Description
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
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#define ll long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
const double e=exp(1.0);
const double pi=acos(-1);
ll mp[205][205],n,m;
ll EK()
{
ll max_flow=0,flow[205]={0};
flow[1]=inf;
while(1)
{
int dir[205]={0};
dir[1]=0;
queue<int>q;
q.push(1);
while(!q.empty())
{
int now=q.front();
q.pop();
if(now==m)break;
for(int i=1;i<=m;i++)
{
if(i!=1&&mp[now][i]>0&&!dir[i]){
dir[i]=now;
flow[i]=min(mp[now][i],flow[now]);
q.push(i);
}
}
}
if(dir[m]==0)break;
int now=m;
while(now!=1)
{
int next=dir[now];
mp[now][next]+=flow[m];
mp[next][now]-=flow[m];
now=next;
}
max_flow+=(ll)flow[m];
}
return max_flow;
}
int main()
{
int s,e,c;
while(scanf("%lld%lld",&n,&m)!=EOF)
{
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
mp[i][j]=0;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&s,&e,&c);
mp[s][e]+=(ll)c;
}
printf("%lld\n",Ek());
}
return 0;
}
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#define ll long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
const double e=exp(1.0);
const double pi=acos(-1);
struct Edge{
int to,cap,rev;//分别记录可以达到的点 最大流量 反向边(为后面的反向标记做准备)
};
int n,m;
bool use[205];
vector<Edge>v[205];
int dfs(int s,int e,int flow)
{
if(s==e)return flow;
use[s]=1;
for(int i=0;i<v[s].size();i++)
{
if(!use[v[s][i].to]&&v[s][i].cap>0)
{
Edge now=v[s][i];
int d=dfs(now.to,m,min(flow,now.cap));
if(d>0){
v[s][i].cap-=d;
v[now.to][now.rev].cap+=d;// 记录反向边 有点巧妙
return d;
}
}
}
return 0;
}
ll FF()
{
ll max_flow=0;
while(1)
{
memset(use,0,sizeof use);
int f=dfs(1,m,inf);
if(f==0)break;
max_flow+=(ll)f;
}
return max_flow;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int s,e,c;
for(int i=1;i<=m;i++)v[i].clear();
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&s,&e,&c);
v[s].push_back((Edge){e,c,v[e].size()});
v[e].push_back((Edge){s,0,v[s].size()-1});//最后那个为标记反向 相当于用vector元素个数来标记
}
printf("%lld\n",FF());
}
return 0;
}
#include<stdio.h> #include<string.h> #include<cmath> #include<stdlib.h> #include<time.h> #include<algorithm> #include<iostream> #include<vector> #include<queue> #define ll long long #define qq printf("QAQ\n"); using namespace std; const int maxn=1e5+5; const int inf=0x3f3f3f3f; const double e=exp(1.0); const double pi=acos(-1); int n,m,ans; struct Edge{ int to,cap,rev; }; vector<Edge>v[205]; int dep[205],iter[105]; bool bfs() { queue<int>q; q.push(1); memset(dep,0,sizeof dep); dep[1]=1;//起初没注意要归起点要归1 一直死循环 找了好久的bug while(!q.empty()) { int now=q.front(); q.pop(); for(int i=0;i<v[now].size();i++) { if(v[now][i].cap>0&&dep[v[now][i].to]==0) { dep[v[now][i].to]=dep[now]+1; q.push(v[now][i].to); } } } return dep[m]>0; } int dfs(int s,int e,int flow) { if(s==e)return flow; int ff=0; for(int i=0;i<v[s].size();i++) { if(v[s][i].cap>0&&dep[s]==dep[v[s][i].to]-1) { int f=dfs(v[s][i].to,e,min(flow-ff,v[s][i].cap)); if(f>0){ v[s][i].cap-=f; v[v[s][i].to][v[s][i].rev].cap+=f; ff+=f; //return f; } } } if(ff==0)dep[s]=0; return ff; } ll Dinic() { ll max_flow=0; int flow; while(bfs()) { //memset(iter,0,sizeof iter); while(flow=dfs(1,m,inf)) max_flow+=(ll)flow; } return max_flow; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int s,e,c; for(int i=1;i<=m;i++)v[i].clear(); for(int i=0;i<n;i++) { scanf("%d%d%d",&s,&e,&c); v[s].push_back((Edge){e,c,v[e].size()}); v[e].push_back((Edge){s,0,v[s].size()-1}); } //ans=0; printf("%lld\n",Dinic()); // printf("%d\n",ans); } return 0; }