HDU 5889 Barricade 最短路+网络流

题目描述:

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(N≤1000) and M(M≤10000).
The i-the line of the next M lines describes the i-th edge with three integers u,v and w where 0≤w≤1000 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
2016 ACM/ICPC Asia Regional Qingdao Online

题目分析:

n个点,m条边,每条边的距离都是1,每条边还有一个权值w,需要将这个图中每一条最短路中的若干条边去掉,同时花费这条边的权w,使得1点和n点不连通,求如何去掉边,使这些权之和最小。
先用djk或spfa求出最短路,再根据最短路建图,用dinic求最小割,得出答案。
与hdu5294类似,先求最短路再建图,求最大流(最小割)。

代码如下:

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;
const int maxn=2005;
const int INF=0x3f3f3f3f;

int n,m,T;

struct Edge
{
    int v;
    int cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost) {}
};
vector <Edge>E[maxn];

void add(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}

bool vis[maxn];
int cnt[maxn];
int dist[maxn];

bool spfa(int start,int n)
{
    memset(vis,false,sizeof(vis));
    for(int i=1; i<=n; i++)
        dist[i]=INF;
    vis[start]=true;
    dist[start]=0;
    queue<int>que;
    while(!que.empty())
        que.pop();
    que.push(start);
    memset(cnt,0,sizeof(cnt));
    cnt[start]=1;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=false;
        for(int i=0; i<E[u].size(); i++)
        {
            int v=E[u][i].v;
            if(dist[v]>dist[u]+1)
            {
                dist[v]=dist[u]+1;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                    if(++cnt[v]>n)
                        return false;
                }
            }
        }
    }
    return true;
}

//DINIC算法
/
struct Edge2
{
    int from,to,cap,flow;
    Edge2(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f) {}
};

struct Dinic
{
    int s,t;
    vector<Edge2>edges;        //边数的两倍
    vector<int> G[maxn];      //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
    bool vis[maxn];           //BFS使用
    int d[maxn];              //从起点到i的距离
    int cur[maxn];            //当前弧下标
    void init()
    {
        for (int i=0; i<=n+1; i++)
            G[i].clear();
        edges.clear();
    }
    void addedge(int from,int to,int cap)
    {
        edges.push_back(Edge2(from,to,cap,0));
        edges.push_back(Edge2(to,from,0,0));        //反向弧
        int mm=edges.size();
        G[from].push_back(mm-2);
        G[to].push_back(mm-1);
    }
    bool BFS()
    {
        memset(vis,0,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=1;
        while (!q.empty())
        {
            int x = q.front();
            q.pop();
            for (int i = 0; i<G[x].size(); i++)
            {
                Edge2 &e = edges[G[x][i]];
                if (!vis[e.to] && e.cap > e.flow)
                {
                    vis[e.to]=1;
                    d[e.to] = d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x,int a)
    {
        if (x==t || a==0)
            return a;
        int flow = 0,f;
        for(int &i=cur[x]; i<G[x].size(); i++)
        {
            Edge2 &e = edges[G[x][i]];
            if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if (a==0)
                    break;
            }
        }
        return flow;
    }
    int flow(int s,int t)
    {
        this->s=s;
        this->t=t;
        int flow = 0;
        while (BFS())
        {
            memset(cur,0,sizeof(cur));
            flow+=DFS(s,INF);
        }
        return flow;
    }

} dinic;



int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<=n; i++)
            E[i].clear();
        for(int i=0; i<m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        spfa(1,n);
        dinic.init();
        for(int i = 1; i<=n; i++)
            for(int j = 0; j<E[i].size(); j++)
                if(dist[E[i][j].v]==dist[i]+1)
                    dinic.addedge(i,E[i][j].v,E[i][j].cost);
        printf("%d\n",dinic.flow(1,n));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值