Small family fled the Rye
Title Description
Master lived in a village, an avalanche day Master running speed of 13m / S , using magic 1s move 50m , but will deduct 10 mana
Master initial mana M , village distance safely is S , avalanche reach time T , the Master recovery magic speed 4:00 / S , but only stagnant can recover
Entry
Input line, comprising three non-negative integers M, S, T
Export
Two output lines
of the first line of output "Yes", "No", indicates whether the safe arrival
or non-arrival of the second row reaches the output time of arrival of the maximum distance
Sample input
36 255 10
Sample Output
Yes
10
Thinking
Two kinds of ways to escape, running and using magic, directly greed can
- Under sufficient magic value, may be used M / 10 times for escape magic
- Calculation available, when the remaining time mana> = 2 , wait for recovery once the magic will have an advantage over the direct running
Note : Of course, you need to focus on time is sufficient , as well as your distance safely , you have to rest a 13m no need to wait to use a magic time - Mana is used up, the remaining time is of course a direct run. . .
coding
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int m = scn.nextInt(), s = scn.nextInt(), t = scn.nextInt();
// count:初始魔法值能用几次 tAns:记录使用的时间 sAns:记录走过的路程 flag:此处标记剩余魔法值是否为 4 的倍数
int count = m/10, sAns = 0, tAns = 0, tt = 0, flag = (10-m%10)%10%4 == 0 ? 0 : 1;
if(m%10 > 1 && m%10/4 + flag <= t) { // tt记录下恢复到至少 10 点魔法值所需时间
tt = m%10 / 4 + flag;
}
flag = s%50 == 0 ? 0 : 1; // 此处标记路程是否为 50 的倍数
count = Math.min(count, Math.min(s/50+flag,t));
sAns += count * 50;
tAns += count;
if(tt != 0 && tt+1 <= t-tAns && tt*13 + sAns < s) { // 存在多余魔法值 未被淹没 使用等待的时间直接走不会到达目的地
sAns += 50;
tAns += tt + 1;
}
flag = (s-sAns)%13 == 0 ? 0 : 1; // 此处标记剩余路程为 13 的倍数
count = Math.min((s-sAns)/13+flag, (t-tAns));
sAns += count * 13;
tAns += count;
if(sAns < s) {
System.out.println("No");
System.out.println(sAns);
} else {
System.out.println("Yes");
System.out.println(tAns);
}
}
}
Small family itinerary
Title Description
There are cities, M N Block route, which now want to go to Marco Polo R cities, please help him develop a shortest route .
Course for undirected edges to ensure no heavy side
Entry
The first input line N, M, R (2 < = N <= 200,1 <= M <= 500,2 <= R <= min (22, N)
a second input line number R cities
M rows after per line x, y, z represents the distance between the cities and urban y z x m (z <= 10000)
Export
The shortest distance route
Sample input
4 6 3
2 3 4
1 2 4
2 3 3
4 3 1
1 4 1
4 2 2
3 1 6
Sample Output
3
Thinking
Search traverse to
the starting point must be the need to go to the city
condition returns all need to go to the city have been to
be cut by comparing the path with ans (shortest path search history appear) some of the long routes
coding
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
private static int[][] map; // 邻接矩阵记录路线
private static int[] vis; // 记录是否去过某城市
private static Set<Integer> set; // 记录是否去过需要去的城市
private static int n, ans = Integer.MAX_VALUE; // ans 记录最短路径
private static int dfs(int x, int s) {
if(s > ans) return Integer.MAX_VALUE; // 超过之前的最优路径,没必要继续走
if(set.isEmpty()) return s; // 需要去的城市都去过,即找到路线
int tmp = Integer.MAX_VALUE; // 记录到达 x 之后, 后面的最短路线
for(int i = 1; i <= n; ++i) {
if(map[x][i] > 0 && vis[i] == 0) {
boolean flag = set.remove(i);
vis[i] = 1;
tmp = Math.min(dfs(i, s+map[x][i]), tmp);
vis[i] = 0;
if(flag) set.add(i);
}
}
return tmp;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
n = scn.nextInt();
int m = scn.nextInt(), r = scn.nextInt();
map = new int[n+1][n+1];
vis = new int[n+1];
set = new HashSet<Integer>();
for(int i = 0; i < r; ++i) {
set.add(scn.nextInt());
}
for(int i = 0; i < m; ++i) {
int x = scn.nextInt(), y = scn.nextInt(), z = scn.nextInt();
map[x][y] = map[y][x] = z;
}
for(int i = 1; i < n; ++i) {
if(set.contains(i)) { // 只有需要去的城市才作为起点进行搜索
set.remove(i);
vis[i] = 1;
ans = Math.min(dfs(i, 0), ans);
vis[i] = 0;
set.add(i);
}
}
System.out.println(ans);
}
}