POJ1932 HDU1317 ZOJ1935 UVA10557 XYZZY【SPFA+Floyd】

XYZZY
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4217 Accepted: 1203

Description

The prototypical computer adventure game, first designed by Will Crowther on the PDP-10 in the mid-1970s as an attempt at computer-refereed fantasy gaming, and expanded into a puzzle-oriented game by Don Woods at Stanford in 1976. (Woods had been one of the authors of INTERCAL.) Now better known as Adventure or Colossal Cave Adventure, but the TOPS-10 operating system permitted only six-letter filenames in uppercase. See also vadding, Zork, and Infocom.

It has recently been discovered how to run open-source software on the Y-Crate gaming device. A number of enterprising designers have developed Advent-style games for deployment on the Y-Crate. Your job is to test a number of these designs to see which are winnable.

Each game consists of a set of up to 100 rooms. One of the rooms is the start and one of the rooms is the finish. Each room has an energy value between -100 and +100. One-way doorways interconnect pairs of rooms.

The player begins in the start room with 100 energy points. She may pass through any doorway that connects the room she is in to another room, thus entering the other room. The energy value of this room is added to the player‘s energy. This process continues until she wins by entering the finish room or dies by running out of energy (or quits in frustration). During her adventure the player may enter the same room several times, receiving its energy each time.

Input

The input consists of several test cases. Each test case begins with n, the number of rooms. The rooms are numbered from 1 (the start room) to n (the finish room). Input for the n rooms follows. The input for each room consists of one or more lines containing:
the energy value for room i
the number of doorways leaving room i
a list of the rooms that are reachable by the doorways leaving room i

The start and finish rooms will always have enery level 0. A line containing -1 follows the last test case.

Output

In one line for each case, output "winnable" if it is possible for the player to win, otherwise output "hopeless".

Sample Input

5
0 1 2
-60 1 3
-60 1 4
20 1 5
0 0
5
0 1 2
20 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
21 1 3
-60 1 4
-60 1 5
0 0
5
0 1 2
20 2 1 3
-60 1 4
-60 1 5
0 0
-1

Sample Output

hopeless
hopeless
winnable
winnable

Source

Waterloo local 2003.09.27

问题链接POJ1932 HDU1317 ZOJ1935 UVA10557 XYZZY
问题简述:(略)
问题分析
????给定一个单向图,每个结点有一个权值,每到一个点就要加上该点的权值,判断是否存在从结点1到结点n的路径。
????最短路的模板题,暂不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ1932 HDU1317 ZOJ1935 UVA10557 XYZZY */

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100;
int g[N + 1][N + 1];
int w[N + 1];
int n;

int vis[N + 1];
int in[N + 1];
int dist[N + 1];
int spfa(int root)
{
    memset(vis, 0, sizeof(vis));
    memset(in, 0, sizeof(in));
    memset(dist, 0, sizeof(dist));

    queue <int> q;

    dist[root] = 100;
    vis[root] = 1;
    in[root]++;
    q.push(root);
    while(!q.empty()) {  //spfa
        int u = q.front();
        q.pop();
        vis[u] = 0;
        if(in[u] > n)
            break;
        for(int i = 1; i <= n; i++) {
            if(g[u][i] && dist[i] < dist[u] + w[i]) {
                dist[i] = dist[u] + w[i];
                if(vis[i] == 0) {
                    vis[i] = 1;
                    q.push(i);
                    in[i]++;
                }
            }
        }
    }
    if(dist[n] > 0) return 1; //存在最短路
    else{
        for(int k=1;k<=n;k++)  //floyd
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    if(g[i][k] && g[k][j])
                        g[i][j]=1;
        for(int i=1;i<=n;i++)
            if(in[i]>n && g[1][i] && g[i][n]) //存在正环和连通
                return 1;
    }
    return 0;
}

int main()
{
    while(scanf("%d", &n) && n != -1) {
        memset(g, 0, sizeof(g));

        for(int u = 1; u <= n; u++) {
            int m, v;
            scanf("%d%d", &w[u], &m);
            while(m--) {
                scanf("%d", &v);
                g[u][v] = 1;
            }
        }

        printf(spfa(1) ? "winnable\n" : "hopeless\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10508224.html

时间: 2024-08-06 02:53:35

POJ1932 HDU1317 ZOJ1935 UVA10557 XYZZY【SPFA+Floyd】的相关文章

poj 2449 Remmarguts&amp;#39; Date 【SPFA+Astar】【古典】

称号:poj 2449 Remmarguts' Date 意甲冠军:给定一个图,乞讨k短路. 算法:SPFA求最短路 + AStar 以下引用大牛的分析: 首先,为了说话方便,列出一些术语: 在启示式搜索中,对于每一个状态 x.启示函数 f(x) 一般是这种形式: f(x) = g(x) + h(x) 当中 g(x) 是从初始状态走到 x 所花的代价:h(x) 是从 x 走到目标状态所须要的代价的预计值. 相对于 h(x).另一个概念叫 h*(x),表示从 x 走到目标状态所须要的实际最小代价(

poj 2449 Remmarguts&#39; Date 【SPFA+Astar】【经典】

题目:poj 2449 Remmarguts' Date 题意:给出一个图,求k短路. 算法:SPFA求最短路 + AStar 下面引用大牛的分析: 首先,为了说话方便,列出一些术语: 在启发式搜索中,对于每个状态 x,启发函数 f(x) 通常是这样的形式: f(x) = g(x) + h(x) 其中 g(x) 是从初始状态走到 x 所花的代价:h(x) 是从 x 走到目标状态所需要的代价的估计值. 相对于 h(x),还有一个概念叫 h*(x),表示从 x 走到目标状态所需要的实际最小代价(当然

【动态规划+Floyd】OpenJudge3368

OpenJudge上刷水都不会了……这题居然写了一个半小时…… [题目大意] 诸葛亮要征服N城市.然而,City-X在击败City-2,City-3……City-x-1后击败.关羽,张飞,赵云,每个人都应该领导一个军队.三个军队从City-0出发,征服所有的城市.求三个军队的行程总长的最小值. [思路] 首先求出最短路径.我们用dp[i][j][k]表示当前三个部队分别位于i.j.k时的答案(j<=k<=i).分别考虑从i.j.k中的一支部队走到i+1的情况,总共三个递推式. 对于返回,直接假

hdoj-1869 六度分离【最短路径--dijkstra&amp;&amp;spfa&amp;&amp;floyd】

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1869 解题思路: 转化成最短路径问题,如果两人认识,把两者之间距离看成1      如果任意两人之间隔着7个人及其以上 (即距离>7)   则不满足六度分离 spfa: #include<cstdio> #include<cstring> #include<queue> #define INF 0x3f3f3f3f #define MAXN 100+10//点数 #def

Codeforces 938D Buy a Ticket 【spfa优化】

用到了网络流的思想(大概).新建一个源点s,所有边权扩大两倍,然后所有的点向s连边权为点权的无向边,然后以s为起点跑spfa(S什么L优化的),这样每个点到s的距离就是答案. 原因的话,考虑答案应该是min(2*dis[i][j]+a[j]} ),那么每个点到s的距离就是若干条边边权的二倍加上某个点的点权,并且这个组合是最小的.证毕. #include<iostream> #include<cstdio> #include<queue> using namespace

bzoj 2750: [HAOI2012]Road【spfa+dfs】

枚举起点做spfa,然后一条边在最短路上的条件是dis[e[i].to]==dis[u]+e[i].va,所以每次spfa完之后,dfs出a[i]表示经过i点的最短路的起点数,b[i]表示经过i点的最短路的终点数,一条边(u,v)在当前起点下的答案就是a[u]*b[v],最终答案是总和 因为最短路构成一个DAG,所以a是按照类似拓扑序的东西来dfs的 #include<iostream> #include<cstdio> #include<queue> #include

【floyd】HDU 1874 畅通工程续

之后的题解偏重实用/总结性质,尽量理解算法本身而不是题,时间复杂度什么的也可以放放. 很久之前做过这个题,当时使用dijkstra做的,关于几个最短路算法,分类的话可以分为以下几种. 1.单源最短路:已知起点(终点),计算从源点到其他各个顶点的最短路径长度. 典型算法:Dijkstra,Bellman-Ford(可以算负的,比较慢),spfa(负权能用,加了松弛操作,速度比较炸天) 2.全局最短路:从一点到另一点,典型如Floyd,A*启发式算法. 重新用floyd写一遍: #include <

【spfa】bzoj3921 Mimori与树海

考虑“删除后图仍连通”,即其不是无向图的桥(bridge),可以用Tarjan算法预处理,这里不赘述. [算法一] 枚举删除的是哪条边,然后枚举起点,暴搜,统计答案. 可以通过0.1号测试点. 预计得分:20分. [算法二] 枚举删除的是哪条边,接着, ①枚举起点,∵op=0,∴裸最短路,Heap-dijkstra/SPFA.时间复杂度: O(m2*n*logn)/O(m2*n*k). ②Floyd,时间复杂度O(m*n3). 可以通过0.1.2.3号测试点. 预计得分:40分. [算法三] 枚

POJ2253&amp;ZOJ1942--Frogger【SPFA】单源最短路变形

链接:http://poj.org/problem?id=2253 题意:一个青蛙在一块石头上,看到了另一个青蛙在另一块石头上,它想跳过去找它,如果距离太远它就需要借助别的石头当跳板,两块石头之间的青蛙距离被定义成两块石头之间所有路径中最大跳跃距离的最小值,求两个青蛙之间的青蛙距离. poj2263和它类似,链接:http://poj.org/problem?id=2263 解题报告:Here 这是最短路的变形,每两点之间都有路可以跳,更新最短路的值,权值记录成目前到这一点的最小青蛙距离就行了