The world is really big.
Network flow is usually directed graph, but undirected graph still exists.
In fact, the drawing method is similar.
Let's look at the following questions first:
description
A criminal gang intends to steal an art gallery. The police decided to send K individuals to block all the roads leading from the bandits'den to the gallery, but they could only stay at the top of the road, not in the bandits' den or the gallery, and there was a minimum number of Ri who needed police to be stationed at each point. Ask the police if they can accomplish the task. N<=100 M<=10000
input
The first line of the input contains a single integer 0 < K ≤ 10000 — the number of policemen engaged to control the stations.
The second line has four integers: N, M, S and F delimited with white-space character.
N is the number of stations in the galaxy (the stations are numbered from 1 to N); 2 < N ≤ 100.
M is the number of teleportation channels; 1 < M ≤ 10000.
S is the number of the planet (and the station) where the museum is; 1 ≤ S ≤ N.
F is the number of the planet (and the station) where the thieves' refuge is; 1 ≤ F ≤ N.
The next line contains N integers ( x 1, ..., xN) separated with white-space character — the number of policemen required to control each of the stations (∑ i=1 N xi ≤ 10000).
Then M lines follow that describe the teleportation channels. Each of these lines contains a pair of space-delimited integers — the numbers of stations being connected by a channel. The channel system is designed so that it is possible to reach any station from any other one (probably it would require several channel transitions).
output
YES or NO Representation Is Feasible
For the restriction condition of a point, we usually transform it into the restriction condition of the opposite side.
Consider splitting a point I into two points I and i+1, connecting an edge of the number of policemen needed for point i, the edge of entering I to point i, and the edge of going out from point I to point i+n
Then run while the minimum cut, that is, with the least police will be divided into art galleries and thieves nest two parts, which is also the classic minimum cut, the model.
Compare the minimum cut and the number of police, output YES or NO
It is worth noting that there is no police in the art gallery and the burglar's den, so when breaking down these two points, we should pay attention to setting the border right as INF.
Nothing else to pay attention to
Just build two-way sides on each side.
Complete code:
#include<stdio.h>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
struct edge
{
int v,w,last;
}ed[100010];
queue <int> state;
int head[100010],dis[100010];
int n,m,S=0,T,K,ans=0,num=1;
void add(int u,int v,int w)
{
num++;
ed[num].v=v;
ed[num].w=w;
ed[num].last=head[u];
head[u]=num;
}
void init()
{
num=1,ans=0;
memset(head,0,sizeof(head));
}
bool bfs()
{
memset(dis,-1,sizeof(dis));
while(!state.empty()) state.pop();
state.push(S);
dis[S]=0;
while(!state.empty())
{
int u=state.front();
state.pop();
for(int i=head[u];i;i=ed[i].last)
{
int v=ed[i].v;
if(dis[v]==-1&&ed[i].w>0)
{
dis[v]=dis[u]+1;
state.push(v);
}
}
}
if(dis[T]==-1) return false;
return true;
}
int dfs(int u,int low)
{
int a=0;
if(u==T || low==0) return low;
for(int i=head[u];i;i=ed[i].last)
{
int v=ed[i].v;
if(ed[i].w>0&&dis[v]==dis[u]+1)
{
int tmp=dfs(v,min(low,ed[i].w));
ed[i].w-=tmp,ed[i^1].w+=tmp;
a+=tmp;
low-=tmp;
if(low==0) return a;
}
}
if(low) dis[u]=-1;
return a;
}
int main()
{
while(scanf("%d",&K)!=EOF)
{
init();
scanf("%d%d%d%d",&n,&m,&T,&S);
for(int i=1;i<=n;i++)
{
int w;
scanf("%d",&w);
if(i==S || i==T) w=INF;
add(i,i+n,w);
add(i+n,i,0);
add(i+n,i,w);
add(i,i+n,0);
}
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u+n,v,INF);
add(v,u+n,0);
add(v+n,u,INF);
add(u,v+n,0);
}
T+=n;
while(bfs())
ans+=dfs(S,INF);
if(ans>K) printf("NO\n");
else printf("YES\n");
}
return 0;
}
/*
Whoso pulleth out this sword out of this stone and anvil is duly King of all England
*/
Well, that's it.