首页 > 代码库 > HDU4289-Control(最小割定理)

HDU4289-Control(最小割定理)

Control

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


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
 
最大流最小割定理:源S到汇T的网络最大流等于S与T间最小边割集的容量和。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1000;
const int maxe = 20000*10;
const int inf = 1<<30;
int n,m,s,d;
int src,sink;
queue<int> que;
bool vis[maxn];
int dist[maxn];
struct edge{
    int v,f,nxt;
}e[maxe];
int head[maxn];
int nume;
void addedge(int u,int v,int w){
    e[++nume].nxt = head[u];
    e[nume].v = v;
    e[nume].f = w;
    head[u] = nume;
    e[++nume].nxt = head[v];
    e[nume].v = u;
    e[nume].f = 0;
    head[v] = nume;
}
void bfs(){
    memset(dist,0,sizeof dist);
    while(!que.empty()) que.pop();
    vis[src] = true;
    que.push(src);
    while(!que.empty()){
        int u = que.front();
        que.pop();
        for(int i = head[u]; i ; i = e[i].nxt){
            if(e[i].f && !vis[e[i].v]){
                que.push(e[i].v);
                vis[e[i].v] = 1;
                dist[e[i].v] = dist[u]+1;
            }
        }
    }
}

int dfs(int u,int delta){
    if(u== sink) return delta;
    else{
        int ret = 0;
        for(int i = head[u]; delta&&i; i = e[i].nxt){
            if(e[i].f && dist[e[i].v] == dist[u]+1){
                int dd = dfs(e[i].v,min(e[i].f,delta));
                e[i].f -= dd;
                e[i^1].f += dd;
                delta -= dd;
                ret += dd;
            }
        }
        return ret;
    }
}

int maxflow(){
    int ret = 0;
    while(true){
        memset(vis,0,sizeof vis);
        bfs();
        if(!vis[sink]) return ret;
        ret += dfs(src,inf);
    }

}
void init(){
    memset(head,0,sizeof head);
    nume = 1;
}
int main(){

    while(~scanf("%d%d",&n,&m)){
        init();
        scanf("%d%d",&s,&d);
        src = http://www.mamicode.com/2*s-1;>