POJ 1932 XYZZY (spfa判断正环+判断是否连通 or SPFA+floyd 求解差分约束)

36 篇文章 4 订阅
9 篇文章 0 订阅


XYZZY
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3836 Accepted: 1109

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


题意:根据给出的关系图,判断是否存在一条从1到n的路径,且最终的cost值为正值,初始值为100。中间各个room的值有正有负。但在求路径的时候,任何一点的value都不能小于或者等于零,否则这条路就不能通。当然,如果有正环,并且可以从1到n是连通的,那么就一定winnable。

思路:

这题,不用构边,因为他是加上到达点的权值的, 因此, 这题最长路直接写就好了, 不用权值为负,跑最短路了

首先明确,要求到n的时候是 >= 0的, 所以就是求最长路,只要有一个符合,就符合题意,所以求最长路, 然后就是要求每个点都 > 0, 就在spfa里加一句话, 如果dis[u] + val[to] > 0,才更新, 如果不是,就不更新让他继续是-inf,这样如果有 <= 0的点存在就到达不了终点,这是根据题意得来的,那如果存在正环呢?存在正环说明他的值可以很大很大, 这样如果1,n连通, 肯定就是可以到达的, 如果不联通,只在一个联通块有正环, 那还是到达不了,为了解决这个问题,有两种解法:

1: 在做spfa时候, 如果出线正环,直接把他的值变成INF, 这样后面的值肯定都可以更新~这样正环里所有的值都是INF, 如果1,n能通过这个正环连接,dis【n】的值肯定>0(被更新过了),但要注意可能有多个正环,INF开太大容易炸,可以开long long 并且第二遍正环的时候continue掉, 不过这题数据水,没啥事;

2.可以先做spfa, 然后弗洛伊德,判断每个正环里的点,可不可以连接1,n


方法1代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 105;
const int INF = 2e9;
vector<int> v[maxn];
int book[maxn], cnt[maxn], dis[maxn], val[maxn], n, x;
void spfa()
{
    queue<int> q;
    memset(book, 0, sizeof(book));
    memset(cnt, 0, sizeof(cnt));
    for(int i = 0; i < maxn; i++) dis[i] = -INF;
    dis[1] = val[1];
    q.push(1);
    book[1] = 1;
    cnt[1] = 1;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        book[u] = 0;
        if(cnt[u] > n) dis[u] = INF;
        for(int i = 0; i < v[u].size(); i++)
        {
            int to = v[u][i];
            if(dis[u] + val[to] > dis[to] && dis[u] + val[to] >= 0)
            {
                dis[to] = val[to] + dis[u];
                if(cnt[to] > n) continue;
                if(!book[to])
                {
                    book[to] = 1;
                    q.push(to);
                    cnt[to]++;
                }
            }
        }
    }
    if(dis[n] > 0) printf("winnable\n");
    else printf("hopeless\n");
}
int main()
{
    while(~scanf("%d", &n), n != -1)
    {
        for(int i = 0; i < maxn; i++)
            v[i].clear();
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &val[i]);
            int len;
            scanf("%d", &len);
            for(int j = 1; j <= len; j++)
            {
                scanf("%d", &x);
                v[i].push_back(x);
            }
        }
        val[1] = 100;
        spfa();
    }
    return 0;
}
方法2代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int INF=-9999999;
const int MAXN=111;//注孤 - -|||
bool map[MAXN][MAXN];
int val[MAXN],dis[MAXN];
int n;
bool SPFA()
{
	for(int i=1;i<=n;i++)
		dis[i]=INF;

	bool vis[MAXN]={0};

	int cnt[MAXN]={0};
	
	queue<int> q;
	q.push(1);
	vis[1]=true;
	dis[1]=100;
	cnt[1]=1;

	while(!q.empty())
	{
		int cur=q.front();
		q.pop();
		if(cnt[cur] > n)
			break;
		vis[cur]=false;
		for(int i=1;i<=n;i++)
		{
			if(map[cur][i]==true && dis[cur] + val[i] > dis[i]
			&& dis[cur] + val[i] > 0 )
			{
				dis[i]=dis[cur] + val[i];
				if(!vis[i])
				{
					q.push(i);
					vis[i]=true;
					cnt[i]++;
				}
			}		
		}
	}

	if(dis[n]>0)
		return true;
	else 
	{
		for(int k=1;k<=n;k++)
			for(int i=1;i<=n;i++)
				for(int j=1;j<=n;j++)
					if(map[i][k] && map[k][j])
						map[i][j]=true;

		for(int i=1;i<=n;i++)
			if(cnt[i]>n && map[1][i] && map[i][n]) //忘记要从i到N了
				return true;
		return false;
	}
}

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

		for(int i=1;i<=n;i++)
		{
			int cnt,temp;
			scanf("%d %d",&val[i],&cnt);
			for(int j=1;j<=cnt;j++)
			{
				scanf("%d",&temp);
				map[i][temp]=true;
			}
		}

		if(SPFA())
			puts("winnable");
		else
			puts("hopeless");

	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值