• Treasure Hunting HDU


    题意:

      输入一个n行m列的图

      每次按字母顺序走最短路, 从一个字母走到下一个字母的过程中,只能拿走一个金子,求走完当前图中所有的字母后能拿到的金子的最大值

    解析:

      bfs求最短路

      对于一个金子如果 dis1[i] + dis2[i] == dis1[next] 那么就代表着这个金子 在这条最短路上 可以拿 那么从上一个 字母 到当前节点连一条边 权值为1

      会了吧。。。

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #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 pd(a) printf("%d
    ", a);
    #define plld(a) printf("%lld
    ", a);
    #define pc(a) printf("%c
    ", a);
    #define ps(a) printf("%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 = 110000, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
    int n, m, s, t;
    int d[maxn], vis[maxn];
    int head[maxn], cur[maxn], cnt, dis1[maxn], dis2[maxn];
    char str[110][110];
    int dis[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    vector<int> g;
    
    struct edge
    {
        int u, v, c, next;
    }Edge[maxn];
    
    void add_(int u, int v, int c)
    {
        Edge[cnt].u = u;
        Edge[cnt].v = v;
        Edge[cnt].c = c;
        Edge[cnt].next = head[u];
        head[u] = cnt++;
    }
    
    void add(int u, int v, int c)
    {
        add_(u, v, c);
        add_(v, u, 0);
    }
    
    struct node
    {
        int alpha, idx;
        bool operator < (const node &a) const{
            return alpha < a.alpha;
        }
    }Node[maxn];
    
    
    void bfs1(int s)
    {
        for(int i = 0; i < maxn; i++) d[i] = INF;
        queue<int> Q;
        mem(vis, 0);
        Q.push(s);
        vis[s] = 1;
        d[s] = 0;
        while(!Q.empty())
        {
            int u = Q.front(); Q.pop();
            for(int i = 0; i < 4; i++)
            {
                int v = u + dis[i][0] * m + dis[i][1];
                if(u % m == 0 && i == 0 || (u - 1) % m == 0 && i == 1) continue;
                if(v < 1 || v > n * m || vis[v] || str[v / m + 1][v % m] == '#') continue;
                d[v] = d[u] + 1;
                vis[v] = 1;
                Q.push(v);
            }
        }
    }
    
    bool bfs2()
    {
        mem(d, 0);
        queue<int> Q;
        Q.push(0);
        d[0] = 1;
        while(!Q.empty())
        {
            int u = Q.front(); Q.pop();
            for(int i = head[u]; i != -1; i = Edge[i].next)
            {
                edge e = Edge[i];
                if(!d[e.v] && e.c > 0)
                {
                    d[e.v] = d[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;
        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[u] + 1 && e.c > 0)
            {
                int V = dfs(e.v, min(e.c, cap));
                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()
    {
        mem(d, 0);
        int ans = 0;
        while(bfs2())
        {
            memcpy(cur, head, sizeof(head));
            ans += dfs(0, INF);
        }
        return ans;
    }
    
    int main()
    {
        while(~scanf("%d%d", &n, &m))
        {
            g.clear();
            t = 10500;
            mem(head, -1);
            cnt = 0;
            int ans = 0;
            for(int i = 1; i <= n; i++)
            {
                rs(str[i] + 1);
                for(int j = 1; j <= m; j++)
                {
                    if(str[i][j] >= 'A' && str[i][j] <= 'z')
                        Node[ans].alpha = str[i][j], Node[ans++].idx = (i - 1) * m + j;
                    if(str[i][j] == '*')
                        g.push_back((i - 1) * m + j), add((i - 1) * m + j + 100, 10500, 1);
                }
            }
    
            sort(Node, Node + ans);
            int num = 0, flag = 0;
            for(int i = 0; i < ans - 1; i++)
            {
                ++num;
                bfs1(Node[i].idx);
                memcpy(dis1, d, sizeof(d));
                if(dis1[Node[i + 1].idx] == INF)
                {
                    flag = 1;
                    break;
                }
                bfs1(Node[i + 1].idx);
                memcpy(dis2, d, sizeof(d));
                for(int j = 0; j < g.size(); j++)
                    if(dis1[g[j]] + dis2[g[j]] == dis1[Node[i + 1].idx])
                        add(num, g[j] + 100, 1);
            }
            if(flag == 1)
            {
                cout << "-1" << endl;
                continue;
            }
            for(int i = 1; i <= 52; i++)
                add(0, i, 1);
    
            cout << Dinic() << endl;
        }
    
    
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    PHP用curl发送get post put delete patch请求
    ubuntu中设置php7.0-fpm开机自启动
    对计算机领域中间层的理解
    后端程序猿怎能不会的linux命令
    查看磁盘空间和目录大小的命令
    Http协议详解
    supervisor使用总结
    vmware中扩充磁盘
    HDU 4946 共线凸包
    Codeforces_GYM Flight Boarding Optimization
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9856413.html
Copyright © 2020-2023  润新知