XYZZY
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1461 Accepted Submission(s): 361
Problem Description 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
University of Waterloo Local Contest 2003.09.27
Recommend Eddy 看了别人的报告,基本都是先用floyd扫描一遍图的连通性,再用ford做一遍最长路即可,我觉得此题只能用bellmanford过 不能spfa(虽然也会ac)原因会在如下给出
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 99999999
using namespace std;
struct node
{
int u,v;
};
node e[10005];
int en[105],n,reach[105][105];
int first[105],next[10005];
void floyd()
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
reach[i][j]=reach[i][j]||(reach[i][k]&&reach[k][j]);
}
inline void add_edge(node e,int k)
{
next[k]=first[e.u];
first[e.u]=k;
}
int bellman_ford(int c)
{
int d[105];
int i;
for(i=1;i<=n;i++)
d[i]=-inf;
d[1]=100;
int k;
for(k=0;k<n-1;k++)
{
for(i=0;i<c;i++)
{
int u=e[i].u;
int v=e[i].v;
if(d[v]<d[u]+en[v]&&d[u]+en[v]>0)
{
d[v]=d[u]+en[v];
}
}
}
/*bool inq[105];
memset(inq,0,sizeof(inq));
queue<int> q;
q.push(1);
int num[105];
memset(num,0,sizeof(num));
while(!q.empty())
{
int x=q.front();
q.pop();
num[x]++;
if(num[x]>n) //如果存在环退出,有一种可能有多个环,但刚好最先检查出来的不行,其他环可以~~这样就会错。。但似乎没有这种数据
return reach[x][n];
inq[x]=false;
int i;
for(i=first[x];i!=-1;i=next[i])
{
int v=e[i].v;
if(d[v]<d[x]+en[v]&&d[x]+en[v]>0)
{
d[v]=d[x]+en[v];
if(!inq[v])
{
inq[v]=true;
q.push(v);
}
if(d[n]>0)
return 1;
}
}
}
//printf("123\n");*/
for(i=0;i<c;i++)
{
int u=e[i].u;
int v=e[i].v;
if(d[v]<d[u]+en[v]&&d[u]+en[v]>0)
{
if(reach[v][n]==1)
return 1;
}
}
return d[n]>0;
}
int main()
{
while(scanf("%d",&n),n!=-1)
{
int i;
memset(en,0,sizeof(en));
memset(reach,0,sizeof(reach));
memset(e,0,sizeof(e));
memset(next,-1,sizeof(next));
memset(first,-1,sizeof(first));
int c=0;
for(i=1;i<=n;i++)
{
int m;
scanf("%d%d",&en[i],&m);
int j;
for(j=0;j<m;j++)
{
int u;
scanf("%d",&u);
reach[i][u]=1;
e[c].u=i;
e[c].v=u;
add_edge(e[c],c);
c++;
}
}
floyd();
if(reach[1][n]==0)
{
printf("hopeless\n");
continue;
}
if(bellman_ford(c))
{
printf("winnable\n");
}
else
printf("hopeless\n");
}
}