Codeforces div.2 ICPC M - Moving Both Hands

题意:给定一个有向图,求1-(2~n)的点的距离,如果走不到则输出-1

思路:由于两个点必定在某一点汇合,其间终点走沿着路径的反向边,因此,我们建一个分层图,两个层之间的顶点对应连长度为0的边,其中一个是正向的图,另一个是反向的图,因此,两个图之间的从正向图的起点,走到反向图的对应终点,即是ans,我们做一下dijkstra,求单源最短路就行了。

注: 该题卡常,一下代码需要优化常数,如果需要验ac,见以下链接

(13条消息) cf-1725 M. Moving Both Hands(反图+dij)_Σ_aphasia的博客-CSDN博客

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>

//#define int ll
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 200010, M = 2 * N, B = N, MOD = 998244353;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
int h[N], e[M], ne[M], w[M], idx;
ll dist[N];
bool st[N];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll mod) {
    ll res = 1;
    while (b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

inline void init() {}

inline void add(int a, int b, int c)
{
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}

void dijkstra(int s)
{
    for (int i = 1; i <= n * 2; i++) dist[i] = LLINF;

    priority_queue<PLL, vector<PLL>, greater<PLL> > heap;
    dist[s] = 0;
    heap.push({ dist[s],s });

    while (heap.size()) {
        PLL u = heap.top(); heap.pop();
        ll ver = u.y, distance = u.x;

        if (st[ver])continue;
        st[ver] = true;

        for (int i = h[ver]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > distance + w[i])
            {
                dist[j] = distance  + w[i];
                heap.push({dist[j],j});
            }
        }
    }
}

void slove()
{
    cin >> n >> m;
    for (int i = 1; i <= 2 * n; i++)h[i] = -1;
    int a, b, c;
    pre(i, 1, m)
    {
        si(a); si(b); si(c);
        add(a, b, c);
        add(b + n, a + n, c);
    }
    
    pre(i, 1, n) add(i, i + n, 0);

    dijkstra(1);

    pre(i, 2, n) {
        if (dist[n + i] == LLINF)printf("-1 ");
        else printf("%lld ", dist[i + n]);
    }
    return;
}

signed main()
{
    int _;
    //si(_);
    _ = 1;
    init();
    while (_--)
    {
        slove();
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题是一道博弈论题目,需要使用到 SG 函数。SG 函数是一个函数,它的输入是一个状态,输出是一个非负整数,用来表示当前状态的取胜情况。具体而言,若 SG 函数的输出为 $0$,则当前状态必败;若 SG 函数的输出不为 $0$,则当前状态必胜。 对于这道题目,我们需要先了解一下如何通过数学分析求出 SG 函数。首先,我们需要对游戏的状态进行编码。对于本题,可以将状态表示为一个三元组 $(n, m, k)$,表示当前棋盘的大小为 $n \times m$,每个人每次最多可以取 $k$ 个棋子。接下来,我们需要定义一个从状态到状态集合的映射 $f(\cdot)$,表示从当前状态可以转移到哪些状态。对于本题,$f((n, m, k))$ 中的每个状态可以通过一次操作得到,即将当前棋盘中的某一行或某一列中的 $k$ 个棋子全部取走,然后将其剩余的部分作为新的棋盘状态。注意,对于这个映射,我们只需要考虑下一步能够到达的状态,而不需要考虑更远的状态。 接下来,我们需要定义 SG 函数的递归式。对于一个状态 $(n, m, k)$,其 SG 值可以通过其可以转移到的状态的 SG 值来计算。具体而言,我们可以将当前状态转移到的所有状态的 SG 值进行异或运算,并将结果加上 $1$,即 $SG((n, m, k)) = \text{mex}\{ SG(f((n, m, k))) \} + 1$,其中 $\text{mex}$ 表示集合中未出现的最小非负整数。 最后,我们需要解决的问题就是如何计算 $\text{mex}$ 函数。对于本题,可以使用一个 $O(k)$ 的算法来计算 $\text{mex}$。具体而言,我们可以记录所有出现的 SG 值,然后从 $0$ 开始枚举所有可能的非负整数,找到第一个未出现的整数即为 $\text{mex}$。 下面是 AC 代码:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值