P1613 跑路

P1613 跑路

题意

已知一个每秒可以移动 2 k 2^k 2k 距离, k k k 任意

给出一个有向图,上限 50 50 50 个点, 10000 10000 10000 条有向边

请问从 1 1 1 n n n 所需的最少时间


思路

由于与 2 k 2^k 2k 次方有关,所以可以像 S T ST ST 表一样处理

d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k] 表示 i i i j j j 之间距离为 2 k 2^k 2k 的路径是否存在

for (int l = 1; l <= 64; l++)
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                dp[i][j][l] |= (dp[i][k][l - 1] & dp[k][j][l - 1]);

代码

#include 
using namespace std;
const int maxn = 100;
const int inf = 0X3f3f3f3f;
bool dp[maxn][maxn][maxn];
int a[maxn][maxn];
int main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);
    // cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    int u, v;
    for (int i = 1; i <= m; i++) {
        cin >> u >> v;
        dp[u][v][0] = true;
    }
    for (int l = 1; l <= 64; l++)
        for (int k = 1; k <= n; k++)
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                    dp[i][j][l] |= (dp[i][k][l - 1] & dp[k][j][l - 1]);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            for (int k = 1; k <= 64; k++)
                dp[i][j][0] |= dp[i][j][k];
    memset(a, 0X3f, sizeof(a));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (dp[i][j][0])
                a[i][j] = 1;
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
    cout << a[1][n] << '\n';
    return 0;
}

你可能感兴趣的:(DP,每日DP)