URAL 1277 - Cops and Thieves - [无向图点带权的最小点割]

题目链接:https://cn.vjudge.net/problem/URAL-1277

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 toN); 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.

Example

inputoutput
10
5 5 1 5
1 6 6 11 1
1 2
1 3
2 4
3 4
4 5
NO
10
5 5 1 5
1 4 4 11 1
1 2
1 3
2 4
3 4
4 5
YES

 

至于为什么要这样拆边,假设in(k)==k',out(k)==k'',即我们要addedge(out(i), in(j), INF)和addedge(out(j), in(i), INF),

为什么要这样addedge呢?因为我们的最大流算法默认都是有向图,addedge加入的边都是有向边,

这样加入边确保了该点k所有的入弧都是连接到in(k),出弧都是连接在out(k);

这使得原本所有流经该点的flow,现在都必须流经edge(in(k), out(k), w),确保了算法的正确性(不是很清楚的话,可以手动画图拆点拆边试试)。

(原本我想当然的addedge(out(i), in(j), INF)和addedge(in(j), in(i), INF),果断WA了……)

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<vector>
  4 #include<queue>
  5 #define MAX 2*100+5
  6 #define INF 0x3f3f3f3f
  7 #define in(x) x
  8 #define out(x) x+N
  9 using namespace std;
 10 int K,N,M,S,F;
 11 struct Edge{
 12     int u,v,c,f;
 13 };
 14 struct Dinic
 15 {
 16     int s,t;
 17     vector<Edge> E;
 18     vector<int> G[MAX];
 19     bool vis[MAX];
 20     int lev[MAX];
 21     int cur[MAX];
 22     void addedge(int from,int to,int cap)
 23     {
 24         E.push_back((Edge){from,to,cap,0});
 25         E.push_back((Edge){to,from,0,0});
 26         G[from].push_back(E.size()-2);
 27         G[to].push_back(E.size()-1);
 28     }
 29     bool bfs()
 30     {
 31         memset(vis,0,sizeof(vis));
 32         queue<int> q;
 33         q.push(s);
 34         lev[s]=0;
 35         vis[s]=1;
 36         while(!q.empty())
 37         {
 38             int now=q.front(); q.pop();
 39             for(int i=0,_size=G[now].size();i<_size;i++)
 40             {
 41                 Edge edge=E[G[now][i]];
 42                 int nex=edge.v;
 43                 if(!vis[nex] && edge.c>edge.f)
 44                 {
 45                     lev[nex]=lev[now]+1;
 46                     q.push(nex);
 47                     vis[nex]=1;
 48                 }
 49             }
 50         }
 51         return vis[t];
 52     }
 53     int dfs(int now,int aug)
 54     {
 55         if(now==t || aug==0) return aug;
 56         int flow=0,f;
 57         for(int& i=cur[now],_size=G[now].size();i<_size;i++)
 58         {
 59             Edge& edge=E[G[now][i]];
 60             int nex=edge.v;
 61             if(lev[now]+1 == lev[nex] && (f=dfs(nex,min(aug,edge.c-edge.f)))>0)
 62             {
 63                 edge.f+=f;
 64                 E[G[now][i]^1].f-=f;
 65                 flow+=f;
 66                 aug-=f;
 67                 if(!aug) break;
 68             }
 69         }
 70         return flow;
 71     }
 72     int maxflow()
 73     {
 74         int flow=0;
 75         while(bfs())
 76         {
 77             memset(cur,0,sizeof(cur));
 78             flow+=dfs(s,INF);
 79         }
 80         return flow;
 81     }
 82 }dinic;
 83 int main()
 84 {
 85     scanf("%d%d%d%d%d",&K,&N,&M,&S,&F);
 86     for(int i=1,x;i<=N;i++)
 87     {
 88         scanf("%d",&x);
 89         dinic.addedge(in(i),out(i),x);
 90         dinic.addedge(out(i),in(i),x);
 91     }
 92     for(int i=1,a,b;i<=M;i++)
 93     {
 94         scanf("%d%d",&a,&b);
 95         dinic.addedge(out(a),in(b),INF);
 96         dinic.addedge(out(b),in(a),INF);
 97     }
 98     if(S==F){
 99         printf("NO\n");
100         return 0;
101     }
102     dinic.s=out(S), dinic.t=in(F);
103     if(dinic.maxflow()<=K) printf("YES\n");
104     else printf("NO\n");
105     return 0;
106 }

 

posted @ 2017-08-30 15:17  Dilthey  阅读(181)  评论(0编辑  收藏  举报