hdoj 3491 Thieves 【最小割 + 拆点】

题目:hdoj 3491 Thieves

题意:给出一个无向图,然后有个出发城市s,结束城市 t ,然后每个点有流量限制,问你最少用多少的人能够使得 s 到 t 没有流量。

分析:题意是抽象出来的,但是很明显看出来是求最小割。难点有2

1:无向图,所以要建双向边

2:点有流量限制,所以要拆点,拆成两个点,然后这两点的容量为点的限制,图中点的连接设置流量为inf,保证割不掉,只能从点之间割。

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int N = 500;
const int inf = 0x3f3f3f3f;
int n,m;
struct Node
{
    int from,to,cap,flow;
};
vector<int> v[N];
vector<Node> e;
int vis[N];  //构建层次图
int cur[N];
void add_Node(int from,int to,int cap)
{
    e.push_back((Node)
    {
        from,to,cap,0
    });
    e.push_back((Node)
    {
        to,from,0,0
    });
    int tmp=e.size();
    v[from].push_back(tmp-2);
    v[to].push_back(tmp-1);
}
bool bfs(int s,int t)
{
    Del(vis,-1);
    queue<int> q;
    q.push(s);
    vis[s] = 0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=0; i<v[x].size(); i++)
        {
            Node tmp = e[v[x][i]];
            if(vis[tmp.to]<0 && tmp.cap>tmp.flow)  //第二个条件保证
            {
                vis[tmp.to]=vis[x]+1;
                q.push(tmp.to);
            }
        }
    }
    if(vis[t]>0)
        return true;
    return false;
}
int dfs(int o,int f,int t)
{
    if(o==t || f==0)  //优化
        return f;
    int a = 0,ans=0;
    for(int &i=cur[o]; i<v[o].size(); i++) //注意前面 ’&‘,很重要的优化
    {
        Node &tmp = e[v[o][i]];
        if(vis[tmp.to]==(vis[o]+1) && (a = dfs(tmp.to,min(f,tmp.cap-tmp.flow),t))>0)
        {
            tmp.flow+=a;
            e[v[o][i]^1].flow-=a; //存图方式
            ans+=a;
            f-=a;
            if(f==0)  //注意优化
                break;
        }
    }
    return ans;  //优化
}

int dinci(int s,int t)
{
    int ans=0;
    while(bfs(s,t))
    {
        Del(cur,0);
        int tm=dfs(s,inf,t);
        ans+=tm;
    }
    return ans;
}
int num[220];
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        int s,t;
        scanf("%d%d%d%d",&n,&m,&s,&t);
        for(int i=1;i<=n;i++){
            scanf("%d",&num[i]);
            add_Node(i<<1,i<<1|1,num[i]);
        }
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add_Node(x<<1|1,y<<1,inf);
            add_Node(y<<1|1,x<<1,inf);  //无向图
        }
        printf("%d\n",dinci(s<<1|1,t<<1));
        for(int i=0;i<=2*n+2;i++)
            v[i].clear();
        e.clear();
    }
    return 0;
}
时间: 2024-08-27 07:49:05

hdoj 3491 Thieves 【最小割 + 拆点】的相关文章

poj 1815 Friendship (最小割+拆点+枚举)

题意: 就在一个给定的无向图中至少应该去掉几个顶点才能使得s和t不联通. 算法: 如果s和t直接相连输出no answer. 把每个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) v和v''分别是一个流进的点,一个流出的点. 根据求最小割的性质,权值小的边是可能被选择的(断开的). 添加源点st=0和汇点en=2*n+1,源点与s连权值为inf的边,t''与汇点连权值为inf的边. s与s'',t与t''连权值为inf的边,这样保证自己和自己是不会失去联系的. 如果i和j有边

hdoj 3820 Golden Eggs 【最小割+拆点】

题目:hdoj 3820 Golden Eggs 题意:给出一个矩阵,然后当前有三种选择,放一个金蛋,放一个银蛋,或者不放,然后给出每个格子放金蛋或者银蛋的得分,如果金蛋相邻的话每个得分要减掉cost1,银蛋相邻的话每个减去cost2得分,问最大得分多少? 分析:做这个题目推荐先做hdoj 1659 ,3657点击打开链接 ,这个题目相当于前两个的融合在加点变化. 首先我们发现和前两个题目一样要求不能相邻,否则要减去一定的值,那么我们可以确定同样要用当前点的行列和的奇偶性建图. 那么我们可以按照

hdu 4289 Control(最小割 + 拆点)

http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2247    Accepted Submission(s): 940 Problem Description You, the head of Department of Secu

HDU 4289 Control (最小割 拆点)

Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2139    Accepted Submission(s): 904 Problem Description You, the head of Department of Security, recently received a top-secret informati

hdu-4289.control(最小割 + 拆点)

Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5636    Accepted Submission(s): 2289 Problem Description You, the head of Department of Security, recently received a top-secret informati

URAL 1277 Cops and Thieves 最小割 无向图点带权点连通度

Cops and Thieves Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1277 Appoint description: Description The Galaxy Police (Galaxpol) found out that a notorious gang of thieves has plans for stea

Thieves (hdu 3491 拆点 最小割)

Thieves Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1453    Accepted Submission(s): 651 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 100) cities, with M (M <= 10

hdoj 4289 Control 【拆点 求最小割】

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 informati

hdu4289 Control --- 最小割,拆点

给一个无向图,告知敌人的起点和终点,你要在图上某些点安排士兵,使得敌人无论从哪条路走都必须经过士兵. 每个点安排士兵的花费不同,求最小花费. 分析: 题意可抽象为,求一些点,使得去掉这些点之后,图分成了两部分,敌人的起点和终点分别在这两部分里.即求最小割. 问题是最小割是边,这里把点拆成两个,自己到自己连边,边权为该点点权.其他题目给的边照连就可以了. 为了方便,对于点i,拆成(i,i+n). 因此,对于题目给的边(a,b),就连双向边边:(a+n,b,inf).(b+n,a,inf) #inc