HDU 4289 Control

Control

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 4289
64-bit integer IO format: %I64d      Java class name: Main
 
 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

 
解题:最大流,将城市拆点,边为此城市的权
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int maxn = 1010;
 5 struct arc{
 6     int to,flow,next;
 7     arc(int x = 0,int y = 0,int z = -1){
 8         to = x;
 9         flow = y;
10         next = z;
11     }
12 }e[maxn*maxn];
13 int head[maxn],d[maxn],cur[maxn],tot,S,T;
14 void add(int u,int v,int flow){
15     e[tot] = arc(v,flow,head[u]);
16     head[u] = tot++;
17     e[tot] = arc(u,0,head[v]);
18     head[v] = tot++;
19 }
20 queue<int>q;
21 bool bfs(){
22     while(!q.empty()) q.pop();
23     q.push(S);
24     memset(d,-1,sizeof d);
25     d[S] = 1;
26     while(!q.empty()){
27         int u = q.front();
28         q.pop();
29         //cout<<"u:"<<u<<endl;
30         for(int i = head[u]; ~i; i = e[i].next){
31             if(e[i].flow && d[e[i].to] == -1){
32                 d[e[i].to] = d[u] + 1;
33                 q.push(e[i].to);
34             }
35         }
36     }
37     //cout<<"ok"<<endl;
38     return d[T] > -1;
39 }
40 int dfs(int u,int low){
41     if(u == T) return low;
42     int tmp = 0,a;
43     for(int &i = cur[u]; ~i; i = e[i].next){
44         if(e[i].flow && d[u]+1==d[e[i].to]&&(a=dfs(e[i].to,min(low,e[i].flow)))){
45             e[i].flow -= a;
46             e[i^1].flow += a;
47             tmp += a;
48             low -= a;
49             if(!low) break;
50         }
51     }
52     if(!tmp) d[u] = -1;
53     return tmp;
54 }
55 int dinic(){
56     int ret = 0;
57     while(bfs()){
58         memcpy(cur,head,sizeof head);
59         ret += dfs(S,INF);
60     }
61     return ret;
62 }
63 int main(){
64     int n,m,u,v;
65     while(~scanf("%d%d",&n,&m)){
66         scanf("%d%d",&S,&T);
67         memset(head,-1,sizeof head);
68         tot = 0;
69         for(int i = 1,tmp; i <= n; ++i){
70             scanf("%d",&tmp);
71             add(i,i + n,tmp);
72         }
73         for(int i = 0; i < m; ++i){
74             scanf("%d%d",&u,&v);
75             add(u + n,v,INF);
76             add(v + n,u,INF);
77         }
78         T = T + n;
79         printf("%d
",dinic());
80     }
81     return 0;
82 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4816105.html