题目链接

题意:给定一个无向图和一个点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的更多相关文章

  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. HTML5七大优势“逼宫”APP

    HTML5颠覆了PC互联网的格局,优化了移动互联网的体验,接下来几年,HTML5将颠覆原生App世界. 跨平台: 在多屏年代,开发者的痛苦指数非常高,人人都期盼HTML5能扮演救星.多套代码.不同技术 ...

  2. ExtJS FormPanel不执行校验

    经检查问题原因在于使用了 validator 属性. 使用validator属性,必须添加返回值.不添加返回值,就会出现FormPanel不执行校验的问题.

  3. Linux I/O总结

    文件流 标准I/O文件流可用于单字节或多字节字符集.流的定向决定了所读写的是单字节还是多字节.流在最初创建时,并没有定向,此时如果在为定向的流上使用多字节I/O函数,那么该流被设置为宽定向的:如果在为 ...

  4. SharePoint 2010 中使用Ztree和EasyUI样式冲突问题

    <style type="text/css"> /*解决ztree和SharePoint样式冲突问题*/ .ztree li a { display: inline-b ...

  5. generate the next AttestationNumber, 格式是ICD-EPRG-DEV-0000000001,ICD-EPRG-DEV-0000000002

    private static int GetNextAttestationNumber(string maxAttestationNumber) { //generate the next Attes ...

  6. Android实现Button事件的处理

    Android实现Button事件的处理 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是最基本的线性布局,给每个控件设立id值,以供代 ...

  7. Jquery Mobile局部刷新后js和css失效,需局部渲染

    $(function () {    $("#submit").click(function(){      var storage = window.localStorage;  ...

  8. “我爱淘”冲刺阶段Scrum站立会议3

    完成任务: 将搜索框的界面已经实现以及部署到整个框架中. 计划任务: 实现搜索功能,通过数据库的链接,实现用户可以查到自己需要的书籍的信息. 遇到问题: 1.数据库的操作,怎么实现查询功能: 2.Ac ...

  9. preseed.cfg分区设定案例

    很久之前做ubuntu的PXE配置ubuntu的preseed费了很大的力气,总结的不多,现在温习一下. 就我所接触的,有分区普通磁盘,LVM,和raid三种方式.其中前两中方式比较多,raid方式是 ...

  10. ios-仿新浪微博app-第1天UI搭建

    1:不用storyboard 点击工程删除main  ui加载全部手码  >> 在application的代理方法didFinishLaunchingWithOptions中添加代码显示w ...