zoukankan      html  css  js  c++  java
  • 1377. T 秒后青蛙的位置,unordered_map+bitset,学习复合类型关键字如何添加hash函数

    题目链接:T 秒后青蛙的位置

    bitset保存遍历状态

    unordered_map保存dp值

    unordered_map自定义键用法

    #include<bits/stdc++.h>
    #define pb push_back
    using namespace std;
    const int N=1e2+5;
    class Solution {
    public:
        struct hash_name{
            size_t operator()(const pair<bitset<N>,int> & p) const{
                return (hash<bitset<N>>()(p.first)*100) + hash<int>()(p.second);
            }
        };
    
        unordered_map<pair<bitset<N>,int>,double,hash_name>dp[2];
        vector<int>g[N];
        void init(vector<vector<int>>& edges)
        {
            for(auto e:edges)
            {
                g[e[0]].pb(e[1]);
                g[e[1]].pb(e[0]);
            }
        }
        vector<int> get_to(bitset<N> st,int v)
        {
            vector<int>ans;
            for(auto x:g[v])
            {
                if(!st[x])
                {
                    ans.pb(x);
                }
            }
            return ans;
        }
        void add(int id,pair<bitset<N>,int> sta,double p)
        {
            if(dp[id].count(sta))
            {
                dp[id][sta]+=p;
            }
            else
            {
                 dp[id][sta]=p;
            }
        }
        double frogPosition(int n, vector<vector<int>>& edges, int t, int target) {
            init(edges);
            int tmp=0;
            pair<bitset<N>,int>state;
            state.first[1]=1;
            state.second=1;
            dp[0][state]=1;
            for(int i=0;i<t;i++)
            {
                dp[tmp^1].clear();
                for(auto it:dp[tmp])
                {
                    pair<bitset<N>,int>sta=it.first;
                    //cout<<sta.first<<" "<<sta.second<<endl;
                    vector<int> to=get_to(sta.first,sta.second);
                    if(to.size()==0)
                    {
                        add(tmp^1,sta,it.second);
                    }
                    else
                    {
                        for(auto x:to)
                        {
                            pair<bitset<N>,int>tt=sta;
                            tt.first[x]=1;
                            tt.second=x;
                            //cout<<x<<" "<<endl;
                            add(tmp^1,tt,it.second/to.size());
                        }
                    }
                }
                tmp^=1;
            }
            double ans=0;
            for(auto it:dp[tmp])
            {
                pair<bitset<N>,int>sta=it.first;
                if(sta.second==target)
                {
                    ans+=it.second;
                }
            }
            return ans;
        }
    };
  • 相关阅读:
    [USACO5.1]二维凸包模板
    HTML 5 Web 存储
    计算两个坐标点的距离(高德or百度)
    Cache的一些总结
    PowerDesigner最基础的使用方法
    MongoDB安装
    MangoDB CSharp Driver
    Linq语法详细
    where用法
    .Net下二进制形式的文件存储与读取
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/15429807.html
Copyright © 2011-2022 走看看