问题描述
G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。
输入格式
输入的第一行包含两个整数n, m,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到n编号,首都为1号。
接下来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
样例输出
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 求最短路径,在求得最短路径的前提下,还要保证选取的边最小,大家想一个问题,如果在求某个点到首都的最短路径。假入我们要求顶点5到顶点1的最短路径,如果1->3->5 和 1->2>5都是1到5的最短路径,那么我们是不是只要选择3->5和2->5的最短的一条边作为dijkstra算法中dijkstra顶点5的连接边,因为在求顶点5的花费时,1->3和1->2的花费都是已经确定的。所以只要选择2->5和3->5中最短的一条就能使整体的花费最小?
下面的算法几乎就是Dijkstra的模板,Dijkstra的详细算法我就不讲了。这个题只是多加了一个cost数组保存到达这个点的费用(不是总的费用,而是从哪个点到达这个点)
在未使用优先队列优化前,能得80分,下面是代码:
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;
public class D {
static ArrayList<Edge>[] node;
static int INF = 0x3f3f3f3f;
static int pre[];
static int dist[];
static boolean vis[];
static int verNum;
static int cost[];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
verNum = n;
node = new ArrayList[n + 1];
dist = new int[n + 1];
pre = new int[n + 1];
vis = new boolean[n + 1];
cost = new int[n + 1];
for (int i = 0; i < node.length; i++) {
node[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
int weight = sc.nextInt();
node[from].add(new Edge(to, weight));
node[to].add(new Edge(from, weight));
}
solve();
int result = 0;
for (int i = 1; i < cost.length; i++) {
result += cost[i];
}
System.out.println(result);
}
private static void solve() {
for (int i = 0; i < dist.length; i++) {
dist[i] = INF;
}
for (int i = 0; i < vis.length; i++) {
vis[i] = false;
cost[i] = INF;
}
pre[1] = 1;
dist[1] = 0;
cost[1] = 0;
for (int i = 0; i < verNum; i++) {
int tmpDist = INF;
int tmpVer = 0;
for (int j = 1; j <= verNum; j++) {
if (!vis[j] && dist[j] < tmpDist) {
tmpDist = dist[j];
tmpVer = j;
}
}
if (tmpVer == 0) {
break;
}
vis[tmpVer] = true;
for (int j = 0; j < node[tmpVer].size(); j++) {
int to = node[tmpVer].get(j).to;
int weight = node[tmpVer].get(j).weight;
if (!vis[to] && dist[tmpVer] + weight < dist[to]) {
dist[to] = dist[tmpVer] + weight;
cost[to] = weight;
} else if (!vis[to] && dist[tmpVer] + weight == dist[to] && weight < cost[to]) {
cost[to] = weight;
pre[to] = j;
}
}
}
}
}
class Edge {
int to;
int weight;
public Edge(int to, int weight) {
super();
this.to = to;
this.weight = weight;
}
}
使用优先队列优化后,就是100分,切记使用优先队列必须重写hashCode(),equals()方法,并且实现Comparable接口:
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;
public class D {
static ArrayList<Edge>[] node;
static int INF = 0x3f3f3f3f;
static int pre[];
static int dist[];
static boolean vis[];
static int verNum;
static int cost[];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
verNum = n;
node = new ArrayList[n + 1];
dist = new int[n + 1];
pre = new int[n + 1];
vis = new boolean[n + 1];
cost = new int[n + 1];
for (int i = 0; i < node.length; i++) {
node[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
int weight = sc.nextInt();
node[from].add(new Edge(to, weight));
node[to].add(new Edge(from, weight));
}
solve();
int result = 0;
for (int i = 1; i < cost.length; i++) {
result += cost[i];
}
System.out.println(result);
}
private static void solve() {
for (int i = 0; i < dist.length; i++) {
dist[i] = INF;
}
for (int i = 0; i < vis.length; i++) {
vis[i] = false;
cost[i] = INF;
}
pre[1] = 1;
dist[1] = 0;
cost[1] = 0;
PriorityQueue<Dist> queue = new PriorityQueue<>();
queue.add(new Dist(1, 0));
while (!queue.isEmpty()) {
int tmpDist = queue.peek().value;
int tmpVer = queue.poll().vertex;
vis[tmpVer] = true;
for (int j = 0; j < node[tmpVer].size(); j++) {
int to = node[tmpVer].get(j).to;
int weight = node[tmpVer].get(j).weight;
if (!vis[to] && dist[tmpVer] + weight < dist[to]) {
dist[to] = dist[tmpVer] + weight;
cost[to] = weight;
queue.add(new Dist(to, dist[to]));
} else if (!vis[to] && dist[tmpVer] + weight == dist[to] && weight < cost[to]) {
cost[to] = weight;
pre[to] = j;
}
}
}
}
}
class Dist implements Comparable<Dist> {
public int vertex;
public int value;
public Dist(int vertex, int value) {
super();
this.vertex = vertex;
this.value = value;
}
@Override
public boolean equals(Object arg0) {
// TODO Auto-generated method stub
return vertex == ((Dist) arg0).vertex;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return vertex;
}
@Override
public int compareTo(Dist o) {
// TODO Auto-generated method stub
return value - o.value;
}
}
class Edge {
int to;
int weight;
public Edge(int to, int weight) {
super();
this.to = to;
this.weight = weight;
}
}