[pat 1003] Emergency
已過測試用例的java程式碼如下
利用了dfs的思想.對於每條路,深度優先搜尋其通往的道路.visited在遍歷中設定為true,在遍歷此節點過後設定為false
public class PAT1003 { private static int shortestPath = Integer.MAX_VALUE; private static int shortCount = 0; private static int maxAmount = 0; private static int destPos; private static int[] teamCountArray; private static int[][] roadLenArray; private static boolean[] visited; private static int cityCount; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); cityCount = scanner.nextInt(); int roadCount = scanner.nextInt(); int currentPos = scanner.nextInt(); destPos = scanner.nextInt(); teamCountArray = new int[cityCount]; visited = new boolean[cityCount]; for (int i = 0; i < cityCount; i++) { teamCountArray[i] = scanner.nextInt(); } roadLenArray = new int[cityCount][cityCount]; for (int i = 0; i < cityCount; i++) { for (int j = 0; j < cityCount; j++) { roadLenArray[i][j] = -1; } } for (int i = 0; i < roadCount; i++) { int src = scanner.nextInt(); int desc = scanner.nextInt(); int len = scanner.nextInt(); roadLenArray[src][desc] = len; roadLenArray[desc][src] = len; } for (int i = 0; i < cityCount; i++) { visited[i] = false; } visited[currentPos] = true; if(currentPos == destPos){ System.out.println("1" + " " + teamCountArray[currentPos]); }else{ dfs(currentPos, 0, teamCountArray[currentPos]); System.out.println(shortCount + " " + maxAmount); } } static void dfs(int start, int pathLen, int teamCount) { for (int i = 0; i < cityCount; i++) { if (roadLenArray[start][i] != -1) { if (i == destPos) { int newPathLen = pathLen + roadLenArray[start][i]; int newTeamCount = teamCount + teamCountArray[i]; if (newPathLen < shortestPath) { maxAmount = newTeamCount; shortCount = 1; shortestPath = newPathLen; } else if (newPathLen == shortestPath) { maxAmount = maxAmount > newTeamCount ? maxAmount : newTeamCount; shortCount++; } visited[i] = false; continue; } if (!visited[i]) { //未遍歷過 if (pathLen + roadLenArray[start][i] <= shortestPath) { visited[i] = true; dfs(i, pathLen + roadLenArray[start][i], teamCount + teamCountArray[i]); visited[i] = false; } } } } } }
相關推薦
PAT 1003 Emergency
PAT 1003 emergency 學習djstra演算法 虛擬碼: 設d[0]=0, 其他d[i]=INF 迴圈n次{ 在所有為標號結點中選出d值最小結點x 給結點x標記 對於x出發的所有邊(x,y), 更新d[y]=min{d[y], d[x]+w
PAT 1003. Emergency (25) (求兩點間最短路的條數)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads
[pat 1003] Emergency
已過測試用例的java程式碼如下 利用了dfs的思想.對於每條路,深度優先搜尋其通往的道路.visited在遍歷中設定為true,在遍歷此節點過後設定為false public class PAT1003 { private static int shorte
PAT 甲級 1003. Emergency (25)
cat stand 題意 current different string imu hand ber 1003. Emergency (25) 時間限制 400 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard
PAT (Advanced Level) Practice 1003 Emergency (25 分) 單源最短路變式
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some ro
PAT (Advanced Level) 1003 Emergency (25 分)
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scatter
PAT甲級1003 Emergency (25)
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several sc
PAT甲級 1003.Emergency(25) 題目翻譯與答案
題目來源自PAT網站 https://www.patest.cn/ 題目描述: 1003. Emergency (25) As an emergencyrescue team leader of a city, you are given a special map o
圖論 | PAT-A | 1003 Emergency
文章目錄 不使用鏈式前向星,使用鄰接矩陣 使用鏈式前向星, 不堆優化 鏈式前向星+堆優化 不使用鏈式前向星,使用鄰接矩陣 時間:4 ms #include <bits/stdc++.h> #define bug(x)
PAT甲級真題(迪傑斯特拉演算法)——1003. Emergency (25)
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scatter
【學習筆記】〖PAT〗1003. Emergency (25)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads
【笨方法學PAT】1003 Emergency(25 分)
一、題目 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities co
1003. Emergency (25)
cit nec emerge rescue poi -1 name ever max As an emergency rescue team leader of a city, you are given a special map of your country. T
PAT——1003. 我要通過!
ron 正則表達 test 現在 gre 空字符串 spa get 測試 原題目鏈接:我要通過 “答案正確”是自動判題系統給出的最令人歡喜的回復。本題屬於PAT的“答案正確”大派送 —— 只要讀入的字符串滿足下列條件,系統就輸出“答案正確”,否則輸出“答案錯誤”。 得到
PAT 1003.我要通過!
clas names 接下來 close != 條件 alt 裁判 iostream 1003. 我要通過!(20) “答案正確”是自動判題系統給出的最令人歡喜的回復。本題屬於PAT的“答案正確”大派送 —— 只要讀入的字符串滿足下列條件,系統就輸出“答案正確”,否則輸
PAT 1003
any dfs follow ios pac quic numbers esc line 1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special m
1003 Emergency
ams distance mic oid std rst utility ret else 1003 Emergency 題目鏈接:https://pintia.cn/problem-sets/994805342720868352/problems/994805523835
pat 1003 我要通過!(20)
裁判 pro 組成 title brush true get char s hat 1003 我要通過!(20)(20 分) “答案正確”是自動判題系統給出的最令人歡喜的回復。本題屬於PAT的“答案正確”大派送 —— 只要讀入的字符串滿足下列條件,系統就輸出“答案正確
1003 Emergency(25 分)C語言版本(提問求解答)
paths sub amount ble max lis sam ams marked 1003 Emergency(25 分) As an emergency rescue team leader of a city, you are given a special ma
1003 Emergency (25 分)--- Dijkstra求解
題目地址: 1003 Emergency (25 分) 本題本質就是求起點到目標點最短路徑 #include <bits/stdc++.h> using namespace std; int n, m, c1, c2; //n為城市數,m為道路數,c1為起點,c2為目標