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

hdoj 4289 Control 【拆点 求最小割】

时间:2015-08-28 23:25:36      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:



Control

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


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
 



杭电什么鬼?提交代码只交上去四分之一,两次CE。我也是醉了。。。


题意:有N个点和M条无向边,已给出去掉每个点的花费。再给你两个点S、D,问你阻断S->D的最小花费。


思路:拆点成边,然后就是裸最小割。


建图:设置超级源点source,超级汇点sink

1,把每个点i拆成左点i ->右点i+N的一条边,边权为去掉i点的费用;

2,source向S左点建边,边权为无穷大,表示不能去掉该边;

3,D右点向sink建边,边权为无穷大;

4,对于无向边<u, v>,建双边<u右,v左>和<v右,u左>,边权为无穷大,表示不能去掉该边。

最后source到sink跑一次最大流即求出最小割。



AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 500
#define MAXM 100000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int dist[MAXN], cur[MAXN];
bool vis[MAXN];
int N, M;
int source, sink;
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
void getMap()
{
    int a, b;
    source = 0, sink = 2*N + 1;
    scanf("%d%d", &a, &b);
    addEdge(source, a, INF);//超级源点连起点
    addEdge(b+N, sink, INF);//终点连超级汇点
    for(int i = 1; i <= N; i++)
    {
        scanf("%d", &a);
        addEdge(i, i+N, a);//拆点
    }
    while(M--)
    {
        scanf("%d%d", &a, &b);
        addEdge(a+N, b, INF);//无穷大流量 因为不能去掉这条边
        addEdge(b+N, a, INF);
    }
}
bool BFS(int s, int t)
{
    queue<int> Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    dist[s] = 0;
    vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap > E.flow)
            {
                dist[E.to] = dist[u] + 1;
                if(E.to == t) return true;
                vis[E.to] = true;
                Q.push(E.to);
            }
        }
    }
    return false;
}
int DFS(int x, int a, int t)
{
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap - E.flow), t)) > 0)
        {
            edge[i].flow += f; 
            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Maxflow(int s, int t)
{
    int flow = 0;
    while(BFS(s, t))
    {
        memcpy(cur, head, sizeof(head));
        flow += DFS(s, INF, t);
    }
    return flow;
}
int main()
{
    while(scanf("%d%d", &N, &M) != EOF)
    {
        init();
        getMap();
        printf("%d\n", Maxflow(source, sink));
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdoj 4289 Control 【拆点 求最小割】

标签:

原文地址:http://blog.csdn.net/chenzhenyu123456/article/details/48059055

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