Barricade HDU - 5889(最短路+最小割)

Barricade

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


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
 
注意审题
题中说的是敌人会走最短的路 所以我们把所有的最短路都拿出来 跑一边最大流即可
怎样把所有的最短路拿出来 跑一边最短路即可。。。因为跑完之后 起点和终点之间的所有的最短路 可以通过判断 d[e.v] == d[e.u] + 1 来进行建网络流的图
注意建网络流图的方式 不然会t
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
int head[maxn], head2[maxn], dis[maxn], d[maxn], vis[maxn], cur[maxn];
int cnt, cnt2;
int n, m, s, t;

struct node
{
    int u, v, w, c, next;
}Node[maxn<<1];

void add_(int u, int v, int w, int c)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt].w = w;
    Node[cnt].c = c;
    Node[cnt].next = head[u];
    head[u] = cnt++;
}

void add(int u, int v, int w, int c)
{
    add_(u, v, w, c);
    add_(v, u, w, c);
}

void spfa()
{
    for(int i=0; i<=n; i++) dis[i] = INF;
    mem(vis, 0);
    queue<int> Q;
    Q.push(s);
    vis[s] = 1;
    dis[s] = 0;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        vis[u] = 0;
        for(int i=head[u]; i!=-1; i=Node[i].next)
        {
            node e = Node[i];
            if(dis[e.v] > dis[u] + e.w)
            {
                dis[e.v] = dis[u] + e.w;
                if(!vis[e.v])
                {
                    vis[e.v] = 1;
                    Q.push(e.v);
                }
            }
        }
    }
}

struct edge
{
    int u, v, c, next;
}Edge[maxn<<1];

void add_edge(int u, int v, int c)
{
    Edge[cnt2].u = u;
    Edge[cnt2].v = v;
    Edge[cnt2].c = c;
    Edge[cnt2].next = head2[u];
    head2[u] = cnt2++;
}

void add_Edge(int u, int v, int c)
{
    add_edge(u, v, c);
    add_edge(v, u, 0);
}

bool bfs()
{
    queue<int> Q;
    mem(d, 0);
    Q.push(s);
    d[s] = 1;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        for(int i=head2[u]; i!=-1; i=Edge[i].next)
        {
            edge e = Edge[i];
            if(!d[e.v] && e.c > 0)
            {
                d[e.v] = d[e.u] + 1;
                Q.push(e.v);
                if(e.v == t) return 1;
            }
        }
    }
    return d[t] != 0;
}

int dfs(int u, int cap)
{
    int ret = 0, V;
    if(u == t || cap == 0)
        return cap;
    for(int &i=cur[u]; i!=-1; i=Edge[i].next)
    {
        edge e = Edge[i];
        if(d[e.v] == d[e.u] + 1 && e.c > 0)
        {
            int V = dfs(e.v, min(cap, e.c));
            Edge[i].c -= V;
            Edge[i^1].c += V;
            ret += V;
            cap -= V;
            if(cap == 0) break;
        }
    }
    if(cap > 0) d[u] = -1;
    return ret;
}

int dinic(int u)
{
    int ans = 0;
    while(bfs())
    {
        memcpy(cur, head2, sizeof(head2));
        ans += dfs(u, INF);
    }
    return ans;
}

void build()
{
    for(int i=1; i<=n; i++)
        for(int j=head[i]; j!=-1; j=Node[j].next)
        {
            node e = Node[j];
            if(dis[e.v] == dis[e.u] + 1)
                add_Edge(e.u, e.v, e.c);
        }
}

int main()
{
    int T;
    rd(T);
    while(T--)
    {
        mem(head, -1);
        mem(head2, -1);
        cnt = cnt2 = 0;
        rd(n); rd(m);
        rep(i, 0, m)
        {
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            add(u, v, 1, c);
        }
        s = 1, t = n;
        spfa();
        build();
        printf("%d\n", dinic(s));
    }
    return 0;
}

 

 
 
 

 

转载于:https://www.cnblogs.com/WTSRUVF/p/9513310.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值