URAL 1277 网络流最小割 解题报告

来源:互联网 发布:c语言定义二维数组 编辑:程序博客网 时间:2024/05/17 04:27

1277. Cops and Thieves

Problem 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: 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 (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.

Samples

input

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

output

NO

input

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

output

YES

[解题报告]
题目大意
一个犯罪团伙打算去偷一家美术馆。警察决定派K 个人堵住所有从匪窝通向美术 馆的道路,不过他们只能驻守在沿途顶点处而不能在匪窝或美术馆,且每个点都 有一个需要警察驻守的最低人数Ri。问警察能否完成任务。(2 < N <= 100, 1 < M <= 10000)
建模方法
此题是无向图点带权的点连通度问题。将每个点i 拆成两个点i’, i“ ,除匪窝s 和 美术馆t 外加边(i’, i“, Ri) ,将每条无向边(i, j)分解为(i“, j’), (j“, i’) 。令s“为源,t’为 汇,求一次最小割即为结果。(注意此题还需要特判匪窝和美术馆在同一点的情况)
(SB题调了半天)

代码如下:

#include<cstdio>#include<cstring> #include<string>#include<algorithm>#include<map>#include<queue>  #include<vector>using namespace std;  #define inf 0x3f3f3f3f  #define maxv 200005#define maxe 500005 int nume=0,head[maxv],e[maxe][3];void inline adde(int i,int j,int c)  {      e[nume][0]=j;e[nume][1]=head[i];head[i]=nume;      e[nume++][2]=c;     e[nume][0]=i;e[nume][1]=head[j];head[j]=nume;      e[nume++][2]=0;  }  int ss,tt,n,m,k,s,f;  int vis[maxv],lev[maxv]; bool bfs()  {      for(int i=0;i<maxv;i++)      vis[i]=lev[i]=0;      queue<int>q;      q.push(ss);      vis[ss]=1;      while(!q.empty())      {          int cur=q.front();          q.pop();          for(int i=head[cur];i!=-1;i=e[i][1])          {              int v=e[i][0];              if(!vis[v]&&e[i][2]>0)              {                  lev[v]=lev[cur]+1;                  vis[v]=1;                  q.push(v);              }          }      }      return vis[tt];  }  int dfs(int u,int minf)  {      if(u==tt||minf==0) return minf;      int sumf=0,f;      for(int i=head[u];i!=-1&&minf;i=e[i][1])      {          int v=e[i][0];          if(lev[v]==lev[u]+1&&e[i][2]>0)          {              f=dfs(v,minf<e[i][2]?minf:e[i][2]);              e[i][2]-=f;e[i^1][2]+=f;              sumf+=f;minf-=f;          }      }      if(!sumf) lev[u]=-1;      return sumf;  }  int Dinic()  {      int sum=0;      while(bfs()) sum+=dfs(ss,inf);      return sum;  } int main(){    while(scanf("%d",&k)!=EOF)    {        nume=0;        memset(head,-1,sizeof(head));        scanf("%d%d%d%d",&n,&m,&s,&f);        ss=s,tt=f+n;        for(int i=1;i<=n;++i)        {            int x;scanf("%d",&x);            adde(i+n,i,x);        }        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);        }        if(s==f)         {            printf("NO\n");continue;        }        if(Dinic()<=k)  printf("YES\n");        else printf("NO\n");    }    return 0;}   

让我看到你们的双手