当前位置:网站首页>Gamer questions
Gamer questions
2022-07-27 10:37:00 【chengqiuming】
One Link to original question
XYZZY - HDU 1317 - Virtual Judgehttps://vjudge.net/problem/HDU-1317
Two Input and output
1 Input
The input contains several test cases . The... Of each test case 1 All lines are n, Indicates the number of rooms . Next is n Information about rooms . Each room information includes : room i Energy value of 、 Leave the room i The number of doors 、 room i List of rooms that can be reached through the door . After the last test case, it contains -1 The line of .
2 Output
If the player is likely to win , The output winnable, Otherwise output hopeless.
3、 ... and Input and output samples
1 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
2 Output
hopeless
hopeless
winnable
winnable
Four analysis
According to the input sample 1, The constructed diagram is shown in the following figure , Not to 5 The energy value of room No. 1 is exhausted , Output “hopeless”.

According to the input sample 4, The constructed diagram is shown in the following figure , It has a positive ring and can reach the end , Output “winnable”

5、 ... and Algorithm design
1 use Floyd Algorithm judges connectivity , Judge whether or not from 1 Go to room No n Room number , If not connected, it ends .
2 use SPFA The algorithm judges whether there is a positive ring , stay cnt[v]>=n Sometimes there is a positive ring , Judge whether the point on the ring is connected to the end . If there is no positive ring , Then judge whether the energy value of the longest path to the destination is greater than 0 that will do .
Be careful : The data given by this problem is the energy value of each node , Not the ability value of the edge , Need to use Floyd Algorithm judges connectivity , So we use adjacency matrix to store graph .
6、 ... and Code
package graph.hdu1317;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Hdu1317 {
static final int maxn = 105;
static int n;
static int energy[];
static int power[]; // Record the strength value of each point
static int cnt[]; // Record the number of visits to each point
static boolean g[][];
static boolean vis[];
static boolean reach[][];
// Judge connectivity
static void floyd() {
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (reach[i][j] || (reach[i][k] && reach[k][j]))
reach[i][j] = true;
}
static boolean spfa(int s) { // Determine the sum of positive rings and find the maximum energy value
Queue<Integer> q = new LinkedList<>();
power = new int[maxn];
vis = new boolean[maxn];
cnt = new int[maxn];
power[s] = 100;
q.add(s);
vis[s] = true;
cnt[s]++;
while (!q.isEmpty()) {
int v = q.peek();
q.poll();
vis[v] = false;
for (int i = 1; i <= n; i++) {
if (g[v][i] && power[i] < power[v] + energy[i] && power[v] + energy[i] > 0) {
power[i] = power[v] + energy[i];
if (!vis[i]) {
if (++cnt[i] >= n)// Positive ring
return reach[v][n]; // return v To n Is it connected
vis[i] = true;
q.add(i);
}
}
}
}
return power[n] > 0;
}
static void solve() {
int k, door;
while (true) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if (n == -1) {
return;
}
g = new boolean[maxn][maxn];
reach = new boolean[maxn][maxn];
energy = new int[maxn];
for (int i = 1; i <= n; i++) {
energy[i] = scanner.nextInt();
k = scanner.nextInt();
for (int j = 1; j <= k; j++) {
door = scanner.nextInt();
g[i][door] = true;
reach[i][door] = true;
}
}
floyd();
if (!reach[1][n]) {
System.out.println("hopeless");
continue;
}
if (spfa(1) == true)
System.out.println("winnable");
else
System.out.println("hopeless");
}
}
public static void main(String[] args) {
solve();
}
}7、 ... and test
Green is the input , White is the output .

边栏推荐
- 解决ORCLE-ORA-01122 01110 01210
- 这种动态规划你见过吗——状态机动态规划之股票问题(上)
- 怎样关闭电脑开机自启动的应用
- Matlab- draw date and duration diagram
- Des/3des/aes differences
- 让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
- 让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
- Share machine learning notes (PDF version) + practical projects (dataset + code)
- Oracle resizing data files
- Warning: remote head references to nonexistent ref, unable to checkout error messages
猜你喜欢

异构计算技术分析

Shardingproxy sub database and table actual combat and comparison of similar products

Establishment of NFS server
![[Linux] mariadb/mysql scheduled full backup script and data recovery](/img/02/8ee01336a46e4956738f3cc8e30683.png)
[Linux] mariadb/mysql scheduled full backup script and data recovery

让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...

The core concept and fast practice of shardingsphere

FTP server

hdu5288(OO’s Sequence)

Oracle resizing data files
[email protected] @Eslint loader / index. JS) "/>Eslint's error message module error (from./node_modules/ [email protected] @Eslint loader / index. JS)
随机推荐
Program translation and execution, from editing, preprocessing, compilation, assembly, linking to execution
【Liunx】MariaDB/MySQL定时全量备份脚本及数据恢复
【Liunx】安装Redis
一起学习C语言:结构体(二)
Sub query of database performance series
程序的翻译和执行,从编辑、预处理、编译、汇编、链接到执行
服务器访问速度
Matlab-绘制日期和持续时间图
简单几步教您实现为工业树莓派共享网络
【Liunx】安装MySQL
声音处理之-梅尔频率倒谱系数(MFCC)
Matlab绘制不同阻尼下的系统响应
Local connection to remote server database under Windows platform (I)
gyp ERR! configure error. gyp ERR! stack Error: gyp failed with exit code: 1
【英雄哥六月集训】第 23天: 字典树
Xiandai 003
Custom page 01 of JSP custom tag
[brother hero June training] day 23: dictionary tree
7z用法
warning package. Json: no license field error