题意:
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
2 1 1 2 表示 共有2个节点,生产能量的点1个,消耗能量的点1个, 传递能量的通道2条;
(0,1)20 (1,0)10 代表(起点,终点)最大传递的能量
(0)15 (产生能量的点)产生的最大能量
(1)20 (消费能量的点)消费的最大能量
初学网络流,我想从基础练起;就先用EK算法写一遍
这道题看似很难,但其实只要加一个源点以及汇点,让所有的产生能量的点指向源点,让所有的消费能量的点指向汇点;
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 110
#define INF 0x3f3f3f3f
#define Min(a, b) a<b?a:b
int G[N][N], pre[N];
int EK(int s, int e);
bool BFS(int s, int e);
int main()
{
int point, source, dest, edge;
while(scanf("%d%d%d%d", &point, &source, &dest, &edge)!=EOF)
{
int a, b, flow, in, inflow, out, outflow;
char ch;
memset(G, 0, sizeof(G));
for(int i=0; i<edge; i++)
{
//scanf("(%d,%d)%d", &a, &b, &flow);
cin>>ch>>a>>ch>>b>>ch>>flow;
G[a+1][b+1]+=flow;
}
for(int i=0; i<source; i++)
{
//scanf("(%d)%d", &out, &outflow);
cin>>ch>>out>>ch>>outflow;
G[0][out+1]+=outflow;
}
for(int i=0; i<dest; i++)
{
//scanf("(%d)%d", &in, &inflow);
cin>>ch>>in>>ch>>inflow;
G[in+1][point+1]+=inflow;
}
int ans=EK(0, point+1);
printf("%d
", ans);
}
return 0;
}
int EK(int s, int e)
{
int maxflow=0;
while(BFS(s, e))
{
int minflow=INF;
for(int i=e; i!=s; i=pre[i])
minflow=Min(minflow, G[pre[i]][i]);
for(int j=e; j!=s; j=pre[j])
{
G[pre[j]][j]-=minflow;
G[j][pre[j]]+=minflow;
}
maxflow+=minflow;
}
return maxflow;
}