545E. Paths and Trees
题意:给定一个无向图和一个点u,找出若干条边组成一个子图,要求这个子图中u到其他个点的最短距离与在原图中的相等,并且要求子图所有边的权重之和最小,求出最小值并输出子图的边号。
思路:先求一遍最短路,从所有到i点的满足最短路的边中选一条权最小的边。
Java程序
- import java.io.PrintStream;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
- import java.util.Scanner;
- public class E545 {
- private static class Edge {
- int v;
- long w;
- int index;
- Edge(int v, long w, int index) {
- this.v = v;
- this.w = w;
- this.index = index;
- }
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- PrintStream out = System.out;
- int n = in.nextInt(), m = in.nextInt();
- List<Edge>[] graph = new List[n];
- for (int i = 0; i < n; i++) {
- graph[i] = new ArrayList<E545.Edge>();
- }
- for (int i = 1; i <= m; i++) {
- int v1 = in.nextInt() - 1;
- int v2 = in.nextInt() - 1;
- long w = in.nextLong();
- graph[v1].add(new Edge(v2, w, i));
- graph[v2].add(new Edge(v1, w, i));
- }
- int u = in.nextInt() - 1;
- Edge[] lastEdge = new Edge[n];
- final long[] min = new long[n];
- for (int i = 0; i < n; i++) {
- min[i] = -1;
- }
- min[u] = 0;
- Queue<Integer> q = new LinkedList<Integer>();
- q.add(u);
- while (!q.isEmpty()) {
- int v = q.poll();
- for (Edge edge : graph[v]) {
- int v1 = edge.v;
- long min1 = min[v] + edge.w;
- if ((min[v1] == -1) || (min1 < min[v1])
- || (min1 == min[v1] && edge.w < lastEdge[v1].w)) {
- min[v1] = min1;
- lastEdge[v1] = edge;
- q.add(v1);
- }
- }
- }
- long res = 0;
- boolean[] f = new boolean[m];
- for (int i = 0; i < n; i++) {
- if (lastEdge[i] != null) {
- res += lastEdge[i].w;
- f[lastEdge[i].index - 1] = true;
- }
- }
- out.println(res);
- StringBuilder s = new StringBuilder();
- boolean first = true;
- for (int i = 0; i < m; i++) {
- if (f[i]) {
- if (!first) {
- s.append(" ");
- }
- s.append(i + 1);
- first = false;
- }
- }
- out.println(s.toString());
- in.close();
- out.close();
- }
- }
Python代码
- import heapq as hq
- class edge(object):
- def __init__(self, to, w, nr):
- self.to = to
- self.w = w
- self.nr = nr
- n, m = map(int, raw_input().split())
- adj = [[] for _ in range(n + 1)]
- for i in range(1, m+1):
- u, v, c = map(int, raw_input().split())
- adj[u].append((v, c, i))
- adj[v].append((u, c, i))
- root = int(raw_input())
- vis = [False] * (n+1)
- q = [(0, 0, root, 0)]
- ans = []
- tot = 0
- while q:
- d, c, n, e = hq.heappop(q)
- if vis[n]:
- continue
- ans.append(e)
- tot += c
- vis[n] = True
- for v, c, i in adj[n]:
- if not vis[v]:
- hq.heappush(q, (d+c, c, v, i))
- ans = map(str, ans)
- print tot
- print " ".join(ans[1:])
上面的代码都是在codeforces上面抄过来的,自己写不出来。。。。
545E. Paths and Trees的更多相关文章
- Codeforces 545E. Paths and Trees 最短路
E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...
- CF 545E Paths and Trees
题目大意:给出n个点,m条无向边,每条边有长度.求一棵树,要求树上的每个点到源点距离最小的前提下,使得树上的边的长度和最小.输出树上边的总长度,以及树上的边的序号(按输入顺序 1...m). 思路 : ...
- Codeforces 545E. Paths and Trees[最短路+贪心]
[题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...
- [Codeforces 545E] Paths and Trees
[题目链接] https://codeforces.com/contest/545/problem/E [算法] 首先求 u 到所有结点的最短路 记录每个节点最短路径上的最后一条边 答 ...
- codeforces 545E E. Paths and Trees(单源最短路+总权重最小)
E. Paths and Trees time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Paths and Trees
Paths and Trees time limit per test3 seconds memory limit per test256 megabytes Little girl Susie ac ...
随机推荐
- 条款7:为多态基类声明virtual析构函数
C++明确指出:当派生类对象是由一个基类指针释放的,而基类中的析构函数不是虚函数,那么结果是未定义的.其实我们执行时其结果就是:只调用最上层基类的析构函数,派生类及其中间基类的析构函数得不到调用. # ...
- 从烙铁手到IT男
时间:2015年8月27日 21:37:44 作者:luomg 摘要: 简要记录一写自己干的这个行当,多少是个回忆,不然某一天呜呼哀哉了啥也没有记录,会随着时间更新(大学时熬夜绘制了很多altium ...
- Python实现DBScan
Python实现DBScan 运行环境 Pyhton3 numpy(科学计算包) matplotlib(画图所需,不画图可不必) 计算过程 st=>start: 开始 e=>end: 结束 ...
- Ubuntu系统安装配置Pintos和Bochs
Ubuntu系统安装配置 Pintos 和 Bochs 安装过程 首先是UEFI启动模式下Win8.1安装Ubuntu14.04双系统,由于篇幅过长,就不在这里详写.可见博主的另一篇博客http:// ...
- tomcat设置内存大小
-Xms256m -Xmx512m -XX:MaxNewSize=256m -XX:MaxPermSize=256m
- hasOwnProperty与isPrototypeOf
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Careercup - Microsoft面试题 - 4840369632051200
2014-05-10 07:06 题目链接 原题: Suppose you have a collection of collection Eg : CEO-> Vps-> GMs -&g ...
- Mysql 慢查询设置
Mysql慢查询设置 分析MySQL语句查询性能的方法除了使用 EXPLAIN 输出执行计划,还可以让MySQL记录下查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为“慢查询”. === ...
- JS 学习笔记--12---面向对象
练习中使用的浏览器为IE10,如果各位朋友有不同意见或者本文有什么错误地方,望指正 ECMASCript有两种开发模式:函数式(面向过程)和面向对象.面向对象有一个很明显的标志,那就是类,我们可以通过 ...
- bzoj 3039 悬线法求最大01子矩阵
首先预处理每个F点左右,下一共有多少个F点,然后 对于每个为0的点(R),从这个点开始,一直到这个点 下面第一个R点,这一区间中的min(左),min(右)更新答案. ps:我估计这道题数据有的格式不 ...