ccf java高速公路_【CCF CSP】 20171203 行车路线 Java(有问题)80分

importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Iterator;importjava.util.LinkedList;importjava.util.List;importjava.util.Scanner;public classMain {private static intn;private final static int MAX =Integer.MAX_VALUE;private static boolean[] finalVex;private static int[] shortPath;private static List>list;public static voidshortPathDij() {

Edge tmp= null;

shortPath= new int[n];int[] tails = new int[n];int[] exp = new int[n];

finalVex= new boolean[n];

Arrays.fill(shortPath, MAX);

Arrays.fill(finalVex,false);

Arrays.fill(exp,0);

shortPath[0] = 0;

tails[0] = 1;while(!finalVex[n-1]) {int index =min(shortPath);if(index == -1)break;

LinkedList p =list.get(index);

Iterator it =p.iterator();int j=0;while(it.hasNext()) {

tmp=it.next();

j=tmp.end;if(finalVex[j])continue;if(tmp.type==1) {int eee = exp[index]+tmp.weight;int sum = shortPath[index]-(int)Math.pow(exp[index], 2)+(int)Math.pow(eee, 2);if(sum

shortPath[j]=sum;

tails[j]= index+1;

exp[j]=eee;

}

}else{if((shortPath[index]+tmp.weight)

shortPath[j]= shortPath[index]+tmp.weight;

tails[j]= index+1;

exp[j]= 0;

}

}

}

}

}private static int min(int[] arr) {int index = -1;for(int i=0; i

index=i;if(index==-1)return -1;for(int i=0; iarr[i]&&!finalVex[i])

index=i;

finalVex[index]= true;returnindex;

}public static voidmain(String[] args) {

Scanner in= newScanner(System.in);

n=in.nextInt();int nums =in.nextInt();

list= new ArrayList<>(n);for(int i=0; i

list.add(new LinkedList());

}for(int i=0; i

list.get(start-1).add(new Edge(type, start-1, end-1, weight));

list.get(end-1).add(new Edge(type, end-1, start-1, weight));

}

shortPathDij();

System.out.println(shortPath[n-1]);

in.close();

}

}classEdge{public inttype;public intstart;public intend;public intweight;public Edge(int type, int start, int end, intweight) {this.type =type;this.start =start;this.end =end;this.weight =weight;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值