GYM - 101502I - Move Between Numbers
题目大意
就是给你 n
, s
、 e
, n
代表的是有多少个字符串,下面每一个字符串可以看做是图上的一个顶点,如果两个字符串之间满足题目所给的那个条件,即: 两个字符串的相同的字符个数的数目 sum = 17
(可以这样理解,就是题目那个等式的意思),两个字符串(顶点) 就可以相互到达,问从 s
到 e
最少的步数 mindistance
。
解析
编写一个函数判断两个点是否可以相互到达,然后用 dijkstra
模板 跑一下即可。
import java.io.BufferedInputStream;
import java.util.*;
public class Main {
static int n; // vertex num
static int m; // edge num
static boolean[] vis;
static ArrayList<Edge>[] G;
static class Edge implements Comparable<Edge> {
public int to;
public int w;
public Edge(int to, int w) {
this.to = to;
this.w = w;
}
@Override
public int compareTo(Edge o) {
return w - o.w;
}
}
static boolean common(String a, String b) {
int[] ca = new int[10];
int[] cb = new int[10];
for (int i = 0; i < a.length(); i++) {
ca[a.charAt(i) - '0']++;
cb[b.charAt(i) - '0']++;
}
int sum = 0;
for (int i = 0; i < 10; i++)
sum += Math.min(ca[i], cb[i]);
return sum == 17;
}
static int[] dijkstra(int start) {
PriorityQueue<Edge> pq = new PriorityQueue<>();
int[] dis = new int[n + 1];
for (int i = 0; i <= n; i++)
dis[i] = Integer.MAX_VALUE;
dis[start] = 0;
// G.vis[str] = true; // 下面 Queue 还要访问 start
pq.add(new Edge(start, 0)); //自环边
while (!pq.isEmpty()) {
Edge curEdge = pq.poll();
int to = curEdge.to;
if (vis[to])
continue;
vis[to] = true;
for (int i = 0; i < G[to].size(); i++) {
int nxtNode = G[to].get(i).to;
int nxtW = G[to].get(i).w;
if (!vis[nxtNode] && dis[nxtNode] > dis[to] + nxtW) {
dis[nxtNode] = dis[to] + nxtW;
pq.add(new Edge(nxtNode, dis[nxtNode]));
}
}
}
return dis;
}
public static void main(String[] args) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
int T = cin.nextInt();
while (T-- > 0) {
n = cin.nextInt();
int s = cin.nextInt();
int e = cin.nextInt();
String[] str = new String[n];
for (int i = 0; i < n; i++)
str[i] = cin.next();
G = new ArrayList[n + 1];
vis = new boolean[n + 1];
for (int i = 0; i <= n; i++) {
G[i] = new ArrayList<>();
vis[i] = false;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (common(str[i], str[j])) {
G[i + 1].add(new Edge(j + 1, 1)); // notice i+1, j+1
G[j + 1].add(new Edge(i + 1, 1));
}
}
}
int[] dis = dijkstra(s);
System.out.println(dis[e] == Integer.MAX_VALUE ? -1 : dis[e]);
}
}
}
C++
代码:
#include <bits/stdc++.h>
const int maxn = 251;
const int INF = 1e9;
class Edge{
public:
int to, w;
Edge(int to, int w):to(to), w(w){}
// bool operator < (const Edge& rhs) const {
// return w > rhs.w;
// }
};
bool operator < (const Edge& ea, const Edge& eb){
return ea.w > eb.w;
}
std::vector<Edge>G[maxn];
bool vis[maxn];
int dis[maxn];
bool common(char *s1,char *s2){
int len = strlen(s1);
int a[10] = {0}, b[10] = {0};
for(int i = 0; i < len; i++)a[s1[i]-'0']++;
for(int i = 0; i < len; i++)b[s2[i]-'0']++;
int sum = 0;
for(int i = 0; i <= 9; i++)
sum += std::min(a[i], b[i]);
return sum == 17;
}
int dijkstra(int s, int e, int n){
// priority_queue<Type, Container, Functional>
// priority_queue 使用: https://blog.csdn.net/bat67/article/details/77585312
std::priority_queue<Edge>pq; //比较方式默认用 operator< ,所以俩个参数缺省的话,优先队列就是大顶堆,队头元素最大。
for(int i = 0; i < n; i++) dis[i] = INF;
dis[s] = 0;
pq.push(Edge(s, 0));
// vis[d] = true;
while(!pq.empty()){
Edge cur = pq.top(); pq.pop();
int to = cur.to;
if(vis[to])
continue;
vis[to] = true;
for(int i = 0; i < G[to].size(); i++){
int to2 = G[to][i].to;
int w = G[to][i].w;
if(!vis[to2] && dis[to2] > w+dis[to]){
dis[to2] = w+dis[to];
pq.push(Edge(to2, dis[to2]));
}
}
}
return dis[e];
}
int main(){
int T, n, m, s, e;
char str[250][25];
std::cin >> T;
while(T--){
std::cin >> n >> s >> e;
for(int i = 0; i < n; i++)
std::cin >> str[i];
for(int i = 0; i < n; i++) G[i].clear();
for(int i = 0; i < n; i++) vis[i] = false;
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
if(common(str[i], str[j])){
G[i].push_back(Edge(j,1));
G[j].push_back(Edge(i,1));
}
}
}
int res = dijkstra(s-1,e-1, n); // notice index
std::cout << (res == INF ? -1 : res) << std::endl;
}
return 0;
}
题目链接
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论