题目链接

题意:给定一个无向图和一个点u,找出若干条边组成一个子图,要求这个子图中u到其他个点的最短距离与在原图中的相等,并且要求子图所有边的权重之和最小,求出最小值并输出子图的边号。

思路:先求一遍最短路,从所有到i点的满足最短路的边中选一条权最小的边。

Java程序

  1. import java.io.PrintStream;
  2. import java.util.ArrayList;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Queue;
  6. import java.util.Scanner;
  7.  
  8. public class E545 {
  9. private static class Edge {
  10. int v;
  11. long w;
  12. int index;
  13.  
  14. Edge(int v, long w, int index) {
  15. this.v = v;
  16. this.w = w;
  17. this.index = index;
  18. }
  19. }
  20.  
  21. public static void main(String[] args) {
  22. Scanner in = new Scanner(System.in);
  23. PrintStream out = System.out;
  24.  
  25. int n = in.nextInt(), m = in.nextInt();
  26. List<Edge>[] graph = new List[n];
  27.  
  28. for (int i = 0; i < n; i++) {
  29. graph[i] = new ArrayList<E545.Edge>();
  30. }
  31.  
  32. for (int i = 1; i <= m; i++) {
  33. int v1 = in.nextInt() - 1;
  34. int v2 = in.nextInt() - 1;
  35. long w = in.nextLong();
  36.  
  37. graph[v1].add(new Edge(v2, w, i));
  38. graph[v2].add(new Edge(v1, w, i));
  39. }
  40. int u = in.nextInt() - 1;
  41.  
  42. Edge[] lastEdge = new Edge[n];
  43. final long[] min = new long[n];
  44. for (int i = 0; i < n; i++) {
  45. min[i] = -1;
  46. }
  47.  
  48. min[u] = 0;
  49. Queue<Integer> q = new LinkedList<Integer>();
  50.  
  51. q.add(u);
  52.  
  53. while (!q.isEmpty()) {
  54. int v = q.poll();
  55.  
  56. for (Edge edge : graph[v]) {
  57. int v1 = edge.v;
  58. long min1 = min[v] + edge.w;
  59.  
  60. if ((min[v1] == -1) || (min1 < min[v1])
  61. || (min1 == min[v1] && edge.w < lastEdge[v1].w)) {
  62.  
  63. min[v1] = min1;
  64. lastEdge[v1] = edge;
  65. q.add(v1);
  66. }
  67. }
  68. }
  69.  
  70. long res = 0;
  71. boolean[] f = new boolean[m];
  72.  
  73. for (int i = 0; i < n; i++) {
  74. if (lastEdge[i] != null) {
  75. res += lastEdge[i].w;
  76. f[lastEdge[i].index - 1] = true;
  77. }
  78. }
  79.  
  80. out.println(res);
  81.  
  82. StringBuilder s = new StringBuilder();
  83. boolean first = true;
  84. for (int i = 0; i < m; i++) {
  85. if (f[i]) {
  86. if (!first) {
  87. s.append(" ");
  88. }
  89. s.append(i + 1);
  90. first = false;
  91. }
  92. }
  93. out.println(s.toString());
  94. in.close();
  95. out.close();
  96.  
  97. }
  98.  
  99. }

 

Python代码

  1. import heapq as hq
  2.  
  3. class edge(object):
  4. def __init__(self, to, w, nr):
  5. self.to = to
  6. self.w = w
  7. self.nr = nr
  8.  
  9. n, m = map(int, raw_input().split())
  10. adj = [[] for _ in range(n + 1)]
  11. for i in range(1, m+1):
  12. u, v, c = map(int, raw_input().split())
  13. adj[u].append((v, c, i))
  14. adj[v].append((u, c, i))
  15. root = int(raw_input())
  16. vis = [False] * (n+1)
  17. q = [(0, 0, root, 0)]
  18. ans = []
  19. tot = 0
  20. while q:
  21. d, c, n, e = hq.heappop(q)
  22. if vis[n]:
  23. continue
  24. ans.append(e)
  25. tot += c
  26. vis[n] = True
  27. for v, c, i in adj[n]:
  28. if not vis[v]:
  29. hq.heappush(q, (d+c, c, v, i))
  30. ans = map(str, ans)
  31. print tot
  32. print " ".join(ans[1:])

 

上面的代码都是在codeforces上面抄过来的,自己写不出来。。。。

545E. Paths and Trees的更多相关文章

  1. Codeforces 545E. Paths and Trees 最短路

    E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...

  2. CF 545E Paths and Trees

    题目大意:给出n个点,m条无向边,每条边有长度.求一棵树,要求树上的每个点到源点距离最小的前提下,使得树上的边的长度和最小.输出树上边的总长度,以及树上的边的序号(按输入顺序 1...m). 思路 : ...

  3. Codeforces 545E. Paths and Trees[最短路+贪心]

    [题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...

  4. [Codeforces 545E] Paths and Trees

    [题目链接] https://codeforces.com/contest/545/problem/E [算法] 首先求 u 到所有结点的最短路 记录每个节点最短路径上的最后一条边         答 ...

  5. codeforces 545E E. Paths and Trees(单源最短路+总权重最小)

    E. Paths and Trees time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

  6. Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心

    题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...

  7. Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路

    E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)

    E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  9. Codeforces Paths and Trees

    Paths and Trees time limit per test3 seconds memory limit per test256 megabytes Little girl Susie ac ...

随机推荐

  1. 条款7:为多态基类声明virtual析构函数

    C++明确指出:当派生类对象是由一个基类指针释放的,而基类中的析构函数不是虚函数,那么结果是未定义的.其实我们执行时其结果就是:只调用最上层基类的析构函数,派生类及其中间基类的析构函数得不到调用. # ...

  2. 从烙铁手到IT男

    时间:2015年8月27日 21:37:44 作者:luomg 摘要: 简要记录一写自己干的这个行当,多少是个回忆,不然某一天呜呼哀哉了啥也没有记录,会随着时间更新(大学时熬夜绘制了很多altium ...

  3. Python实现DBScan

    Python实现DBScan 运行环境 Pyhton3 numpy(科学计算包) matplotlib(画图所需,不画图可不必) 计算过程 st=>start: 开始 e=>end: 结束 ...

  4. Ubuntu系统安装配置Pintos和Bochs

    Ubuntu系统安装配置 Pintos 和 Bochs 安装过程 首先是UEFI启动模式下Win8.1安装Ubuntu14.04双系统,由于篇幅过长,就不在这里详写.可见博主的另一篇博客http:// ...

  5. tomcat设置内存大小

    -Xms256m -Xmx512m -XX:MaxNewSize=256m -XX:MaxPermSize=256m

  6. hasOwnProperty与isPrototypeOf

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Careercup - Microsoft面试题 - 4840369632051200

    2014-05-10 07:06 题目链接 原题: Suppose you have a collection of collection Eg : CEO-> Vps-> GMs -&g ...

  8. Mysql 慢查询设置

    Mysql慢查询设置 分析MySQL语句查询性能的方法除了使用 EXPLAIN 输出执行计划,还可以让MySQL记录下查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为“慢查询”. === ...

  9. JS 学习笔记--12---面向对象

    练习中使用的浏览器为IE10,如果各位朋友有不同意见或者本文有什么错误地方,望指正 ECMASCript有两种开发模式:函数式(面向过程)和面向对象.面向对象有一个很明显的标志,那就是类,我们可以通过 ...

  10. bzoj 3039 悬线法求最大01子矩阵

    首先预处理每个F点左右,下一共有多少个F点,然后 对于每个为0的点(R),从这个点开始,一直到这个点 下面第一个R点,这一区间中的min(左),min(右)更新答案. ps:我估计这道题数据有的格式不 ...