样例说明
所有机器人在 1 号节点着陆。
第一个机器人的行走路径为 1->6 ,在 6 号节点返回地球,花费能量为1000。
第二个机器人的行走路径为 1->2->3->2->4 ,在 4 号节点返回地球,花费能量为1003。
第一个机器人的行走路径为 1->2->5 ,在 5 号节点返回地球,花费能量为1001。
数据规模与约定
本题有10个测试点。
对于测试点 1~2 , n <= 10 , k <= 5 。
对于测试点 3 , n <= 100000 , k = 1 。
对于测试点 4 , n <= 1000 , k = 2 。
对于测试点 5~6 , n <= 1000 , k <= 10 。
对于测试点 7~10 , n <= 100000 , k <= 10 。
道路的能量 w 均为不超过 1000 的正整数。
参考代码:
import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList;
public class Main { private static class MyScanner { private InputStream is = System.in; public int nextInt() { try { int i; while ((i = is.read()) < 45 || i > 57) { }
}
}
int mark = 1, temp = 0; if (i == 45) { mark = -1; i = is.read(); }
while (i > 47 && i < 58) { temp = temp * 10 + i - 48; i = is.read(); }
return temp * mark; } catch (IOException e) { e.printStackTrace(); } return -1;
private static class Node { int[] dp; HashMap
public Node() { dp = new int[k + 1]; nodes = new HashMap<>(); }
private static int n, s, k, max = 10000; private static int[] minEmpty; private static Node[] nodeList;
private static void dfs(int i) { LinkedList
while (!stack.isEmpty()) { i = stack.pop(); Node root = nodeList[i];
for (Iterator
}
}
}
.hasNext();) { int j = it.next();
Node sub = nodeList[j]; sub.nodes.remove(i); if (!sub.nodes.isEmpty()) { stack.push(i); stack.push(j); break; }
int cost = root.nodes.get(j);
for (int num = k; num >= 0; --num) { root.dp[num] += sub.dp[0] + 2 * cost; }
for (int l = 1; l <= num; ++l) { root.dp[num] = Math.min(root.dp[num], root.dp[num - l] + l * cost + sub.dp[l]); }
it.remove();
public static void main(String[] args) { MyScanner sc = new MyScanner(); n = sc.nextInt(); s = sc.nextInt(); k = sc.nextInt(); nodeList = new Node[n + 1];
for (int i = 1; i <= n; ++i) { nodeList[i] = new Node(); }
for (int i = 1; i < n; ++i) { int x = sc.nextInt(), y = sc.nextInt(), w = sc.nextInt(); nodeList[x].nodes.put(y, w); nodeList[y].nodes.put(x, w); }
minEmpty = new int[k + 1]; for (int i = 1; i <= k; ++i) { minEmpty[i] = max; } dfs(s); System.out.println(nodeList[s].dp[k]); } }
编号: ADV-4 题目:道路和航路 关键字:最短路 类型:普通试题 问题描述:
农夫约翰正在针对一个新区域的牛奶配送合同进行研究。他打算分发牛奶到T个城镇(标号为1..T),这些城镇通过R条标号为(1..R)的道路和P条标号为(1..P)的航路相连。
每一条公路i或者航路i表示成连接城镇Ai(1<=A_i<=T)和Bi(1<=Bi<=T)代价为Ci。每一条公路,Ci的范围为0<=Ci<=10,000;由于奇怪的运营策略,每一条航路的Ci可能为负的,也就是-10,000<=Ci<=10,000。
每一条公路都是双向的,正向和反向的花费是一样的,都是非负的。
每一条航路都根据输入的Ai和Bi进行从Ai->Bi的单向通行。实际上,如果现在有一条航路是从Ai到Bi的话,那么意味着肯定没有通行方案从Bi回到Ai。
农夫约翰想把他那优良的牛奶从配送中心送到各个城镇,当然希望代价越小越好,你可以帮助他嘛?配送中心位于城镇S中(1<=S<=T)。
输入格式
输入的第一行包含四个用空格隔开的整数T,R,P,S。
接下来R行,描述公路信息,每行包含三个整数,分别表示Ai,Bi和Ci。
接下来P行,描述航路信息,每行包含三个整数,分别表示Ai,Bi和Ci。
输出格式
输出T行,分别表示从城镇S到每个城市的最小花费,如果到不了的话输出NO PATH。 样例输入 6 3 3 4 1 2 5 3 4 5
5 6 10 3 5 -100 4 6 -100 1 3 -10
样例输出 NO PATH NO PATH 5 0 -95 -100
数据规模与约定
对于20%的数据,T<=100,R<=500,P<=500;
对于30%的数据,R<=1000,R<=10000,P<=3000;
对于100%的数据,1<=T<=25000,1<=R<=50000,1<=P<=50000。 参考代码:
该题暂时没有人完全正确,暂时没有该语言的参考程序。
编号: ADV-5
题目:最小方差生成树 关键字:最小生成树 类型:普通试题 问题描述:
给定带权无向图,求出一颗方差最小的生成树。 输入格式
输入多组测试数据。第一行为N,M,依次是点数和边数。接下来M行,每行三个整数U,V,W,代表连接U,V的边,和权值W。保证图连通。n=m=0标志着测试文件的结束。 输出格式
对于每组数据,输出最小方差,四舍五入到0.01。输出格式按照样例。 样例输入 4 5 1 2 1 2 3 2 3 4 2 4 1 1 2 4 3 4 6 1 2 1 2 3 2 3 4 3 4 1 1