URAL 1277 Cops and Thieves

时间:2022-05-31 02:32:55

Cops and Thieves

Time Limit: 1000ms
Memory Limit: 16384KB

This problem will be judged on Ural. Original ID: 1277
64-bit integer IO format: %lld      Java class name: (Any)

The Galaxy Police (Galaxpol) found out that a notorious gang of thieves has plans for stealing an extremely valuable exhibit from the Earth Planetary Museum — an ancient microprocessor. The police chiefs decided to intercept the criminals on the way from their refuge to the museum. A problem arose while planning the police operation: would it be possible for the Galaxpol staff to control all the possible routes of the criminals?
The galaxy transport system is designed as follows. Each planet has a transport station that is connected to some of the other stations via two-way teleportation channels. Transport stations vary in their sizes, so different numbers of policemen may be required to take control over different stations. In order not to upset the operation, it was decided to leave the planets that are next to the museum or the refuge without any police control.
Help the Galaxpol to place their staff at the stations in order to block all possible routes of the thieves.
 

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: NMS 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 (x1, …, xN) separated with white-space character — the number of policemen required to control each of the stations (∑i=1Nxi ≤ 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

Write “YES” if it is possible to block all the possible routes within given limitations, and “NO” otherwise.
 

Sample Input

10
5 5 1 5
1 6 6 11 1
1 2
1 3
2 4
3 4
4 5

Sample Output

NO

Source

 
解题:很典型的最小割模型
 #include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>;
const int maxn = ;
struct arc {
int to,flow,next;
arc(int x = ,int y = ,int z = -) {
to = x;
flow = y;
next = z;
}
} e[maxn*maxn];
int head[maxn],gap[maxn],d[maxn],tot,S,T,n,m;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
void bfs() {
queue<int>q;
q.push(T);
memset(d,-,sizeof d);
memset(gap,,sizeof gap);
d[T] = ;
while(!q.empty()) {
int u = q.front();
q.pop();
++gap[d[u]];
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i^].flow && d[e[i].to] == -) {
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
}
int sap(int u,int low) {
if(u == T) return low;
int tmp = ,minh = *n - ;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow) {
if(d[u] == d[e[i].to] + ) {
int a = sap(e[i].to,min(e[i].flow,low));
e[i].flow -= a;
e[i^].flow += a;
tmp += a;
low -= a;
if(!low) break;
if(d[S] >= *n) return tmp;
}
if(e[i].flow) minh = min(minh,d[e[i].to]);
}
}
if(!tmp){
if(--gap[d[u]] == ) d[S] = *n;
++gap[d[u] = minh + ];
}
return tmp;
}
int maxflow(int ret = ){
bfs();
while(d[S] < *n) ret += sap(S,INF);
return ret;
}
int main() {
int police,u,v;
while(~scanf("%d",&police)){
scanf("%d%d%d%d",&n,&m,&S,&T);
S = n + S;
memset(head,-,sizeof head);
tot = ;
for(int i = ,tmp; i <= n; ++i){
scanf("%d",&tmp);
add(i,n + i,tmp);
}
for(int i = ; i < m; ++i){
scanf("%d%d",&u,&v);
add(u + n,v,INF);
add(v + n,u,INF);
}
puts(((S - n == T)||(maxflow() > police))?"NO":"YES");
}
return ;
}