原题链接:蓝桥杯算法提高VIP-文化之旅
import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main { static Scanner scanner = new Scanner(System.in); static int n, k, m, s, t; // static int wenhua[]; static int paichi[][]; static int gra[][]; static LinkedList<Integer>[] wenhua; public static void main(String[] args) { n = scanner.nextInt(); k = scanner.nextInt(); m = scanner.nextInt(); s = scanner.nextInt(); t = scanner.nextInt(); gra = new int[n + 1][n + 1]; wenhua = new LinkedList[k + 1]; for (int i = 1; i <= k; i++) { wenhua[i] = new LinkedList(); } // wenhua = new int[n + 1]; paichi = new int[k + 1][k + 1]; // System.out.println("ddddddddddd"); // 依次代表国家个数(国家编号为1到N),文化种数(文化编号为1到K),道路的条数,以及起点和终点的编号(保证S不等于T); for (int i = 1; i <= n; i++) { int wen = scanner.nextInt(); wenhua[wen].add(i); // System.out.println(wen); } // 如果两个国家排斥的话,设琪距离为非常大就可以了 for (int i = 1; i <= n; i++) { Arrays.fill(gra[i], 1001); } for (int i = 1; i <= k; i++) { for (int j = 1; j <= k; j++) { paichi[i][j] = scanner.nextInt(); // System.out.println(paichi[i][j]); } } for (int i = 0; i < m; i++) { int u, v, w; u = scanner.nextInt(); v = scanner.nextInt(); w = scanner.nextInt(); gra[u][v] = w; gra[v][u] = w; // System.out.println(gra[u][v]); } for (int i = 1; i <= k; i++) { for (int j = 1; j <= k; j++) { // paichi[i][j] = scanner.nextInt(); if (paichi[i][j] == 1) { List<Integer> wenhua1 = wenhua[i]; List<Integer> wenhua2 = wenhua[j]; for (int a = 0; a < wenhua1.size(); a++) { for (int b = 0; b < wenhua2.size(); b++) { gra[wenhua2.get(b)][wenhua1.get(a)] = 1001; } } } } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { gra[i][j] = Math.min(gra[i][j], gra[i][k] + gra[k][j]); } } } System.out.println(gra[s][t] == 1001 ? -1 : gra[s][t]); } }
0.0分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复