URAL1277 Cops and Thieves(最小割)

Cops and Thieves

Description:

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 (  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:
 
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
题意:
给出一个无向图以及其起点和终点,每个点都有点权,问是否能用k个人去截断起点到终点的路径,注意不能将人员安排在起点和终点上面。
 
题解:
就是一个最小割的问题,建双向边以及拆点就好了。因为不能在起点和终点安排人员,所以起点和终点拆边的时候容量为无穷大就行了。
 
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 505,M = 1e5+5;
int n,m,s,F,tot,k;
int w[N],head[N],d[N];
struct Edge{
    int u,v,c,next;
}e[M];
void adde(int u,int v,int c){
    e[tot].v=v;e[tot].next=head[u];e[tot].c=c;head[u]=tot++;
    e[tot].v=u;e[tot].next=head[v];e[tot].c=0;head[v]=tot++;
}
int bfs(){
    memset(d,0,sizeof(d));d[F]=1;
    queue <int > q;q.push(F);
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(e[i].c>0 && !d[v]){
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return d[s+n]!=0;
}
int dfs(int S,int a){
    if(S==s+n || a==0) return a;
    int flow=0,f;
    for(int i=head[S];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(d[v]!=d[S]+1) continue ;
        f=dfs(v,min(a,e[i].c));
        if(f>0){
            e[i].c-=f;
            e[i^1].c+=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    if(!flow) d[S]=-1;
    return flow;
}
int Dinic(int S,int T){
    int flow = 0;
    while(bfs())
        flow+=dfs(S,INF);
    return flow ;
}
int main(){
    cin>>k>>n>>m>>s>>F;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++) scanf("%d",&w[i]);
    for(int i=1;i<=n;i++){
        if(i==s ||i==F) adde(i,i+n,INF);
        else adde(i,i+n,w[i]);
    }
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        adde(u+n,v,INF);adde(v+n,u,INF);
    }
    int flow = Dinic(F,s+n);
    if(flow>k) cout<<"NO";
    else cout<<"YES";
    return 0;
}

 

转载于:https://www.cnblogs.com/heyuhhh/p/10230189.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值