zoukankan      html  css  js  c++  java
  • Barricade HDU

    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
    ", dinic(s));
        }
        return 0;
    }
     
     
     
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    SAS学习笔记27 卡方检验
    SAS学习笔记26 方差分析
    SAS学习笔记25 t检验(单个样本t检验、配对样本t检验、两个独立样本t检验及方差不齐时的t'检验)
    SAS学习笔记23 线性回归、多元回归
    HTML canvas画布
    HTML 新全局特性
    MYSQL数据库学习(五)如何自定义函数
    什么是单机结构?什么是集群?什么是分布式?
    MYSQL数据库学习(四)如何备份还原数据库
    MYSQL数据库学习(三)关于DML操作
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9513310.html
Copyright © 2011-2022 走看看