HDU--Barricade--最短路存图+网络流

Barricade

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


 

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

 

 

Recommend

wange2014

 

 

给出N个点M条边,敌人在N点,每次都会选择最短路径来攻击1点,初始边权为1,想要改变第i条边价值为wi,问 现在想堵住一些路使得最短路径增大,需要增加边权的最小值。

跑一遍最短路之后,然后

for(int u = 1; u <= n; u ++)
        {
            for(int i = DJ.head[u]; ~i; i = DJ.nxt[i])
            {
                int v = DJ.to[i];
                ll cst = DJ.val[i];
                if(d[v]+1 ==  d[u])
                {
                    DC.ade(u, DJ.to[i], cst), DC.ade(DJ.to[i],u,0);
                }
            }
        }

这样就把最短路径上的点存了下来。

然后再跑一遍最大流最小割,求出答案。

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5;
typedef pair<ll, int> P;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, cas;

ll d[maxn];
struct GDJ
{
    int head[maxn], cnt;
    int to[maxn << 1], nxt[maxn << 1];
    ll val[maxn << 1];

    void init()
    {
        memset(head, -1, (n + 5) * sizeof(int));
        cnt = -1;
    }

    void ade(int a, int b, ll c)
    {
        to[++cnt] = b;
        nxt[cnt] = head[a];
        val[cnt] = c;
        head[a] = cnt;
    }

    bool vis[maxn];
    void dj(int s)
    {
        priority_queue<P, vector<P>, greater<P> > que;
        memset(d, 0x3f, (n + 5) * sizeof(ll));
        memset(vis, 0, (n + 5) * sizeof(bool));
        d[s] = 0;
        que.push(P(0, s));
        while(!que.empty())
        {
            P p = que.top();
            que.pop();
            int u = p.second;
            if(vis[u])
                continue;
            vis[u] = 1;
            for(int i = head[u]; ~i; i = nxt[i])
            {
                int v = to[i];
                if(d[v] > d[u] + 1)
                {
                    d[v] = d[u] + 1;
                    que.push(P(d[v], v));
                }
            }
        }
    }
} DJ;

struct GDC
{
    int depth[maxn], cur[maxn], head[maxn], cnt;
    int to[maxn << 1], nxt[maxn << 1];
    ll val[maxn << 1];

    void init()
    {
        memset(head, -1, (n + 5) * sizeof(ll));
        cnt = -1;
    }

    void ade(int a, int b, ll c)
    {
        to[++cnt] = b;
        nxt[cnt] = head[a];
        val[cnt] = c;
        head[a] = cnt;
    }

    bool bfs()
    {
        queue<int> que;
        que.push(1);
        memset(depth, 0, (n + 5) * sizeof(int));
        depth[1] = 1;
        while(!que.empty())
        {
            int u = que.front();
            que.pop();
            for(int i = head[u]; ~i; i = nxt[i])
            {
                if(val[i] > 0 && depth[to[i]] == 0)
                {
                    depth[to[i]] = depth[u] + 1;
                    que.push(to[i]);
                }
            }
        }
        if(depth[n])
            return 1;
        else
            return 0;
    }

    ll dfs(int u, ll dist)
    {
        if(u == n)
            return dist;
        for(int &i = cur[u]; ~i; i = nxt[i])
        {
            if(depth[to[i]] == depth[u] + 1 && val[i] > 0)
            {
                ll tmp = dfs(to[i], min(dist, val[i]));
                if(tmp > 0)
                {
                    val[i] -= tmp;
                    val[i ^ 1] += tmp;
                    return tmp;
                }
            }
        }
        return 0;
    }

    ll dinic()
    {
        ll res =0, d;
        while(bfs())
        {
            for(int i = 0; i <= n; i ++)
                cur[i] = head[i];
            while(d = dfs(1,INF))
                res += d;
        }
        return res;
    }
} DC;

int main()
{
    scanf("%d",&cas);
    while(cas --)
    {
        scanf("%d %d",&n,&m);
        DJ.init();
        for(int i = 1; i <= m; i++)
        {
            int a, b;
            ll c;
            scanf("%d %d %d",&a,&b,&c);
            DJ.ade(a, b, c);
            DJ.ade(b, a, c);
        }
        DJ.dj(n);
        DC.init();
        for(int u = 1; u <= n; u ++)
        {
            for(int i = DJ.head[u]; ~i; i = DJ.nxt[i])
            {
                int v = DJ.to[i];
                ll cst = DJ.val[i];
                if(d[v]+1 ==  d[u])
                {
                    DC.ade(u, DJ.to[i], cst), DC.ade(DJ.to[i],u,0);
                }
            }
        }
        printf("%lld\n",DC.dinic());
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值