hdu 5889 Barricade (最短路的最小割)

Barricade

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1967    Accepted Submission(s): 570


Problem Description
The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as  N  towns and  M  roads, and each road has the same length and connects two towns. The town numbered  1  is where general's castle is located, and the town numbered  N  is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the  i -th road requires  wi  units of wood. Because of lacking resources, you need to use as less wood as possible.
 

Input
The first line of input contains an integer  t , then  t  test cases follow.
For each test case, in the first line there are two integers  N(N1000)  and  M(M10000) .
The  i -the line of the next  M  lines describes the  i -th edge with three integers  u,v  and  w  where  0w1000  denoting an edge between  u  and  v  of barricade cost  w .
 

Output
For each test cases, output the minimum wood cost.
 

Sample Input
  
  
1 4 4 1 2 1 2 4 2 3 1 3 4 3 4
 

Sample Output
  
  
4
 

Source
 

Recommend
wange2014


最小割 ==  最大流

最大流 == 每条增广路的最小花费和
最小割 == 切断每条增广路的最小花费

题目中的敌人会选择最短路 进攻。
当最短路唯一的时候我们只需要切断最短路那条边即可。
当最短路不唯一时我们需要切断的路径就多了。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
#define N 1005
#define inf 0x3f3f3f3f
using namespace std;

struct node
{
    int  v,len,w;
    node(int x,int y,int z)
    {
        v=x,len=y,w=z;
    }
};
struct edge
{
    int to,cap,rev;
    edge(int x,int y,int z)
    {
        to=x,cap=y,rev=z;
    }
};
int n,m;
vector<node>g[N];
vector<edge>G[N];
void addedge(int u,int v,int cap)
{
    G[u].push_back(edge(v,cap,G[v].size()));
    G[v].push_back(edge(u,0,G[u].size()-1));
}

int d[N];
bool used[N];

/// 清空数组
void init()
{
    memset(d,1,sizeof(d));
    for(int i=0; i<N; i++)
        g[i].clear(),G[i].clear();
}

/// SPFA  跑最短路。。
void SPFA(int u)
{
    int v;
    int vis[N];
    memset(vis,0,sizeof(vis));
    queue<int>q;
    q.push(u);
    vis[u]=1,d[u]=0;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=0; i<g[u].size(); i++)
        {
            v=g[u][i].v;
            if(d[v]>d[u]+g[u][i].len)
            {
                d[v]=d[u]+g[u][i].len;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
void solve()
{
    for(int i = 1; i <= n; i++)
    {
        int len = g[i].size();
        for(int j = 0; j < len; j++)
        {
            int v = g[i][j].v;
            int l = g[i][j].len;
            int w = g[i][j].w;
            if(d[v] - d[i] == l)   ///判断是否为最短路
            {
                addedge(i,v,w);///从 i -> v 是最短路存边
            }
        }
    }
}
int dfs(int v,int t,int f)
{
    if(v==t)
    {
        return f;
    }
    used[v]=true;
    for(int i=0; i<G[v].size(); i++)
    {
        edge &e=G[v][i];
        if(!used[e.to]&&e.cap>0)
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)///最大流
{
    int flow=0;
    while(1)
    {
        memset(used,false,sizeof(used));
        int f = dfs(s,t,inf);
        if(f==0)
            return flow;
        flow+=f;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        int u,v,w;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            g[u].push_back(node(v,1,w));
            g[v].push_back(node(u,1,w));
        }
        SPFA(1);
        solve();
        long long res = max_flow(1,n);
        printf("%lld\n",res);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值