这题就是基本的网络流,要看解释的话,直接看上一篇文章就可以了,写的比较详细的,不过我开始没有注意capacity的清零,没有1a....
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int INF=0x7fffffff;
const int maxn=205;
int capacity[maxn][maxn];
int pre[maxn];
int flow[maxn];
queue<int> que;
int Min(int a,int b)
{
return a<b?a:b;
}
int n,m;
int BFS(int src,int des)
{
while(!que.empty())
que.pop();
memset(pre,-1,sizeof(pre));
pre[src]=0;
que.push(src);
flow[src]=INF;
while(!que.empty())
{
int index=que.front();
que.pop();
if(index==des)break;
for(int i=1;i<=m;i++)
{
if(i!=src&&capacity[index][i]>0&&pre[i]==-1)
{
pre[i]=index;
flow[i]=Min(flow[index],capacity[index][i]);
que.push(i);
}
}
}
if(pre[des]==-1)
return -1;
else
return flow[des];
}
int maxFlow(int src,int des)
{
int increasement=0;
int sumFlow=0;
while((increasement=BFS(src,des))!=-1)
{
int k=des;
while(k!=src)
{
int last=pre[k];
capacity[last][k]-=increasement;
capacity[k][last]+=increasement;
k=last;
}
sumFlow+=increasement;
}
return sumFlow;
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
memset(capacity,0,sizeof(capacity));
memset(flow,0,sizeof(flow));
int start,end;
int ci;
for(int i=0;i<n;i++)
{
scanf("%d%d",&start,&end);
if(start==end)
continue;
capacity[start][end]+=ci;
}
printf("%d\n",maxFlow(1,m));
}
return 0;
}