HDU 1317(Floyd判断连通性+spfa判断正环)
XYZZY
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4421    Accepted Submission(s): 1252
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.
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. 
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
hopeless
winnable
winnable
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int n;
int energy[maxn];
int power[maxn]; //记录到每一点的力量值
int cnt[maxn]; //记录访问每一个点的次数
bool g[maxn][maxn];
bool reach[maxn][maxn];
void floyd()
{
for(int k = ; k<=n; k++)
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
if(reach[i][j]||(reach[i][k]&&reach[k][j])) reach[i][j] = true;
}
bool spfa(int s)
{
queue<int> q;
memset(power,,sizeof(power));
power[s] = ;
memset(cnt,,sizeof(power));
q.push(s);
while(!q.empty())
{
int v = q.front();
q.pop();
cnt[v]++;
if(cnt[v]>=n) return reach[v][n]; //有正环
for(int i = ; i<=n; i++)
{
if(g[v][i]&&power[i]<power[v]+energy[i]&&power[v]+energy[i]>)
{
power[i] = power[v]+energy[i]; q.push(i);
}
}
}
return power[n]>; }
void solve()
{
while(scanf("%d",&n)!=EOF&&n!=-)
{
memset(g,false,sizeof(g));
memset(reach,false,sizeof(reach));
for(int i = ; i<=n; i++)
{
int k,door;
scanf("%d %d",&energy[i],&k);
for(int j = ; j<=k; j++)
{
scanf("%d",&door);
g[i][door] = true;
reach[i][door] = true;
}
}
floyd();
if(!reach[][n])
{
printf("hopeless\n");
continue;
}
if(spfa())
{
printf("winnable\n");
}
else
{
printf("hopeless\n");
}
}
}
int main()
{
solve();
return ;
}
HDU 1317(Floyd判断连通性+spfa判断正环)的更多相关文章
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
		
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
 - POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
		
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
 - POJ 2240 Arbitrage spfa 判正环
		
d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...
 - poj1860(spfa判正环)
		
题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...
 - POJ1860 Currency Exchange —— spfa求正环
		
题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
 - loj 1221(spfa判正环)
		
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25957 思路:由于路线为一个环,将路径上的权值改为c-p*d,那么 ...
 - POJ1680 Currency Exchange SPFA判正环
		
转载来源:優YoU http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...
 - poj1860 Currency Exchange(spfa判断正环)
		
Description Several currency exchange points are working in our city. Let us suppose that each point ...
 - poj3621 SPFA判断正环+二分答案
		
Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...
 
随机推荐
- Hadoop YARN ERROR 1/1 local-dirs are bad *, 1/1 log-dirs are bad *
			
转 http://blog.csdn.net/u012303571/article/details/46913471 查看 nodemanager 日志发下 如下信息 2015-07-16 1 ...
 - Hibernate4 占位符(?)
			
Hibernate3使用?占位符: Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); ...
 - (转)Tomcat内存设置详解
			
Java内存溢出详解 一.常见的Java内存溢出有以下三种: 1. java.lang.OutOfMemoryError: Java heap space ----JVM Heap(堆)溢出JVM在启 ...
 - 用sql server的sql语句算一个empty GUID
			
在C#中得到一个empty GUID的方法是: Guid id= Guid.Empty; 那么在SQL Server Management Studio中怎样得到一个empty GUID呢? 方法有两 ...
 - Springmvc默认首页的问题
			
之前自己写的springmvc 默认首页都是偷懒方式: web.xml 中定义的默认首页: <welcome-file-list> <welcome-file>index.ht ...
 - easyui easyui-filebox 显示中文
			
<input class="easyui-filebox" name="uploadFile" id="uploadFileid" d ...
 - frame、bounds表示大小和位置的属性以及center、position、anchorPosition
			
在iOS开发开发过程中经常会用到界面元素的frame.bounds表示大小和位置的属性以及center.position.anchorPosition等单纯表示位置的属性.这些属性究竟什么含义?彼此间 ...
 - dhcpv6开源软件配置
			
ISC-dhcp server for IPv6 1. 下载源码:http://www.isc.org/software/dhcp 2.安装:最好以root身份安装,否则会permission de ...
 - java 接口的回调
			
Example6_3.java interface ShowMessage { void 显示商标(String s); } class TV implements ShowMessage { pub ...
 - Linux学习 -- 系统管理
			
1 进程管理 判断服务器健康状态 top [选项] 查看系统中所有进程 ps aux BSD格式 ps -le Linux格式 pstree [选项] -p 显示PID - ...