您的位置:首页 > 编程语言 > Java开发

ccf 之交通规划 java版

2016-11-09 13:58 253 查看
<table class="probtable " align="center" width="100%" cellpadding="0" cellspacing="1" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); font-family: 宋体; font-size: 14px; text-align: center; background: rgb(192, 214, 221);"><tbody style="margin: 0px; padding: 0px;"><tr style="margin: 0px; padding: 0px;"><td style="margin: 0px; padding: 12px; text-align: left; background: rgb(255, 255, 255);">201612-4</td></tr><tr style="margin: 0px; padding: 0px;"><td class="probref" style="margin: 0px; padding: 12px; width: 100px; vertical-align: middle; color: rgb(0, 87, 164); background: rgb(248, 248, 248);">试题名称:</td><td style="margin: 0px; padding: 12px; text-align: left; background: rgb(255, 255, 255);">交通规划</td></tr><tr style="margin: 0px; padding: 0px;"><td class="probref" style="margin: 0px; padding: 12px; width: 100px; vertical-align: middle; color: rgb(0, 87, 164); background: rgb(248, 248, 248);">时间限制:</td><td style="margin: 0px; padding: 12px; text-align: left; background: rgb(255, 255, 255);">1.0s</td></tr><tr style="margin: 0px; padding: 0px;"><td class="probref" style="margin: 0px; padding: 12px; width: 100px; vertical-align: middle; color: rgb(0, 87, 164); background: rgb(248, 248, 248);">内存限制:</td><td style="margin: 0px; padding: 12px; text-align: left; background: rgb(255, 255, 255);">256.0MB</td></tr><tr style="margin: 0px; padding: 0px;"><td class="probref" style="margin: 0px; padding: 12px; width: 100px; vertical-align: middle; color: rgb(0, 87, 164); background: rgb(248, 248, 248);">问题描述:</td><td style="margin: 0px; padding: 12px; text-align: left; background: rgb(255, 255, 255);"><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold;">问题描述</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px;">  G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。<br style="margin: 0px; padding: 0px;" />  建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold;">输入格式</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px;">  输入的第一行包含两个整数n, m,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到n编号,首都为1号。<br style="margin: 0px; padding: 0px;" />  接下来m行,每行三个整数a, b, c,表示城市a和城市b之间有一条长度为c的双向铁路。这条铁路不会经过a和b以外的城市。</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold;">输出格式</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px;">  输出一行,表示在满足条件的情况下最少要改造的铁路长度。</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold;">样例输入</div><div class="pddata" style="margin: 0px 0px 0px 32px; padding: 0px; font-family: 'Courier New'; font-size: 16px;">4 5<br style="margin: 0px; padding: 0px;" />1 2 4<br style="margin: 0px; padding: 0px;" />1 3 5<br style="margin: 0px; padding: 0px;" />2 3 2<br style="margin: 0px; padding: 0px;" />2 4 3<br style="margin: 0px; padding: 0px;" />3 4 2</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold;">样例输出</div><div class="pddata" style="margin: 0px 0px 0px 32px; padding: 0px; font-family: 'Courier New'; font-size: 16px;">11</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold;">评测用例规模与约定</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px;">  对于20%的评测用例,1 ≤ n ≤ 10,1 ≤ m ≤ 50;<br style="margin: 0px; padding: 0px;" />  对于50%的评测用例,1 ≤ n ≤ 100,1 ≤ m ≤ 5000;<br style="margin: 0px; padding: 0px;" />  对于80%的评测用例,1 ≤ n ≤ 1000,1 ≤ m ≤ 50000;<br style="margin: 0px; padding: 0px;" />  对于100%的评测用例,1 ≤ n ≤ 10000,1 ≤ m ≤ 100000,1 ≤ a, b ≤ n,1 ≤ c ≤ 1000。输入保证每个城市都可以通过铁路达到首都。</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px;">
</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px;">
</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px;">分析:此题用到单源最短路径,并记录确认最短路径前一位置的城市。将路线长度存为二维数组,建立open和close集合,不断添加新城市,确认每个新城市的最短路径。因为应题意要求,要建尽可能少的铁路,所以在比较时要注意两条路径路程相等,但最后一段路不同的情况加以改进。最后只要将数组中的 djs【最短路径的前一城市】【目的城市】累加即可得出结论。</div></td></tr></tbody></table>import java.util.HashSet;
import java.util.Scanner;

public class Main {
public static void main(String[] args)
{
new Main().run();

}
public void run ()
{
HashSet<Integer> close=new HashSet<Integer>();
HashSet<Integer> open=new HashSet<Integer>();
close.add(0);
Scanner s=new Scanner(System.in);
int citynum=s.nextInt();
int roadnum=s.nextInt();
for (int i = 1; i <citynum ; i++) {
open.add(i);
}
//init
int[][] djs=new int[citynum][citynum];
for (int i = 0; i < citynum; i++) {
for (int j = 0; j < citynum; j++) {
if(i==j)
{djs[i][j]=0;}
else
djs[i][j]=10000;
}
}
int[][] minroad=new int[citynum][2];
for (int i = 0; i <roadnum ; i++) {
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
djs[a-1][b-1]=djs[b-1][a-1]=c;
}
while(!open.isEmpty())
{
int roadlong = 100000000;
int targetcity = 0;
int passcity=0;
for (int start:close)
{
for (int target:open) {

if(djs[start][target]+djs[0][start]<roadlong||((djs[start][target]+djs[0][start]==roadlong)&&(djs[start][target]<djs[passcity][targetcity])))
{
passcity=start;
targetcity=target;
roadlong=djs[start][target]+djs[0][start];
}
}
}
open.remove(targetcity);
close.add(targetcity);
minroad[targetcity][0]=passcity;
minroad[targetcity][1]=roadlong;

}
int min=0;
for (int i = 1; i <citynum; i++) {
min+=djs[minroad[i][0]][i];
}
System.out.println(min);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java ccf 交通规划