PAT L2-001緊急救援(java)

來源:互聯網
上載者:User

 
import java.util.Scanner;/** * dijkstra 經典應用變式 * L2-001. 緊急救援 * @author panda * */public class Main {static int point_num;//點數   MAXINT 10000static int arc_num;//邊數static int G[][];//無向圖static int firstaid[];//每個城市的救援隊數static int start_point;//出發城市static int end_point;//到達城市public static void main(String[] args){Scanner scanner =new Scanner(System.in);point_num=scanner.nextInt();arc_num=scanner.nextInt();start_point=scanner.nextInt();end_point=scanner.nextInt();G=new int [point_num][point_num];firstaid=new int [point_num];for (int i = 0; i < firstaid.length; i++) {//初始化firstaid[i]=scanner.nextInt();}for (int i = 0; i < point_num; i++) {//注意主對角線上的值為0,其他的為maxintfor (int j = 0; j < point_num; j++) {if(i==j) G[i][j]=0;else G[i][j]=10000;}}for (int i = 0; i < arc_num; i++) {//注意無向圖雙向都要賦值int a=0,b=0,c=0;a=scanner.nextInt();b=scanner.nextInt();c=scanner.nextInt();G[a][b]=c;G[b][a]=c;}shortpath(start_point);}public static void shortpath(int v0) {int pre[]=new int [point_num];//每個點的前驅節點int aidall[]=new int [point_num];//起點到每個節點的救援隊總數int dis_v0[]=new int [point_num];//到起點的距離int road_num[]=new int [point_num];//到每個點的最短路徑數boolean vis[]=new boolean[point_num];//此點是否訪問for (int i = 0; i < point_num; i++) {//初始化以上變數pre[i]= -1;//先將各點的前驅置為-1dis_v0[i]=10000;//到原點的距離置為 maxintaidall[i]=0;//總救援隊設為0vis[i]=false;road_num[i]=0;//總路徑數設為0}aidall[v0]=firstaid[v0];//現將起點的救援隊寫入vis[v0]=true;dis_v0[v0]=0;//原點自身到自身距離為0road_num[v0]=1;for (int i = 0; i < point_num; i++) {int yuan_point=v0;for (int j = 0; j < vis.length; j++) {if(!vis[j]&&10000>dis_v0[j]){//尋找已經被前面已經訪問過的點的後繼點yuan_point=j;//作為起點(原點)}}vis[yuan_point]=true;//設為已訪問for (int j = 0; j < point_num; j++) {if(!vis[j]){if(dis_v0[j]>dis_v0[yuan_point]+G[yuan_point][j]){//如果後繼點原先到v0點的距離大於當前點到原點的距離加上此點到後繼點的距離dis_v0[j]=dis_v0[yuan_point]+G[yuan_point][j];//更新後繼點距離aidall[j]=aidall[yuan_point]+firstaid[j];//計算出到達後繼點時所召集的救援隊pre[j]=yuan_point;//設定後繼點的最優前驅點為當前點road_num[j]=road_num[yuan_point];//將前驅點的路徑數賦給當前點}else if (dis_v0[j]==dis_v0[yuan_point]+G[yuan_point][j]) {//如果路徑長相等road_num[j]+=road_num[yuan_point];//當前點路徑數加上剛搜尋到的距離相同的點的路徑數if(aidall[j]<aidall[yuan_point]+firstaid[j]){//如果救援隊更多aidall[j]=aidall[yuan_point]+firstaid[j];//更新救援隊數pre[j]=yuan_point;//同時將前驅點設為剛搜尋到的點}}}}}System.out.print(road_num[end_point]+" "+aidall[end_point]+"\n");dispath(pre,end_point);System.out.print(end_point);}public static void dispath(int a[],int v0) {if(a[v0]!= -1){dispath(a, a[v0]);System.out.print(a[v0]+" ");//通過前驅點記錄數組,進行遞迴顯示路徑數}}}

參考部落格:http://blog.csdn.net/strokess/article/details/51339933


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.