ccf认证交通规划0分
来源:互联网 发布:centos安装glibc2.17 编辑:程序博客网 时间:2024/05/03 01:22
试题编号:201609-4试题名称:交通规划时间限制:1.0s内存限制:256.0MB问题描述:
问题描述
G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。
建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。
输入格式
输入的第一行包含两个整数n, m,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到n编号,首都为1号。
接下来m行,每行三个整数a, b, c,表示城市a和城市b之间有一条长度为c的双向铁路。这条铁路不会经过a和b以外的城市。
接下来m行,每行三个整数a, b, c,表示城市a和城市b之间有一条长度为c的双向铁路。这条铁路不会经过a和b以外的城市。
输出格式
输出一行,表示在满足条件的情况下最少要改造的铁路长度。
样例输入
4 5
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2
样例输出
11
评测用例规模与约定
对于20%的评测用例,1 ≤ n ≤ 10,1 ≤ m ≤ 50;
对于50%的评测用例,1 ≤ n ≤ 100,1 ≤ m ≤ 5000;
对于80%的评测用例,1 ≤ n ≤ 1000,1 ≤ m ≤ 50000;
对于100%的评测用例,1 ≤ n ≤ 10000,1 ≤ m ≤ 100000,1 ≤ a, b ≤ n,1 ≤ c ≤ 1000。输入保证每个城市都可以通过铁路达到首都。
使用单源最短路径的dijkstra算法,边计算起点到各点的最短路径,边计算其中需要增加的铁路的最短长度,因为到最后遍历出来的边是最小路径,(prim算法和dijkstra算法的相似性),得出的结果任意两点都是可达的。对于50%的评测用例,1 ≤ n ≤ 100,1 ≤ m ≤ 5000;
对于80%的评测用例,1 ≤ n ≤ 1000,1 ≤ m ≤ 50000;
对于100%的评测用例,1 ≤ n ≤ 10000,1 ≤ m ≤ 100000,1 ≤ a, b ≤ n,1 ≤ c ≤ 1000。输入保证每个城市都可以通过铁路达到首都。
import java.util.ArrayDeque;import java.util.ArrayList;import java.util.List;import java.util.Queue;import java.util.Scanner;public class Main1 {static int INT_MAX=10000;public static void main(String[] args) {Scanner scanner=new Scanner(System.in);int v=scanner.nextInt();int e=scanner.nextInt();Vex[] vex=new Vex[v+1];for (int i = 1; i <= v; i++) {vex[i]=new Vex();}for (int i = 0; i < e; i++) {int from=scanner.nextInt();int to=scanner.nextInt();int cost=scanner.nextInt();Edge edge=new Edge(to, cost);vex[from].edges.add(edge);edge=new Edge(from, cost);vex[to].edges.add(edge);}int cost=dijkstra(1,vex,v);System.out.println(cost);}public static int dijkstra(int start,Vex[] vex,int v){int lowcost=0;int[] visited=new int[v+1];int[] dist=new int[v+1];int[] cost=new int[v+1];for (int i = 1; i <=v; i++) {dist[i]=INT_MAX;cost[i]=INT_MAX;}dist[start]=0;cost[start]=0;Queue<Integer> queue=new ArrayDeque<>();queue.add(start);while (!queue.isEmpty()) {int node=queue.poll();while (visited[node]!=1) {visited[node]=1;for (Edge edge : vex[node].edges) {int to=edge.to;if (visited[to]==1) {continue;}int tempcost=edge.cost;int tempdist=dist[node]+tempcost;if (tempdist<dist[to]) {dist[to]=tempdist;cost[to]=tempcost;queue.add(to);}else if (tempdist==dist[to]) {if (tempcost<cost[to]) {cost[to]=tempcost;}}}}}for (int i = 2; i <=v; i++) {lowcost=lowcost+cost[i];}return lowcost;}}class Vex{List<Edge> edges;public Vex(){edges=new ArrayList<Edge>();}}class Edge{int to;int cost;public Edge(int t,int c){to=t;cost=c;}}
阅读全文
0 0
- ccf认证交通规划0分
- CCF认证 2016-09 交通规划
- 交通规划0分
- CCF-CSP 交通规划 JAVA 2016-09-04 100分
- CCF-交通规划
- CCF交通规划
- CCF 交通规划
- ccf 交通规划
- ccf交通规划
- CCF交通规划
- CCF 交通规划
- ccf 交通规划
- CCF-20160904-交通规划
- ccf 交通规划
- CCF 201612-4 交通规划
- CCF 201609-4 交通规划
- CCF 201612-4 交通规划
- 第八次ccf-交通规划
- 贪心算法
- SpringMVC框架页面和controller之间日期类型转换解决方案
- 经过一个月的努力,专业技术继续教育学习代听课系统开发完毕!
- Baffle.js – 用于实现文本模糊效果的 JavaScript 库
- 第一周、课时15:综合案例——选项卡
- ccf认证交通规划0分
- AD使用过程—新建工程
- C#基础教程(全部)
- 边缘计算如何层次化部署
- 棋盘上放棋子(任意两个棋子不相邻)
- LintCode 关于递归问题的总结
- 统计学习方法笔记:K近邻法
- MySql安装和配置详情
- IDEA之Maven搭建Spring4 SpringMVC4 Mybatis3(笔记记录篇)