首页 > 其他分享 >HDU - 1317 XYZZY (floyd + 最长路)

HDU - 1317 XYZZY (floyd + 最长路)

时间:2023-04-07 11:01:45浏览次数:44  
标签:1317 HDU dist int edges floyd connect return include


题目大意:有一种游戏,游戏里面有N个房间,每个房间有相应的能量值,走入该房间就可以得到相应的能量值
现在你要从房间1出发,走到房间N,如果中途能量耗尽了,就表示输了,反之,则为赢

解题思路:首先得判断一下能不能到达N,这可以用Floyd去判断
如果能直接走到N的话,就算赢,否则判断一下,看是否有正环,且正环中有点能到N

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define N 110
#define INF 0x3f3f3f3f
using namespace std;

struct Edge {
    int from, to, dist;
    Edge() {}
    Edge(int from, int to, int dist): from(from), to(to), dist(dist){}
};

vector<Edge> edges;
bool connect[N][N];
int d[N], n;

void init() {
    memset(connect, 0, sizeof(connect));
    edges.clear();

    int x, m, pow;
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &pow, &m);

        for (int j = 0; j < m; j++) {
            scanf("%d", &x);
            edges.push_back(Edge(i, x, pow));
            connect[i][x] = true;
        }
    }
}

void Floyd() {
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                connect[i][j] = (connect[i][j] || (connect[i][k] && connect[k][j]));
}

bool BellmanFord() {
    for (int i = 2; i <= n; i++)
        d[i] = -INF;
    d[1] = 100;
    for (int i = 2; i <= n; i++)
        for (int j = 0; j < edges.size(); j++) {
            Edge &e = edges[j];
            if (d[e.to] < d[e.from] + e.dist && d[e.from] + e.dist > 0) {
                d[e.to] = d[e.from] + e.dist;
            }
        }

    if (d[n] > 0)
        return true;

    for (int j = 0; j < edges.size(); j++) {
        Edge &e = edges[j];
        if (d[e.to] < d[e.from] + e.dist && d[e.from] + e.dist > 0) {
            d[e.to] = d[e.from] + e.dist;
            if (connect[e.to][n])
                return true;
        }
    }

    return false;
}

void solve() {
    Floyd();
    if (!connect[1][n] || !BellmanFord()) 
        printf("hopeless\n");
    else
        printf("winnable\n");
}

int main() {
    while (scanf("%d", &n) != EOF && n != -1) {
        init();
        solve();
    }
    return 0;
}


标签:1317,HDU,dist,int,edges,floyd,connect,return,include
From: https://blog.51cto.com/u_10970600/6175580

相关文章

  • HDU 5479 Scaena Felix(DP)
    题目大意:给出一系列的由’(‘和’)’组成的字符串,现有一种操作,可以将括号的方向变反,但需要花费1现在问,需要花费多少的代价才能使该字符串的任意一个连续子串都不存在”()”的配对解题思路:用dp[i][0]表示第i个位置变成’(‘的代价,dp[i][1]表示第i个位置变成’)’的代价如果当前......
  • hdu-5306(区间最值+线段树)
    hduGorgeousSequenceHDU-5306题意:给定一个长度为n的区间,做m次操作,三种操作对于序列[L,R]区间中的每个ai,用min(ai,x)替换。打印序列[L,R]区间的最大值打印序列[L,R]区间和因为区间和与区间最值无关,所以无法直接用简单的标记处理。区间最值与区间和如何扯上关系呢?我......
  • HDU 2196 Computer(树形DP) 入门模板
    ComputerTimeLimit:1000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):34313    AcceptedSubmission(s):5345 ProblemDescriptionAschoolboughtthefirstcomputersometimeago(sothiscomputer'sidis1).D......
  • HDU动态规划题解目录
    ProblemA:MaxSum(HDU1003)   点击这里ProblemB:CommonSubsequence(HDU1159)    点击这里ProblemC:SuperJumping!Jumping!Jumping!(HDU1087)    点击这里ProblemD:HumbleNumbers(HDU1058)   点击这里ProblemE:MonkeyandBanana(HDU1069)    点击这里ProblemF:......
  • hdu - 4578(线段树)
    题目:Yuanfangispuzzledwiththequestionbelow:Therearenintegers,a1,a2,…,an.Theinitialvaluesofthemare0.Therearefourkindsofoperations.Operation1:Addctoeachnumberbetweenaxandayinclusive.Inotherwords,dotransformationak&......
  • HDU 3328 Flipper 栈的应用
    FlipperTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):521    AcceptedSubmission(s):334ProblemDescriptionLittleBobbyRoberts(sonofBigBob,ofProblemG)playsthissolitairememory......
  • hdu3595 GG and MM Every SG博弈
    这是一道很神奇的题,神奇的地方在于全网这个模型只有这题什么是EverySG?不同于普通的ICG,它由多个游戏同时进行,且必须同时进行比如这道题,要求先手一次对所有石头堆都要操作显然对于单独的每种情况,我们都可以求出这是必败态还是必胜态现在摆在我们眼前的就有一堆游戏,对于每个游......
  • hdu3980 Paint Chain SG函数+SG定理+记忆化搜索
    liyishui今天学习博弈,因为liyishui今天写树链剖分写得有点理智--题意:有一个圆,上面有n个豆子,每次要挑出连续m个没染色的豆子进行染色,不能移动时输掉游戏问先手必胜还是后手必胜,其中n、m<=1000题解:会很朴素地想到如果第一个人拿走了m个,那么剩下的就是一条链的问题。所以可以先......
  • leetcode-1317-easy
    ConvertIntegertotheSumofTwoNo-ZeroIntegersNo-Zerointegerisapositiveintegerthatdoesnotcontainany0initsdecimalrepresentation.Givenani......
  • 确定比赛名次 HDU - 1285 (拓扑排序)
    题意:有N个比赛队(1≤N≤500),编号依次为1,2,3,...,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比......