题目链接

题意:给定一个无向图和一个点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. 从零开始学ios开发(一):准备起航

    首先介绍一下自己的背景,本人09年研究生毕业,大学就不介绍了,反正是上海的一所211大学,学的是计算机科学与技术专业,学生时代,从事过ACM,没有什么太大的成就,中国的牛人是在太多,我的水平,估计连高 ...

  2. 横屏下的ImagePickerController

    Try this way.... As per Apple Document, ImagePicker Controller never Rotate in Landscape mode. You h ...

  3. Nginx + django windows下配置

    1.下载nginx, 去http://nginx.org/en/download.html 下载,我下载的是1.8 stable版本. 2.配置文件/conf/nginx.conf #user nob ...

  4. lua编程基础

    1.目前最新的lua版本是lua5.2.3 2.官网下载地址:http://www.lua.org/ftp/ 3.lua的初衷就是一个用于c/c++的小巧的脚本语言,本身是什么功能都没有的,需要手动用 ...

  5. 程序调试手段之gdb, vxworks shell

    调试一个程序主要用到的功能: 启动程序 设置函数断点 设置数据断点 单步执行 查看内存值 修改内存值 linux下的gdb,和vxworks下的shell 虽然使用方式和调试命令略有不同,但是都能满足 ...

  6. zepto判断手机横竖屏

    var CheckOrientation = (function(){ var win = $( window ), get_orientation, last_orientation, initia ...

  7. Noip模拟考第三题——饥饿游戏

    饥饿游戏 (hungry.pas/c/cpp) [问题描述] Chanxer饿了,但是囊中羞涩,于是他去参加号称免费吃到饱的“饥饿游戏”. 这个游戏的规则是这样的,举办者会摆出一排 个食物,希望你能够 ...

  8. shadowmap 及优化

    对于子阴影的走样, 条纹 开zbias resterizeState zbias = 1000...大概这样 另一个方法是画背面 backface是指一个人肚子那面,后背那面 而不是肚子的里面那层 所 ...

  9. Codeforces Round #204 (Div. 2)->B. Jeff and Periods

    B. Jeff and Periods time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. 12 高性能I/O框架库Libevent

    这里不讲Libevent库的具体内容了,从宏观上对I/O库整体做个介绍 Linux服务器程序必须处理三类事件:I/O事件,信号和定时事件 统一事件源:统一处理这三类事件既能使代码简单易懂,又能避免一些 ...