HDU - 1317 XYZZY (floyd + 最長路)

來源:互聯網
上載者:User

標籤:

題目大意:有一種遊戲,遊戲裡面有N個房間,每個房間有相應的能量值,走入該房間就可以得到相應的能量值
現在你要從房間1出發,走到房間N,如果中途能量耗盡了,就表示輸了,反之,則為贏

解題思路:首先得判斷一下能不能到達N,這可以用Floyd去判斷
如果能直接走到N的話,就算贏,否則判斷一下,看是否有正環,且正環中有點能到N

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#define N 110#define INF 0x3f3f3f3fusing 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;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

HDU - 1317 XYZZY (floyd + 最長路)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.