码迷,mamicode.com
首页 > 其他好文 > 详细

HDU4289(KB11-I 最小割)

时间:2017-08-24 16:10:18      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:oca   port   var   center   const   esc   uri   using   panel   

Control

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3577    Accepted Submission(s): 1503


Problem Description

  You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
  The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.
  You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.
  It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:
  * all traffic of the terrorists must pass at least one city of the set.
  * sum of cost of controlling all cities in the set is minimal.
  You may assume that it is always possible to get from source of the terrorists to their destination.
------------------------------------------------------------
1 Weapon of Mass Destruction
 

 

Input

  There are several test cases.
  The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.
  The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.
  The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 107.
  The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.
  Please process until EOF (End Of File).
 

 

Output

  For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
  See samples for detailed information.
 

 

Sample Input

5 6 5 3 5 2 3 4 12 1 5 5 4 2 3 2 4 4 3 2 1
 

 

Sample Output

3
 

 

Source

 

顶点上有容量限制则进行拆点。把u点拆成u和u+n,若<u,v>有边,则u+n和v加边,v+n和u加边。

  1 //2017-08-24
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <queue>
  7 #pragma comment(linker, "/STACK:1024000000,1024000000") 
  8 
  9 using namespace std;
 10 
 11 const int N = 1010;
 12 const int M = 210010;
 13 const int INF = 0x3f3f3f3f;
 14 int head[N], tot;
 15 struct Edge{
 16     int next, to, w;
 17 }edge[M];
 18 
 19 void add_edge(int u, int v, int w){
 20     edge[tot].w = w;
 21     edge[tot].to = v;
 22     edge[tot].next = head[u];
 23     head[u] = tot++;
 24 }
 25 
 26 struct Dinic{
 27     int level[N], S, T;
 28     void init(int _S, int _T){
 29         S = _S;
 30         T = _T;
 31         tot = 0;
 32         memset(head, -1, sizeof(head));
 33     }
 34     bool bfs(){
 35         queue<int> que;
 36         memset(level, -1, sizeof(level));
 37         level[S] = 0;
 38         que.push(S);
 39         while(!que.empty()){
 40             int u = que.front();
 41             que.pop();
 42             for(int i = head[u]; i != -1; i = edge[i].next){
 43                 int v = edge[i].to;
 44                 int w = edge[i].w;
 45                 if(level[v] == -1 && w > 0){
 46                     level[v] = level[u]+1;
 47                     que.push(v);
 48                 }
 49             }
 50         }
 51         return level[T] != -1;
 52     }
 53     int dfs(int u, int flow){
 54         if(u == T)return flow;
 55         int ans = 0, fw;
 56         for(int i = head[u]; i != -1; i = edge[i].next){
 57             int v = edge[i].to, w = edge[i].w;
 58             if(!w || level[v] != level[u]+1)
 59                   continue;
 60             fw = dfs(v, min(flow-ans, w));
 61             ans += fw;
 62             edge[i].w -= fw;
 63             edge[i^1].w += fw;
 64             if(ans == flow)return ans;
 65         }
 66         if(ans == 0)level[u] = -1;
 67         return ans;
 68     }
 69     int maxflow(){
 70         int flow = 0, f;
 71         while(bfs())
 72               while((f = dfs(S, INF)) > 0)
 73                 flow += f;
 74         return flow;    
 75     }
 76 }dinic;
 77 
 78 char str[N];
 79 
 80 int main()
 81 {
 82     std::ios::sync_with_stdio(false);    
 83     //freopen("inputI.txt", "r", stdin);
 84     int n, m;
 85     while(cin>>n>>m){
 86         int s, t;
 87         cin>>s>>t;
 88         dinic.init(s, t+n);
 89         int u, v, w;
 90         for(int i = 1; i <= n; i++){
 91             cin>>w;
 92             add_edge(i, i+n, w);
 93             add_edge(i+n, i, 0);
 94         }
 95         while(m--){
 96             cin>>u>>v;
 97             add_edge(u+n, v, INF);
 98             add_edge(v, u+n, 0);
 99             add_edge(v+n, u, INF);
100             add_edge(u, v+n, 0);
101         }
102         printf("%d\n", dinic.maxflow());
103     }    
104     return 0;
105 }

 

HDU4289(KB11-I 最小割)

标签:oca   port   var   center   const   esc   uri   using   panel   

原文地址:http://www.cnblogs.com/Penn000/p/7423487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!