poj 1932 XYZZY(spfa最长路+判断正环+floyd求传递闭包)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 4154 | Accepted: 1185 |
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 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
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
n个点,每个点有一个权值,存在负权值
构成一个有向图,点从1到n编号,起点是1,终点是n
起点和终点的权值都为0
每个点可以走多次
现在又一个人,从起点开始走,他开始具有100的能量,走到一个点,就加上这个点的权值
如果能量小于等于0的话就会死去,不能到达终点
问你他是否可以到达终点
每个点可以走多次,意味着只要存在正环且正环上的某个点到n是可达的(所以需要求传递闭包)
那么他一定可以到达n点,这是第一种情况
第二种情况就是跑个最长路,然后dis[n]是大于0的
这两种情况都是可以到达n的
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 99999999
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
}
void out(int a)
{
if(a>)
out(a/);
putchar(a%+'');
} #define max_v 105
int vis[max_v];
int dis[max_v];
int a[max_v];
int G[max_v][max_v];
int cnt[max_v];
int n;
int kk; void init()
{
kk=;
me(a,);
me(vis,);
me(G,);
me(cnt,);
me(dis,);
}
void floyd()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j)
continue;
if(G[i][k]&&G[k][j])
G[i][j]=;
}
}
}
}
int spfa(int s)
{
queue<int> q;
q.push(s);
vis[s]=;
dis[s]=a[s];
cnt[s]++; while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=; for(int i=;i<=n;i++)
{
if(G[u][i]&&dis[i]<dis[u]+a[i])
{
dis[i]=dis[u]+a[i];
if(vis[i]==)
{
q.push(i);
vis[i]=;
cnt[i]++;
if(cnt[i]>n)
{
kk=i;
return ;
}
}
}
}
}
return ;
}
int main()
{
while(~scanf("%d",&n))
{
if(n<)
break;
int x,y,z;
init();
for(int i=;i<=n;i++)
{
scanf("%d %d",&x,&y);
a[i]=x;
while(y--)
{
scanf("%d",&z);
G[i][z]=;
}
}
a[]=;
int flag=spfa();
floyd();
if((flag==&&G[kk][n])||dis[n]>)
{
printf("winnable\n");
}else
{
printf("hopeless\n");
}
}
return ;
}
/*
题目意思:
n个点,每个点有一个权值,存在负权值
构成一个有向图,点从1到n编号,起点是1,终点是n
起点和终点的权值都为0
每个点可以走多次
现在又一个人,从起点开始走,他开始具有100的能量,走到一个点,就加上这个点的权值
如果能量小于等于0的话就会死去,不能到达终点
问你他是否可以到达终点 分析:
每个点可以走多次,意味着只要存在正环且正环上的某个点到n是可达的(所以需要求传递闭包)
那么他一定可以到达n点,这是第一种情况
第二种情况就是跑个最长路,然后dis[n]是大于0的
这两种情况都是可以到达n的 注意最长路dis初始化0...... */
最新文章
- [DPDK][转]DPDK编程开发(4)—lcore
- [转载]Matlab之静态文本多行输出
- Javascript起源...
- matlab文件操作及读txt文件(fopen,fseek,fread,fclose)
- avatar Logo
- LA 2038
- mac 生成支付宝的rsa公钥和私钥 php版本
- eclise -The method onClick(View) of type new View.OnClickListener(){} must override a superclass method
- 【HDOJ】1403 Longest Common Substring
- 用fcntl()设置堵塞函数的堵塞性质
- 石头剪刀布 R语言统计分析
- Vivado常见问题集锦
- SmartSql Map
- create-react-app 修改项目端口号及ip,设置代理
- c++算法实现(一) - 递归和初始化
- 【BZOJ5417】[NOI2018]你的名字(线段树,后缀自动机)
- 潭州课堂25班:Ph201805201 django 项目 第十五课 用户注册功能后台实现 (课堂笔记)
- 一张图知道HTML5布局(图)
- 使用VNC连接管理VPS
- 2.Linux环境下配置Solr4.10.3